Comp 255 - Computer Organization Lab 07 The HACK Assembly Language February 26, 2013 Name: Grade: /10 Begin by opening the two ECS applications Assembler.bat and CPUEmulator.bat; the former will be used to assemble HACK assembly language programs (.asm files) into HACK machine code (.hack files) to be executed by the CPUEmulator application. 1a. A simple addition program A. Using NotePad create the following file. Call it Lab09a.asm // // Name: // File: Lab09a.asm // Date: February 26, 2013 // Desc: A simple addition program for the HACK computer // @R1 (END) Notes: D=M // load R1 @R2 D=D+M // R1 + R2 @R3 M=D // store in R3 // label @END 0;JMP // Infinite loop a. R1, R2, and R3 refer to memory locations 1 3 (RAM[1] RAM[3]). b. Since there is no halt instruction in Hack assembly language the last two instructions sends the program into an infinite loop B. Assemble this source code file into an object code file called Lab09a.hack C. Load Lab09a.hack into the CPUEmulator. Initialize memory locations 1 and 2 to 2 and 3 respectively Run (Step) the code. If successful you should see the value 5 appear at address 3. D. Print out your Lab07a.asm file and your Lab07a.Hack file to be handed in. Lab07-1
1b Run the same program using a.tst file: When done this way the Assembler is automatically invoked by the.tst script which also initializes RAM locations (variables). Since compare and output files are not used their corresponding lines have been commented out. D. Using NotePad create following.tst file. load lab07a.asm, //output-file lab07a.out, //compare-to lab07a.cmp, //output-list RAM[3]%D2.6.2; Set PC 0; set RAM[1] 2; set RAM[2] 3; //output E. Run this.tst script from the CPUEmulator application 1.c Optional Version: The alternate version of Lab09a.asm below allocated three variables a, b, and c (instead of R1, R2, an R3). The assembler would allocate locations RAM[16], RAM[17] and RAM[18] respectively for each. The.tst file is altered to initialize a and b. Name the source code file Lab09a1.asm... // Desc: A simple addition program // (END) D=M // load a @b D=D+M // a + b @c M=D // store in c // label @END 0;JMP // Infinite loop The corresponding alternate.tst file load lab09a.asm, //output-file lab09a.out, //compare-to lab09a.cmp, //output-list RAM[18]%D2.6.2; set PC 0; set RAM[16] 2; set RAM[17] 3; //output Lab07-2
2. A simple subtraction program: Write and execute a Hack assembly language program to subtract R2 from R1 putting the difference in R3. Call it Lab09b.asm Modify Lab07a.tst to test Lab07b.asm. Call it Lab07b.tst Run the Lab07b.tst script in the CPUEmulator to execute Lab07b.asm. When it successfully runs print out the source code file Verification #2: 3. Unconditional and Conditional Branching Unconditional branching is done by setting A to the target address (a label) followed by 0; JMP @label 0; JMP // jump always Conditional branching can be done by testing the contents of the D register and jumping if the condition is met (in a sense this is similar to how the PDP-8 does it) @target D;JEQ // jump to target if D = 0 Write and test a simple Hack assembly language program that given two registers R1 and R2 subtracts the smaller from the larger putting the result in R3. Call it Lab09c.asm. Test your program using the following.tst code. Note that this.tst file generates an output file (Lab09c.out) which will allow you to check your results. (Note that the CPUEmulator can display an output file) load lab07c.asm, output-file lab07c.out, //compare-to lab07c.cmp, output-list RAM[3]%D2.6.2; Set PC 0; set RAM[1] 2; set RAM[2] 7; output; set PC 0; set RAM[1] 5; set RAM[2] 3; output; Lab07-3
Run the Lab07c.tst script in the CPUEmulator to execute Lab07c.asm. When it successfully runs print out the source code file Verification #3: 4 Loops: Write a program to sum the integers between K and N inclusive (you may assume k < n). Use R1 to hold K, R2 to hold N and put the SUM R3. Allocate other variables or registers as needed. Probably the easiest way to program this is to a. allocate a loop counter variable I b initialize I to K c. add I to SUM d. add 1 to I e subtract I from N f test: tf you store the results in register D then the conditional branch instruction @StepC D; JGE should work. Call your program Lab07d.asm Create an appropriate Lab09d.tst script and run it in the CPUEmulator to execute Lab09d.asm. When it successfully runs print out the source code file Verification #4: Hand In: Source code files Lab07a.asm, Lab07b.asm, Lab07c.asm, Lab07d.asm (Lab07a1.asm was optional) Test file Lab07d.tst Lab07-4
An Overview of Hack Assembly Language The Hack instruction set has two instruction format: a A-instruction which loads the A register with an immediate value and a C-instruction which is a combined compute and optionally branch instruction. Most instructions are C-instructions A-instruction: set contents of Address Register @value // A value C-instruction: Compute & (optionally) Branch dest = comp ; jump // comp is required where comp is one of 0, 1, -1, D, A,!D,!A, -D, -A, D+1, A+1, D-1, A-1 M,!M, -M, M+1, M-1 D+A, D-A, A-D, D&A, D A D+M, D-M, M-D, D&M, D M Note M refers to Mem[A] dest is one of jump is one of null, M, D, MD, A, AM, AD, AMD null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP where A holds target address for the jump if taken; The condition is determined by a results of the comp operation. Directives & Conventions (label) // label Predefined Symbols: R0 thru R15 are RAM locations 0 thru 15 The assembler allocates storage for variables beginning at RAM location 16; the programmer has no control over storage allocation. Storage is allocated as each variable is encountered. Lab07-5
Examples and Techniques To set D to a constant value @17 // A = 17 D = A // D = A Accessing Memory: Load D from a D = M // D = a Accessing Memory: Store D to a M = D // a = D Accessing Memory: Load R0 from a D = M @R0 M = D // D = a // get address R0 // R0 = D Accessing Memory: Store R0 to a @R0 D = M M = D // get address R0 // D = R0 // a = D Unconditional Jump @END // get address END (a label) 0; JMP // jump always to END Conditional Jump branch to L1 on D = 0 To initialize a variable @L1 // get address L1 (a label) D; JEQ // jump if D == 0 to L1 @17 // A = 17 D = A // D = A M = D // a = D Lab07-6