程序代写代做代考 C assembly FTP CL16: Writing Basic HACK Programs

CL16: Writing Basic HACK Programs
1

Key Mentions on HACK Machine Language
A & C instructions Registers and Memory
Alustruction
Syntax nonneg
G 21 HA 21
p 11A valueofP
C
Register2Mein
Instruction
Syntax dest op jump
3
control g useforflow
T
2
P optioned T top
ofD l andJump 0 yD U
DM dest D
Dtl
D Dtl Mla
2 destDIop
3 17 iJ coronparedtjtumpif
DD
T butnotboth
2111A
21 ARomGDifD1
DlE
2
A
address data il 9 use as deta
y g use as addrers
21 11 A 21 D M HD_MLA
21
D l JEANcheckthevalue to
EEMJ
a
DMED 62

Hack Programming – ADD RAM Values
Assembly program
// Add RAM[0] and RAM[1] and put result in RAM[1]
mAIo o mc
I
Dis iii
After
120 O
II
0 HA0 Ism 11D ME
RO D M Rl
m.FI
MD D1M
I
3

Hack Programming – Swap RAM Values (use of variables) Assembly program
conceptualize
IItem
μ
D MG
M D
Dik
// flips the values of RAM[0] and RAM[1]
RO DIgmgflYjf9ggggtFeIMempM
O 11A 0 D11MfgggsJ.D MCD
M D Rl
I 11A I oMffEom
DHM 999g
Eko
temp DM
4

HACK ASM Programming Warm Up Exercises
RAM
sum = p + q + 2 HE u oEhiniiiIniian.s c
Sum mD
3 or HA q A 6g 4DDtM11DDtMEA D1Mlbs
S1 2
D DtA
If (D != 5) jumps to execute the instruction stored in ROM[56]
so
tobeexeaded igfDyfffg4aEsEFakEED
sD.J
NE
5

Handling Pointers
RAM[x] = 6
Iii
In
6

Hack Programming – Array
a index
X[j] = p
conceptual
variable
TipI away index
akin etamine Ipa a t.io em 3 getthevalueofp in D
i
4 retriedthecalculatedaddvldestTnfTJawayXCode x11A.x
D MHDMG
FTP mInFFIdr
HEEM calculatedadder 3 DpMHDMLP
a
S M DYMCdest 7 D
AIemmPyAhasltedestadd

Hack Programming – Branching (use of Labels)
O
00
OHAdo O
// Program: Signum.asm // Computes: if RAM[0]>0
// // //
RAM[1]=1
else
RAM[1]=0
I DMHD_ME 2 811A_8
3 D Jat
S MIofetepart G 10
7 0JMP 8 1
9M
to 10
11 0 JMP
DM Food
D JLT
MlO f002
0JMP Fooda g
Mel
FOOL
Feroz 0 JMP
8

Terminating a program
Hack assembly code
Memory (ROM)
0 1 2 3
@0
D=M
@1
D=D+M
@2
M=D
􏰂 􏰂 􏰂
// Program: Add2.asm
// Computes: RAM[2] = RAM[0] + RAM[1] // Usage: put values in RAM[0], RAM[1]
@0
D=M // D = RAM[0]
@1
D=D+M // D = D + RAM[1]
@2
M=D // RAM[2] = D
04 15
2 3
6 7 8 9
11 12 13 14 15
4
5 10
credit: nand2tetris.org
9

Terminating a program
Hack assembly code
Memory (ROM)
0 1 2 3
@0
D=M
@1
D=D+M
@2
M=D
malicious code
starts here…
􏰂 􏰂 􏰂
// Program: Add2.asm
// Computes: RAM[2] = RAM[0] + RAM[1] // Usage: put values in RAM[0], RAM[1]
@0
D=M // D = RAM[0]
@1
D=D+M // D = D + RAM[1]
@2
M=D // RAM[2] = D
04 15
2 3
4 5
6 7 8 9
10 11 12 13 14 15
Resulting from some attack on the computer
credit: nand2tetris.org
10

Terminating a program
Hack assembly code
Memory (ROM)
0 1 2 3
@0
D=M
@1
D=D+M
@2
M=D
@6
2
0;JMP
􏰂 􏰂 􏰂
// Program: Add2.asm
// Computes: RAM[2] = RAM[0] + RAM[1]
// Usage: put values in RAM[0], RAM[1]
@0
D=M // D = RAM[0]
@1
D=D+M // D = D + RAM[1]
@2
M=D // RAM[2] = D
@6 0;JMP
• Jump to instruction number A (which happens to be 6)
04 15
2 3
4 5
6 7
C6 7
8
9 10 11 12 13 14 15
75 Eia
to be the is designed
last lineofthe program
• 0: syntax convention for jmp instructions Best practice:
To terminate a program safely, end it with an infinite loop.
credit: nand2tetris.org
11

Built-in symbols
The Hack assembly language features built-in symbols:
symbol value
R0 0 R1 1 R2 2
… … R15 15
Attention: Hack is case-sensitive! R5 and r5 are different symbols.
These symbols can be used to denote “virtual registers”
Example: suppose we wish to use RAM[5] to represent some variable,
say x, and we wish to let x=7 implementation: better style:
// let RAM[5] = 7
@7 D=A
@5 M=D
// let RAM[5] = 7
@7 D=A
@R5
M=D
credit: nand2tetris.org 12

Built-in symbols
The Hack assembly language features built-in symbols:
symbol
R0 R1 R2

R15
SCREEN
KBD
• R0, R1 ,…, R15
• SCREEN and KBD
value symbol value
0 SP 0
1
2

15
16384
24576
LCL 1
ARG 2 THIS 3 THAT 4
: “virtual registers”, can be used as variables : base addresses of I/O memory maps
• Remaining symbols: used in the implementation of the Hack virtual
machine
credit: nand2tetris.org 13