Low Level Programming Taste Project

Objective Get a taste of low-level programming in machine language, and get acquainted with the Hack computer platform. In the process of working on this project, you will also become familiar with the assembly process, and you will ap- preciate visually how the translated binary code executes on the target hardware.

Resources In this project you will use two tools supplied with the book: An assem- bler, designed to translate Hack assembly programs into binary code, and a CPU emulator, designed to run binary programs on a simulated Hack platform.

Contract Write and test the two programs described in what follows. When exe- cuted on the CPU emulator, your programs should generate the results mandated by the test scripts supplied in the project directory.

  • Multiplication Program (Mult.asm): The inputs of this program are the current values stored in R0 and R1 (i.e., the two top RAM locations). The program computes the product R0R1 and stores the result in R2. We assume (in this program) that R0>=0, R1>=0, and R0R1<32768. Your program need not test these conditions, but rather assume that they hold. The supplied Mult.tst and Mult.cmp scripts will test your program on several representative data values.
  • I/O-Handling Program (Fill.asm): This program runs an infinite loop that listens to the keyboard input. When a key is pressed (any key), the program blackens the screen, namely, writes ‘‘black’’ in every pixel. When no key is pressed, the screen should be cleared. You may choose to blacken and clear the screen in any spatial order, as long as pressing a key continuously for long enough will result in a fully blackened screen and not pressing any key for long enough will result in a cleared screen. This program has a test script (Fill.tst) but no compare file—it should be checked by visibly inspecting the simulated screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Mult.asm

// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)

// Put your code here.


//m * n = m + m + m + ... + m


// @sum
// M=0
// @i
// M=1

// (LOOP)
// @i
// D=M
// @R0
// D=M-D
// @END
// D;JGT
// @R1
// sum=sum+M
// @i
// M=M+1
// @LOOP
// 0;JMP
// (END)
// @sum
// D=M
// @R2
// M=D

@sum
M=0 //sum=0
@i //i=1
M=1
(LOOP)
@i //D=i
D=M
@R0
D=D-M //D=I-R0
@END
D;JGT // if(i-R0) > 0 goto END
@R1
D=M //D=R1
@sum
M=D+M
@i
M=M+1
@LOOP
0;JMP //Goto LOOP
(END)
@sum
D=M
@R2
M=D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Fill.asm

// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel;
// the screen should remain fully black as long as the key is pressed.
// When no key is pressed, the program clears the screen, i.e. writes
// "white" in every pixel;
// the screen should remain fully clear as long as no key is pressed.

// Put your code here.

@status
M=-1 // status=0xFFFF
D=0 // Argument - what to set screen bits to
@SETSCREEN
0;JMP

(LOOP)
@KBD
D=M // D = current keyboard character
@SETSCREEN
D;JEQ // If no key, set screen to zeroes (white)
D=-1 // If key pressed, set screen to all 1 bits (black)

(SETSCREEN) // Set D=new status before jumping here
@ARG
M=D // Save new status arg
@status // FFFF=black, 0=white - status of entire screen
D=D-M // D=newstatus-status
@LOOP
D;JEQ // Do nothing if new status == old status

@ARG
D=M
@status
M=D // status = ARG

@SCREEN
D=A // D=Screen address
@8192
D=D+A // D=Byte just past last screen address
@i
M=D // i=SCREEN address

(SETLOOP)
@i
D=M-1
M=D // i=i-1
@LOOP
D;JLT // if i<0 goto LOOP

@status
D=M // D=status
@i
A=M // Indirect
M=D // M[current screen address]=status
@SETLOOP
0;JMP
Donate article here
Share the post