Assignment 5
Assembly Language Using MIPS
Translation and Assemblers
Due: Wednesday April 10 (Total: 30 points)
Problems (10 points)
0. Type in the following pseudo-instructions to SPIM, and write down the translated MIPS instructions.
.text
lw $7, label
la $8, label
.data
.space 400
label:
a. Explain in your own words why and how the real MIPS instructions accomplish the meaning of the pseudo instructions.
b. Write down the Hex representation for each translated instruction. Explain your answers.
1. Hand assemble the following lines. You can show your answers in Hex.
add $3, $6, $19
sb$3, 0($5)
lw $8, 4($5)
2. Hand assemble the following lines. You can show your answers in Hex.
and $5, $6, $18
beq $5, $0, br1
lui $20, 0x66aa
br1: lb $9, -8($20)
3. Reverse engineer these hex values into MIPS instructions. Make up label names if needed. Assume code starts at 0x0040 0000. Use this link to look up opcodes if you can't find them in the book's cheat sheet.
Address Contents
0x0040 0000 0x8166 0000
0x0040 0004 0x0068 a020
0x0040 0008 0x0013 a280
0x0040 000c 0x1274 fffc
Last Program - A One Operand Assembler
(20 points) The goal of this assignment is to write an assembler for the one operand, two address mode assembly language described below. You do not need to check for syntax errors. You do need to generate the machine code. You do not need to write this program in MIPS. Any language of your choice is okay.
The language has 8 instructions: Load, Store, Add, Sub, Bra, Bgtr, Bzr, End. Every instruction has at most one operand which must be a PC-relative address, or an immediate value. End has no parameters. Immediate values are base 10 integer numbers preceded by a #, and PC-relative addresses are written as labels. Every instruction is encoded in 16 bits. The first 3 bits of the instruction are for the opcode, the next bit indicates which of the 2 address modes to use (0 for PC-relative and 1 for immediate), and the last 12 bits hold either a two's complement offset for the PC-relative address (i.e. the difference in bytes between that line and the label), or a two's complement immediate value. Labels must be any 3 letters (even an op code like "end", "bra", or "add") followed immediately by a colon. The colon is what lets you distinguish between a label and an opcode. If a label has no instruction following it, then it is assumed to be an empty 16 bits of memory.
You may assume that every machine program will be loaded starting at memory location zero. Note that End, which has no parameter, is encoded by 111000000000000. Your program should read an assembly program line by line or from a file, and then it should print out the machine language version.
Recall, that the standard algorithm for writing an assembler uses two passes.
Pass 1: Read all labels and make a table of them with their addresses. Start at line 0, and scan each line for a leading label (3 letters and colon). Keep an array of labels and their addresses. Every line adds 2 to the current address. Strip off the labels as you go. You do not need them any more in Pass 2.
Pass 2: Start back at line 0 and parse each line, now knowing that there are no leading labels. Look up any address parameters in the label (symbol) table to calculate offsets when needed.
Examples:
Here are two examples assuming you number the instructions in order from 000 through 111, and 0 indicates PC-relative while 1 indicates immediate address mode:
abc: end:
Load #23 lop: Add #-1
Store abc Store end
End Bzr fin
Bra lop
fin: End
The output for each program is shown below:
000 1 000000010111 010 1 111111111111
001 0 111111111100 001 0 111111111100
111 0 000000000000 110 0 000000000100
100 0 111111111010
111 0 000000000000