Gettysburg College

CS 221
Computer Organization and Assembly Language Programming

Fall 2025

Assignment 12

Due: Thu, Dec 11, by 11:59pm

Readings

Description and Testing

This is a continuation of Assignment 11. The goal is to add functionality for stack manipulation and user defined variables/arrays.

Separate Files

Organize the code into separate files:

Here is an example:

Student.h | Student.cpp | main.cpp

ArmToHack Translator (Stack)

Each translated program should start by initializing the Stack to the appropriate address in Memory so that the stack behaves as in our earlier setup for ARM. (The user may change that value later, but it should start with the expected value.)

ArmToHack Translator (Basic LDMIB and STMDA)

Add support for basic LDMIB and STMDB. To simplify, for now assume that the register list has a single entry:

STMDA Rn!, {R7}
LDMIB Rn!, {R7}

Here are sample test programs. The first writes three registers on the stack, the second reads three values from the stack.

For convenience each changes the default value of SP and sets it closer to the the top. What should be the value of SP when the first program is done? the second?

program11.arm (should see 12,13,11 in cells 20,19,18)

program12.arm (should see R1=11,R2=12,R3=13)

ArmToHack Translator (Full LDMIB and STMDA)

Add support for LDMIB and STMDB with multi-entry register list. The ARM user will be expected to list the registers in the same order for related STMDA and LDMIB instructions (for example saving and restoring registers for a function call).

STMDA Rn!, {R7, R2, R11}
LDMIB Rn!, {R7, R2, R11}

Here are sample test programs. The first writes three registers on the stack, the second reads three values from the stack.

For convenience each changes the default value of SP and sets it closer to the top. What should be the value of SP when the first program is done? the second?

program13.arm | program14.arm

ArmToHack Translator (LDR and STR)

Add support for LDR and STR:

STR Rs, [Rb, Ri, LSL #2]
LDR Rd, [Rb, Ri, LSL #2]

STR Rs, [Rb, #±N]
LDR Rd, [Rb, #±N]

Here Rb is the base register and Ri is the index which may also be a numeric value (i.e. Operand2). The LSL will be ignored, since the Hack alignment is on individual cells, not the 4 bytes of ARM.

program15.arm (should see R7=42)

program16.arm (should see R3=42, R4=24, R5=66)

program17.arm (should see 42 in cell 19)

program18.arm (should see 42, 24, 66 in cells 18,19,20)

Defined Variables

Add support for user defined arrays/variables.

numbers DCD 1, +2, -3
value DCD 4

All user defined variables will start immediately after the registers, i.e. from index 16.

Each value will be allocated just one Hack cell. In the above example, the array numbers will occupy cells 16-18 and the array value will occupy cells 19.

program19.arm (should see 1..4 in cells 16..19)

program20.arm (should see 1..8 in cells 16..23)

Defined Variables and LDR

Extend the definition of LDR to include the option of loading the base of an array:

LDR Rd, =array

This will require another lookup table to store the base of each array. In the above example, the base of array numbers is 16 and the base of array value is 19.

program21.arm (should see 4,2,-3,1 in cells 16..19)

program22.arm (should see 1,4,5,5,4,5,7 in registers R4..R10)

ASR Instruction (R0,R1 only)

Add support for the ASR instruction (Arithmetic Shift Right) but only for 1 shift. This effectively performs division by 2.

ASR DestReg, RegToShift, #1

To accomplish this, write a mini-program in Hack Assembly that keeps subtracting 2 from R0 until we cannot subract 2 anymore from R0. Along the way keep track in R1 how many times the program ran. (Think of this as if it was assigned as a Hack programming problem in Assignment 10.)

Once you have the mini-program, write its source code for ASR. This is the same as writing, for example, the source code for the mini-program for MOV from class, but the mini-program for MOV had fewer instructions (MOV R2, R7 becomes @7;D=M;@2;M=D).

Test with the following program. However, before running in CPU Emulator, manually put a value in cell 0 and look for the result in cell 1 (try even and odd number):

programASR1.arm (should see in cell 1 the value from cell 0 divided by 2)

Note that the Hack Assembly code should only have the instructions of the mini-program, so this can serve as a simple test whether things were written out correctly.

ASR Instruction (Stack)

Rewrite the mini-program for ASR to use the two cells on top of the stack instead of R0,R1. Note that the mini-program is part of ASR, but it only does the computation based on what is on the stack, so getting things ready for the mini-program and any follow-up actions is done by ASR.

To simplify the code, there is no need to claim and release the two working cells on the stack, i.e. there is no need to move SP up and down. Use the first cell on top of the stack to serve the purpose of R0 and the cell on top of it for R1. (Where does SP point in our ARM convention?)

Test with the following program. Again, manually put a number in cell 5 and cell 8; should see the results in cells 3 and 9:

programASR2.arm (should see in cells 3,9 the values from cells 5,8 divided by 2)

Note that the Hack Assembly code should only have the instructions of two mini-program one after the other, so this can serve as a simple test whether things were written out correctly. Check the jumps.

Test with the following program. This time do not put numbers manually, since they are part of the program:

programASR3.arm (should see 6,4,5 in cells 3,9,10)

Final Test (program23)

Convert the following program and execute it on the Computer.

Upload a screenshot named program23.png of Hardware Simulator / CPU Emulator that shows the contents of the RAM.

program23.arm (should see 1,3,6,10,15,21 in cells 22..27)


What to turn in

Upload ArmToHack.h, ArmToHack.cpp, main.cpp, program23.png to the Moodle dropbox.