Programming Using C Homework 5 1. In this problem you will simulate the execution of a computer s CPU. The computer that we will simulate has 32 bytes of addressable memory (each byte is uniquely addressed). The CPU of this machine consists of the ALU (Arithmetic and Logic Unit) that performs all operations, the Inst (Instruction) Register that holds the current instruction being executed, the PC (Program Counter) that keeps track of the location of the instruction to be executed, and the Accumulator that stores the results of the arithmetic calculations. Each memory location contains a value or an instruction to execute. If a memory location contains an instruction the first three bits contain the OPCode (operation code) and the remaining 5 bits contain the argument of the instruction. Since three bits are used to describe the instruction, our machine can execute eight unique instructions: Each of these instructions has a single argument that is 5 bits in length. These instructions, in detail, are: (a) Halt. Halt the execution of the computer. Execute no more instructions. This instruction ignores the argument. (b) JANZ. Jump if the Accumulator is not zero. Examine the accumulators value. If it is not zero, jump to the address given as the argument 1
(set the PC to that value). (c) StAM. Store the Accumulator in Memory. The argument of this command is a memory address. Make a copy of the value in the Accumulator and store it in this memory location. (d) LdAM. Load the Accumulator with Memory value. The argument of this command is a memory address. Make a copy of the value stored in this location and place it in the Accumulator. (e) AddM. Add Memory value to Accumulator. The argument of this command is a memory address. Make a copy of the value stored in this location and add it to the Accumulators value. (f) AddI. Add Immediate. The argument of this command is a value. Since the argument is only 5 bits long, the 3 bits missing in front should be considered to be 0 and this value should be added to the value currently in the Accumulator replacing the value that is currently there. (g) SubM. Subtract Memory value from Accumulator. The argument of this command is a memory address. The value in this location should be subtracted from the Accumulators value. (h) SubI. Subtract Immediate. The argument of this command is a value. Since the argument is only 5 bits long, the 3 bits missing in front should be considered to be 0 and this value should be subtracted from the value in the Accumulator. The result should be placed in the Accumulator. Each execution cycle of the CPU involves performing the following steps in the order given: (a) The value in the PC is examined to find the next instruction to execute. (b) This instruction is loaded into the Instruction Register. (c) The PC is incremented. 2
(d) The instruction is interpreted and its specified action(s) is performed. For example, suppose that the first eight bytes of memory contain the following instructions: Given this information in memory and starting execution at 00000, the following occurs: See if you can figure out what is in each memory position when the program ends. Your program should represent the memory as an array of null-terminated character strings of length 8. It should contain the following macros, global declarations and functions: #define MEM_WIDTH 8 #define MEM_SIZE 32 #define OPCODE_SIZE 3 int pc = 0; // program counter char ir[mem_width+1]; // instruction register char ac[mem_width+1]; //accumulator char memory[mem_size][mem_width+1]; //memory // sets the memory to some default content void set_memory(); 3
// reads the memory from keyboard void read_memory(); // prints the memory on screen void print_memory(); // converts an integer from decimal to binary, // represented as a null-terminated string // the result is always represented on MEM_WIDTH bits void dec2bin(int dec, char *bin); // converts an integer from binary, represented as a null-terminated string, // to decimal // the string can have any length (up to MEM_WIDTH), // depending on the position of the null character int bin2dec(char *bin); // executes the program in memory void execute(); The program should take one argument. If no argument is given it should print an informative message about the expected argument and terminate. If the argument is 0, the program uses the function set memory to set the memory to the content given below. If the argument is anything else, it should use read memory to read the memory from keyboard. If the user enters a byte 00000000 the program should stop reading input and assign 00000000 to the rest of the memory locations. The program then executes the program in memory, starting at adress 0, and then prints the content of the memory and the value of the acumulator after execution. 4
2. Consider the code of the following macro: // This macro calculates the SIGMA function, from bottom to top, // and places the result in sum. #define sigma(func, bottom, top, sum) \ do \ { \ int i; \ sum =0; \ for(i=bottom; i<=top; i++) \ sum+=func(i); \ } \ while (0) Write a program with the necessary functions and statements to calculate each of the following: (a) 100 i i=1 5
(b) (c) (d) 7 i= 3 25 i=20 15 i 3 i 2 i i=1 (e) 15 i=10 sin i Submission instructions: Submit 2 archived source code files on Sakai by July 16 11:59 PM. The files should be named cpu.c and sums.c and saved in a folder named homework5. Grading scale Comments: 5% Use of descriptive identifiers and symbolic constants: 5% Spacing and indentation: 5% Following submission instructions: 5% Problem1: 60% Problem2: 20% 6