Homework 2
Homework 2
Homework 2
Prof. Forsyth
There is no turn-in for this portion of the assignment, but it is recommended that you attempt it in order
to familiarize yourself with the ISA.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
Note that this C code is just to help your understanding and does not need to be exactly followed. However,
your assembly code implementation should meet all of the given conditions in the description.
Open hanoi.s file in the assembly directory. This file contains an implementation of the Tower of Hanoi
minimal moves calculator program that is missing significant portions of the calling convention. Near the
bottom of the hanoi.s we have provided multiple numbers that you can use to test your homework. They are
located at labels testNumDisks1, testNumDisks2, testNumDisks3. Be sure to use these provided integers
by loading them from the labels into registers. None of the numbers provided and tested will be lower than
1.
Complete the program by implementing the various missing portions of the LC-4000 calling convention.
Each location where you need to implement a portion of the calling convention is marked with a TODO
label as well as a short hint describing the portion of the calling convention you should be implementing.
Please note that we will be testing your implementation for multiple different instances, so please do not
attempt to hardcode your solutions.
4 Deliverables
• hanoi.s: your assembly code from Section 2
• answers.txt: your answer to the problem from Section 3
Submit these files to Gradescope before the assignment deadline.
5 Local Debugging
To debug locally, you can use commands below.
python3 assembler.py -i lc4000 hanoi.s --bin hanoi.bin --sym hanoi.sym
python3 lc4000-sim.py hanoi.bin
To learn more about the python files used here, read Appendix B.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
6.1 Registers
The LC-4000 has 16 general-purpose registers. While there are no hardware-enforced restraints on the uses
of these registers, your code is expected to follow the conventions outlined below.
1. Register 0 is always read as zero. Any values written to it are discarded. Note: for the purposes of
this project, you must implement the zero register. Regardless of what is written to this register, it
should always output zero.
2. Register 1 is used to hold the target address of a jump. It may also be used by pseudo-instructions
generated by the assembler.
3. Register 2 is where you should store any returned value from a subroutine call.
4. Registers 3 - 5 are used to store function/subroutine arguments. Note: registers 2 through 8 should
be placed on the stack if the caller wants to retain those values. These registers are fair game for the
callee (subroutine) to trash.
5. Registers 6 - 8 are designated for temporary variables. The caller must save these registers if they
want these values to be retained.
6. Registers 9 - 11 are saved registers. The caller may assume that these registers are never tampered
with by the subroutine. If the subroutine needs these registers, then it should place them on the stack
and restore them before they jump back to the caller.
7. Register 12 is reserved for handling interrupts. While it should be implemented, it otherwise will not
have any special use on this assignment.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
8. Register 13 is the everchanging top of the stack; it keeps track of the top of the activation record for
a subroutine.
9. Register 14 is the anchor point of the activation frame. It is used to point to the first address on the
activation record for the currently executing process.
10. Register 15 is used to store the address a subroutine should return to when it is finished executing.
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
DR = SR1 + SR2;
Description
The ADD instruction obtains the first source operand from the SR1 register. The second source operand is
obtained from the SR2 register. The second operand is added to the first source operand, and the result is
stored in DR.
6.3.2 NAND
Assembler Syntax
NAND DR, SR1, SR2
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
DR = ~(SR1 & SR2);
Description
The NAND instruction performs a logical NAND (AND NOT) on the source operands obtained from SR1
and SR2. The result is stored in DR.
HINT: A logical NOT can be achieved by performing a NAND with both source operands the same.
The following assembly:
NAND DR, SR1, SR1
achieves the following logical operation: DR ← SR1.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
6.3.3 ADDI
Assembler Syntax
ADDI DR, SR1, immval20
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
DR = SR1 + SEXT(immval20);
Description
The ADDI instruction obtains the first source operand from the SR1 register. The second source operand is
obtained by sign-extending the immval20 field to 32 bits. The resulting operand is added to the first source
operand, and the result is stored in DR.
6.3.4 LW
Assembler Syntax
LW DR, offset20(BaseR)
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
DR = MEM[BaseR + SEXT(offset20)];
Description
An address is computed by sign-extending bits [19:0] to 32 bits and then adding this result to the contents
of the register specified by bits [23:20]. The 32-bit word at this address is loaded into DR.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
6.3.5 SW
Assembler Syntax
SW SR, offset20(BaseR)
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
MEM[BaseR + SEXT(offset20)] = SR;
Description
An address is computed by sign-extending bits [19:0] to 32 bits and then adding this result to the contents
of the register specified by bits [23:20]. The 32-bit word obtained from register SR is then stored at this
address.
6.3.6 BEQ
Assembler Syntax
BEQ SR1, SR2, offset20
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
if (SR1 == SR2) {
PC = incrementedPC + offset20
}
Description
A branch is taken if SR1 is equal to SR2. If this is the case, the PC will be set to the sum of the incremented
PC (since we have already undergone fetch) and the sign-extended offset[19:0].
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
6.3.7 JALR
Assembler Syntax
JALR AT, RA
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0110 AT RA unused
Operation
RA = PC;
PC = AT;
Description
First, the incremented PC (address of the instruction + 1) is stored in register RA. Next, the PC is loaded
with the value of register AT, and the computer resumes execution at the new PC.
6.3.8 HALT
Assembler Syntax
HALT
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0111 unused
Description
The machine is brought to a halt and executes no further instructions.
6.3.9 BLT
Assembler Syntax
BLT SR1, SR2, offset20
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
if (SR1 < SR2) {
PC = incrementedPC + offset20
}
Description
A branch is taken if SR1 is less than SR2. If this is the case, the PC will be set to the sum of the incremented
PC (since we have already undergone fetch) and the sign-extended offset[19:0].
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
6.3.10 LEA
Assembler Syntax
LEA DR, label
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
DR = PC + SEXT(PCoffset20);
Description
An address is computed by sign-extending bits [19:0] to 32 bits and adding this result to the incremented
PC (address of instruction + 1). It then stores the computed address in the register DR.
6.3.11 MIN
Assembler Syntax
MIN DR, SR1, SR2
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
if (SR1 < SR2) {
DR = SR1
} else {
DR = SR2
}
Description
The minimum is computed between the values in both source registers. It then stores the minimum value
in the register DR.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024
6.3.12 MAX
Assembler Syntax
MAX DR, SR1, SR2
Encoding
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Operation
if (SR1 > SR2) {
DR = SR1
} else {
DR = SR2
}
Description
The maximum is computed between the values in both source registers. It then stores the maximum value
in the register DR.
Homework 2 CS 2200 - Computer Systems and Networks Fall 2024