Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

replace all the stub please, spammer\'s mom will die #include <stdio.h> #include

ID: 3604222 • Letter: R

Question

replace all the stub please, spammer's mom will die

#include <stdio.h>
#include <stdlib.h>   // For error exit()

// CPU Declarations -- a CPU is a structure with fields for the
// different parts of the CPU.
//
   typedef short int Word;       // type that represents a word of SDC memory
   typedef unsigned char Address;   // type that represents an SDC address

   #define MEMLEN 100
   #define NREG 10

   struct CPU {
       Word mem[MEMLEN];
       Word reg[NREG];   // Note: "register" is a reserved word
       Address pc;   // Program Counter
       int running;   // running = 1 iff CPU is executing instructions
       Word ir;   // Instruction Register
       int instr_sign;   //   sign of instruction
       int opcode;   //   opcode field
       int reg_R;   //   register field
       int addr_MM;   //   memory field
   };
   typedef struct CPU CPU;


// Prototypes [note the functions are also declared in this order]
//
   // *** STUB *** Lab 5 items omitted ....

   int read_execute_command(CPU *cpu);
   int execute_command(char cmd_char, CPU *cpu);
   void help_message(void);
   void many_instruction_cycles(int nbr_cycles, CPU *cpu);
   void one_instruction_cycle(CPU *cpu);

   void exec_HLT(CPU *cpu);
   // *** STUB ***

// Main program: Initialize the cpu, and read the initial memory values.
// Then run the command loop to let the user execute the program.
//
int main(int argc, char *argv[]) {
printf("SDC Simulator pt 2 for ***Your name, Lab section *** "); // STUB
   CPU cpu_value, *cpu = &cpu_value;

   // *** STUB *** Lab 5 items omitted (initialize CPU and memory) ....

   // Run the command loop
   //
   char *prompt = "> ";
   printf(" Beginning execution; type h for help %s", prompt);

   int done = read_execute_command(cpu);
   while (!done) {
       printf("%s", prompt);
       done = read_execute_command(cpu);
   }
   return 0;
}


// *** STUB ***   Rest of Lab 5 items omitted
// (initialize_control_unit, ..., print_instr)


// Read a simulator command from the keyboard (q, h, ?, d, number,
// or empty line) and execute it. Return true if we hit end-of-input
// or execute_command told us to quit.   Otherwise return false.
//
int read_execute_command(CPU *cpu) {
// Buffer to read next command line into
#define CMD_LINE_LEN 256
   char cmd_line[CMD_LINE_LEN];
   char *read_success;       // NULL if reading in a line fails.

   int nbr_cycles;       // Number of instruction cycles to execute
   char cmd_char;       // Command 'q', 'h', '?', 'd', or ' '
   int done = 0;       // Should simulator stop?

   read_success = fgets(cmd_line, CMD_LINE_LEN, stdin);

   // *** STUB ***   done if we have we hit eof

   // *** STUB ***
   // while not done,
   // Handle q, h, ?, d commands, integer (nbr of instruction cycles),
   // or empty line (one instruction cycle)
   // Read next command line, check for eof

   return done;
}

// Execute a nonnumeric command; complain if it's not 'h', '?',
// 'd', 'q' or ' '. Return true for the q command, false otherwise.
//
int execute_command(char cmd_char, CPU *cpu) {
   if (cmd_char == '?' || cmd_char == 'h') {
       help_message();
   }
   // *** STUB ****
   return 0;
}

// Print standard message for simulator help command ('h' or '?')
//
void help_message(void) {
   // *** STUB ****
}

// Execute a number of instruction cycles. Exceptions: If the
// number of cycles is <= 0, complain and return; if the CPU is
// not running, say so and return; if the number of cycles is
// unreasonably large, warn the user and substitute a limit.
//
// If, as we execute the many cycles, the CPU stops running,
// just stop and return.
//
void many_instruction_cycles(int nbr_cycles, CPU *cpu) {
   int REASONABLE_NBR_CYCLES = 100;

   // *** STUB ****
}

// Execute one instruction cycle
//
void one_instruction_cycle(CPU *cpu) {
   // If the CPU isn't running, say so and return.
   // If the pc is out of range, complain and stop running the CPU.
   //
   // *** STUB ****

   // Get instruction and increment pc
   //
   // For printing purposes, we'll save the location of
   // the instruction (the pc before the increment).
   //
   int instr_loc = cpu -> pc;
   cpu -> ir = cpu -> mem[cpu -> pc];
   (cpu -> pc)++;

   // Decode instruction into opcode, reg_R, addr_MM, and instruction sign
   //
   // *** STUB ****

   // Echo instruction
   // *** STUB ***

   switch (cpu -> opcode) {
   case 0: exec_HLT(cpu); break;
   // *** STUB ****
   default: printf("Bad opcode!? %d ", cpu -> opcode);
   }
}

// Execute the halt instruction (make CPU stop running)
//
void exec_HLT(CPU *cpu) {
   // *** STUB *** Print a message?
   cpu -> running = 0;
}

