This assignment serves as an introduction to the 16-bit LC-2200 processor assembly language. There are two problems:
This problem introduces the 16-bit LC-2200 assembly language and has you hand- and machine-assemble a few instructions.
Figure 1: The LC-2200-16 Datapath (Conceptual)
The LC-2200-16 (Little Computer 2200-16 bits) is very simple, but it is general enough to solve complex problems. This section describes the instruction set and instruction format of the LC-2200. The LC-2200 is a 16-register, 16-bit computer. All addresses are word (2 bytes)-addresses.
Although the 16 registers are known as general purpose registers, they are generally assigned special duties by software convention.
Reg# |
Name |
Use |
Callee Save? |
0 |
$zero |
always zero (by hardware) |
n.a. |
1 |
$at |
reserved for assembler |
n.a. |
2 |
$v0 |
return value |
no |
3 |
$a0 |
argument |
no |
4 |
$a1 |
argument |
no |
5 |
$a2 |
argument |
no |
6 |
$t0 |
temporary |
no |
7 |
$t1 |
temporary |
no |
8 |
$t2 |
temporary |
no |
9 |
$s0 |
saved register |
YES |
10 |
$s1 |
saved register |
YES |
11 |
$s2 |
saved register |
YES |
12 |
$k0 |
reserved for OS/traps |
n.a. |
13 |
$sp |
stack pointer |
No |
14 |
$fp |
frame pointer |
YES |
15 |
$ra |
return address |
No |
Register 0: This register will always contain zero when read from. As an additional feature it may be written to in those cases where a value is not needed.
Registers 1: Although this is a general purpose register by convention, programmers should not use it. It may be used by the assembler when processing pseudo-instructions.
Register 2: Is designated as the register used to return values from functions.
Registers 3-5: Are designated to be used for passing arguments to functions.
Registers 6-8: Are designated for temporary variables.
Note: When calling a function the programmer should assume that the contents of registers 2-8 that were present when the call was made will no longer be valid. Thus, if needed after the call those values should be saved by the programmer calling the function.
Registers 9-11: These are saved registers. The caller of a function may assume that once the function returns the values that were in these registers before the call will still be there.
Note: This implies that a programmer writing a function that wishes to use these registers should first save them (most likely on the activation stack), then use them and then restore them before returning control to the caller
Register 12: This register is reserved for handling interrupts.
Register 13: The stack pointer which is used to keep track of the location of the top of the activation stack.
Register 14: You do not have to worry about this register.
Register 15: When a function is called the JALR instruction will save the address to return to and by convention this register is used for that purpose.
Instructions are 16 bits wide and require two sequential fetches from memory. There are 5 instruction formats (bit 0 is the least- significant bit).
R-type instructions (add, nand): bits 15-13: opcode bits 12- 9: RX bits 8- 5: RY bits 4- 1: RZ bit 0 unused I-type instructions (addi, lw, sw, beq): bits 15-13: opcode bits 12- 9: RX bits 8- 5: RY bits 4- 0: offsetField (a 5-bit, 2's complement number with a range of -16 to +15) J-type instructions (jalr): bits 15-13: opcode bits 12- 9: RX bits 8- 5: RY bits 4- 0: unused (should all be 0) S-type instructions (spop): bits 15-13: opcode bits 12- 2: unused (should all be 0) bits 1- 0: control code
Symbolic instructions should follow the same layout. For example, the add instructions
is written in assembly as:
add <RX><RY><RZ>
------------------------------------------------------------------ Table 1: Description of Machine Instructions ------------------------------------------------------------------ Assembly language Opcode (binary Action name for instruction (bits 15-13) ------------------------------------------------------------------ add (R-type format) 000 Add contents of RY with ex: add $v0, $a0, $a1 contents of RZ, store results in RX. nand (R-type format) 001 Nand contents of RY with ex: nand $v0, $a0, $a1 contents of RZ, store results in RX. addi (I-type format) 010 Add contents of RY to ex: addi $v0, $a0, 25 contents of offset field and store result in RX. lw (I-type format) 011 Load RX from memory. ex: lw $v0, 0x42($sp) Memory address is formed by adding offsetField with the contents of RY. sw (I-type format) 100 Store RX into memory. ex: sw $a0, 0x42($sp) Memory address is formed by adding offsetField with the contents of RY. beq (I-type format) 101 Compare the contents of RX ex: beq $a0, $a1, done and RY; if they are the same, then branch to the address PC+2+offsetField, where PC is the address of the beq instruction. Note that since all instructions begin at an address that is some multiple of 2, the beq instruction will leftshift the offset field by one bit in order to double the range for branch targets. jalr (J-type format) 110 First store PC+2 into RY, ex: jalr $at, $ra where PC is the address of the jalr instruction. Then branch to the address now contained in RX. Note that if RX is the same as RY, the processor will first store PC+2 into that register, then end up branching to PC+2. spop (S-type format) 111 Perform the action as ex: spop 0 determined by the control code (last 2 bits) cc=0 Halt processor
Like many processors, an assembler for the LC-2200-16 would supply a number of pseudo instructions:
halt Simply emits a spop 0. Halts the processor. noop No operation: does nothing ex: noop (actually emits " "add $zero, $zero, $zero") .word (pseudo-op) fill word with a value. ex: .word 32 The assembler supports labels which represent the address of the line.
If a label is used in a beq instruction, it will evaluate to the the relative offset leftshifted by one bit.
A. [0 points] Play around with the simulator. Try
writing some simple programs to copy numbers from one register to another, to
load and store values in memory, and to get used to the syntax for the
assembler. To use the simulator, extract the tarball, cd into the created
directory, and run "./config.sh". This is a config script that will set up
a Makefile based on your system. If you want to install it, give it paths
for installation; otherwise, just press enter at all the prompts to NOT
install it and just create the executables in that directory. Then run
make to actually build the executables. If you have the Qt libraries installed,
you will get a graphical simulator in addition to the standard text simulator.
The simulator comes with a help option to let you explore its options.
This
problem has you use the LC-2200 assembly language to write a simple program.
A. [30 points] Define a procedure calling convention
for the LC-2200 assembly language and machine model. The answer should be in
the form of a written specification in enough detail so that one person could
write a procedure (or a procedure call) to be used as part of another person's
program. Use the standard convention as described in class.
Be sure to explicitly address the standard issues:
B. [70 points] Write a function in LC-2200 assembly
language to compute pow(n, m), that is, n raised to the mth power.
(For Example, pow(2, 4) should return 16.
Your function should follow the calling convention that you have outlined above.
YOUR FUNCTION MUST BE RECURSIVE!
Iterative solutions to this problem will not receive credit!
You must implement recursion using the stack and stack pointer in order to receive full credit.
If you have questions about how to do this, please post to the newsgroup or ask in help session.
Make sure that pow.s is in unix-format and not in Dos/Windows format. Otherwise, you will not be able to assemble it!
Turn ALL of your files into T-Square in a .zip or .tar.gz file:
End of CS2200 Homework 1