Unformatted text preview:

CMSC 212 Project #3 Due 03/27/2007 8:00 PM Background This assignment builds on what you learned about assembly language and the SPIN instruction set in assignment #1. In this assignment, you will write code to convert assembly language program into machine code (called an assembler). You will also build an interpreter to simulate the execution of instructions in the SPIN machine language. The Simulated Computer Recall from assignment #1, that the simulated computer has a memory that contains 216 (65,536) words of memory, each 32 bits long. In addition to memory, the computer has 16 registers that can be used to hold values. Two of the registers are special. R0 is hardwired to 0 and writing to it is legal, but doesn’t change it, but reading from it returns 0. R1 is the “Program Counter” and always contains the address of the next instruction to execute. R1 can be read like a normal register, but can only be modified using special instructions (bal and beq), any other attempt to modify it is an Illegal Instruction (including using it as the register1 value of bal) R2-R15 are General Purpose Registers, and can be read or written. Description of instructions lw <register1> <register2> <memory> Copies the value stored in the memory location <memory> + <register2> into the register location <register1>. For example if register2 is R0 and memory is 42, the data stored in location 42 is loaded into the register. However, if the second operand was R4 (rather than R4) and R4 contained 22, the values would be loaded from memory location 42 + 22 = 64. (opcode 0) li <register1> <number> Copies the supplied number <number> into the register location <register1> (opcode 1). The number must be an unsigned integer in the range 0 to 65535 inclusive. mv <register1> <register2> Copies the value stored in <register2> into <register1> (opcode 2) sw <register1> <register2> <memory> Copies the value stored in <register1> into memory location <memory> + the contents of <register2> (opcode 3)add <register1> <register2> <register3> Adds the value stored in <register1> to the value stored in <register2> and stores the result into <register3> (opcode 4) neg <register1> Negates the value stored in <register1> (i.e. 1 becomes –1), (opcode 5) beq <register1> <register2> <memory> If the value stored in <register1> is equal to the value stored in <register2>, change the program counter (next instruction to execute) to execute the instruction stored in <memory> next. If they are not equal the instruction has no effect (opcode 6) bal <register1> <register2> <memory> Stores the current value of R1 (program counter) into <register1>, Sets the value of R1 (program counter) to the contents of <register2> plus the value of the <memory> field. (opcode 7) read <register1> Read an integer from standard input, and store the value in <register1> (opcode 8) write <register1> Write the integer value stored in <register1> to standard output. The instruction should print a newline after the integer. (opcode 9) halt Terminates execution of the machine. No operands are used (opcode 10) In addition, in the assembly language there is a special directive called .data. A .data directive is used to specify a memory location that contains information other than machine instructions (such as data values). The syntax of this directive is: .data const1 Where const1 is a 32 bit signed integer value to define what should be stored in that location. Also, before any instruction or .data directive can be a label. A label is a symbolic name for that memory location. Labels may be up to 32 characters long and may contain any characters except a space or tab. However, a label may not start with a digit (0-9). Immediately after a label definition is a colon character (:). Here are two sample assembly instructions with labels defined. loop1: add R3 R3 R4 name: .data 1234567Labels may be used anywhere an instruction takes a memory operand. The value of labels is that it make it easier to read and modify assembly code. For example, if you need to insert a new instruction between two instructions, if memory locations were specified numerically, it would be tedious to make all of the changes. Instructions may define their memory field using either a label or a numeric value. Here are some examples of instructions that use labels. lw R4 name beq R3 R5 loop1 The Assignment In this assignment, you will write two functions (and several helper functions). int assemble(char *file, memoryLocation mem[]); This function reads input from the file name passed as the first parameter. Each line consists of assembly code and which should be assembled into instructions in the passed computer memory array mem. The first instruction read from input should be placed at location 0, and the next at location 1, and so on. If the input contains any errors, the return value should be negative and the value of the negative result is the line the first error was encountered. For example, if the first error is at line 4, the return value is -4. If the program contains no errors, the return value should be the number of memory locations filled in. When assembling instructions, the values of operands that are not used for a given instruction (i.e. operands 2 and 3 for the read instruction) should be set to zero. Your assembler should check for valid assembly files. Here is a list of the types of errors you should be able to detect. This is not exhaustive, but should get you thinking about the types of errors that can occur in an assembly program. • Labels that are used with an instruction, but not defined (i.e. label: missing). • Invalid op codes • Missing or Invalid operands • Invalid Memory address specified (out of the range 0-65535). • Invalid Register numbers specified Your assembler needs to process any valid input which includes sequences of multiple spaces or tabs in the input to separate operands, labels, or before the operator. Each instruction will be one line. Any input line with more than 256 characters is invalid, and is considered an error.int execute(memoryLocation mem[], int startingPC, bool traceFlag); This function simulates the execution of the SPIN architecture. Memory of the computer to simulate is passed in mem. The first memory location to execute is passed as startingPC. If the traceFlag is true, the simulate program should print out debugging output. The


View Full Document

UMD CMSC 212 - Project #3

Download Project #3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Project #3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Project #3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?