For this lab, you have to add a command-processing loop: You read a line containing a simulator command and execute it, read another line and execute it, and so on until you are given the quit command. There are six commands q for quit, d for dump control ut and memory, h and ? for help, an integer (to execute that many instruction cycles), or an empty line (to execute one instruction cycle) 1. To start the command loop, prompt for and read a command line. (You'll want to use the fgets /sscanf technique from the previous lab.) See if the line is empty or has a command. If you hit end-of-file on standard input, exit the program 2. For command q, note that you've seen a quit command and exit the program. (Don't dump things back out, just quit.) 3. For command d, dump out the control unit and the memory values. 4. For h or 2, print out a help message. 5. For an integer (let's call it N), execute the instruction cycles that many times but make sure N is reasonable first.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>   // For error exit()

// CPU Declarations -- a CPU is a structure with fields for the
// different parts of the CPU.
//
   typedef short int Word;       // type that represents a word of SDC memory
   typedef unsigned char Address;   // type that represents an SDC address

   #define MEMLEN 100
   #define NREG 10

   struct CPU {
       Word mem[MEMLEN];
       Word reg[NREG];   // Note: "register" is a reserved word
       Address pc;   // Program Counter
       int running;   // running = 1 iff CPU is executing instructions
       Word ir;   // Instruction Register
       int instr_sign;   //   sign of instruction
       int opcode;   //   opcode field
       int reg_R;   //   register field
       int addr_MM;   //   memory field
   };
   typedef struct CPU CPU;


// Prototypes [note the functions are also declared in this order]
//
   // *** STUB *** Lab 5 items omitted ....

   int read_execute_command(CPU *cpu);
   int execute_command(char cmd_char, CPU *cpu);
   void help_message(void);
   void many_instruction_cycles(int nbr_cycles, CPU *cpu);
   void one_instruction_cycle(CPU *cpu);

   void exec_HLT(CPU *cpu);
   // *** STUB ***

// Main program: Initialize the cpu, and read the initial memory values.
// Then run the command loop to let the user execute the program.
//
int main(int argc, char *argv[]) {
printf("SDC Simulator pt 2 for ***Your name, Lab section *** "); // STUB
   CPU cpu_value, *cpu = &cpu_value;

   // *** STUB *** Lab 5 items omitted (initialize CPU and memory) ....

   // Run the command loop
   //
   char *prompt = "> ";
   printf(" Beginning execution; type h for help %s", prompt);

   int done = read_execute_command(cpu);
   while (!done) {
       printf("%s", prompt);
       done = read_execute_command(cpu);
   }
   return 0;
}


// *** STUB ***   Rest of Lab 5 items omitted
// (initialize_control_unit, ..., print_instr)


// Read a simulator command from the keyboard (q, h, ?, d, number,
// or empty line) and execute it. Return true if we hit end-of-input
// or execute_command told us to quit.   Otherwise return false.
//
int read_execute_command(CPU *cpu) {
// Buffer to read next command line into
#define CMD_LINE_LEN 256
   char cmd_line[CMD_LINE_LEN];
   char *read_success;       // NULL if reading in a line fails.

   int nbr_cycles;       // Number of instruction cycles to execute
   char cmd_char;       // Command 'q', 'h', '?', 'd', or ' '
   int done = 0;       // Should simulator stop?

   read_success = fgets(cmd_line, CMD_LINE_LEN, stdin);

   // *** STUB ***   done if we have we hit eof

   // *** STUB ***
   // while not done,
   // Handle q, h, ?, d commands, integer (nbr of instruction cycles),
   // or empty line (one instruction cycle)
   // Read next command line, check for eof

   return done;
}

// Execute a nonnumeric command; complain if it's not 'h', '?',
// 'd', 'q' or ' '. Return true for the q command, false otherwise.
//
int execute_command(char cmd_char, CPU *cpu) {
   if (cmd_char == '?' || cmd_char == 'h') {
       help_message();
   }
   // *** STUB ****
   return 0;
}

// Print standard message for simulator help command ('h' or '?')
//
void help_message(void) {
   // *** STUB ****
}

// Execute a number of instruction cycles. Exceptions: If the
// number of cycles is <= 0, complain and return; if the CPU is
// not running, say so and return; if the number of cycles is
// unreasonably large, warn the user and substitute a limit.
//
// If, as we execute the many cycles, the CPU stops running,
// just stop and return.
//
void many_instruction_cycles(int nbr_cycles, CPU *cpu) {
   int REASONABLE_NBR_CYCLES = 100;

   // *** STUB ****
}

// Execute one instruction cycle
//
void one_instruction_cycle(CPU *cpu) {
   // If the CPU isn't running, say so and return.
   // If the pc is out of range, complain and stop running the CPU.
   //
   // *** STUB ****

   // Get instruction and increment pc
   //
   // For printing purposes, we'll save the location of
   // the instruction (the pc before the increment).
   //
   int instr_loc = cpu -> pc;
   cpu -> ir = cpu -> mem[cpu -> pc];
   (cpu -> pc)++;

   // Decode instruction into opcode, reg_R, addr_MM, and instruction sign
   //
   // *** STUB ****

   // Echo instruction
   // *** STUB ***

   switch (cpu -> opcode) {
   case 0: exec_HLT(cpu); break;
   // *** STUB ****
   default: printf("Bad opcode!? %d ", cpu -> opcode);
   }
}

// Execute the halt instruction (make CPU stop running)
//
void exec_HLT(CPU *cpu) {
   // *** STUB *** Print a message?
   cpu -> r