• No results found

Comp Computer Organization Lab 07 The HACK Assembly Language

N/A
N/A
Protected

Academic year: 2021

Share "Comp Computer Organization Lab 07 The HACK Assembly Language"

Copied!
6
0
0

Loading.... (view fulltext now)

Full text

(1)

Lab07-1

Comp 255 - Computer Organization Name: _____________________________ Lab 07 – The HACK Assembly Language

February 26, 2013 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 D=M // load R1 @R2 D=D+M // R1 + R2 @R3 M=D // store in R3 (END) // label @END 0;JMP // Infinite loop Notes:

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.

(2)

Lab07-2

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; repeat 20 { ticktock; } //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 // @a D=M // load a @b D=D+M // a + b @c M=D // store in c (END) // 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; repeat 20 { ticktock; } //output

(3)

Lab07-3

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; repeat 20 { ticktock; } output; set PC 0; set RAM[1] 5; set RAM[2] 3; repeat 20 { ticktock; } output;

(4)

Lab07-4

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)

(5)

Lab07-5

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

null, M, D, MD, A, AM, AD, AMD jump is one of

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.

(6)

Lab07-6

Examples and Techniques

To set D to a constant value

@17 // A = 17

D = A // D = A Accessing Memory: Load D from a

@a // get address a

D = M // D = a Accessing Memory: Store D to a

@a // get address a

M = D // a = D Accessing Memory: Load R0 from a

@a // get address a

D = M // D = a

@R0 // get address R0 M = D // R0 = D

Accessing Memory: Store R0 to a

@R0 // get address R0 D = M // D = R0

@a // get address a

M = D // a = D Unconditional Jump

@END // get address END (a label) 0; JMP // jump always to END

Conditional Jump – branch to L1 on D = 0

@L1 // get address L1 (a label) D; JEQ // jump if D == 0 to L1 To initialize a variable @17 // A = 17 D = A // D = A @a // get address a M = D // a = D

References

Related documents

This suggests that personal knowledge is an important factor in food-media literacy, but more work must be done to determine whether these critical thinking skills can be

Before I move on to investigate, if the spatial patterns of precolonial state- hood nonetheless continue to shape institutional choices in conflict resolu- tion, I draw on

All compounds scoring better than 1 standard deviation from the mean were tested in extra precision (XP), and the top 10% of XP ligands were put through flexible XP docking,

So, barometric pressure sensor detects low pressure and send the data to PCB circuit board (PR22) and it send data to LCD display as shown in Figure 10.. This LCD display acts as

In this study, we take ad- vantage of the Zero-Determinant (ZD) strategy to analyze the block withholding attack between any two pools, where the ZD adopter has the unilateral

The UK Market Unit comprises private health insurance, wellbeing services, care homes, out of hospital healthcare services and a complex care hospital in London.. In 2012,

For example, the reconsbuction of a distinctive Basran hostility lo wril- iDg ;n the second half ot the second century is a historical hpothes;s that mal€s sense of

By far the majority of borrowers fall into the normal repayment cycle; 88.7% of the loan book balance relates to loans that are being repaid when the borrower earns above the