Chapter 4
Hack Machine Language
The hack computer is a 16-bit machine,consisting of a CPU,two separate memory modules serving as instuction memory and data memory,and two memory-mapped I/O devices:a screen and a keyboard.
Memory Address Spaces:
instruction memory:16-bit wide
data memory:16-bit wide
address space:15-bit
Registers:
D register:only can be used to store data values
A register:can be interpreted either as a data value or as an address in the data memory,or as an address in the instruction memory.
M register:refer to the memory word whose address is the current value of the A register.(M stands for Memory[A])
Example:
@value
:stores the specified value in the A register.
For example,if I set @21,that means the effect is that setting the A register to 21,and also RAM[21]becomes the selected RAM register.
Actually,the Hack language consists of two generic instructions:an address instruction,also called A-instruction,and a compute instruction,also called C-instruction.Each instruction has a binary representation.
Typical operation:
// D = 10
@10
D = A
// D++
D = D + 1
// D = RAM[17]
@17
D = M
//RAM[17] = D
@17
M = D
//RAM[17] = 10
@10
D = A
@17
M = D
//RAM[5] = RAM[3]
@3
D = M
@5
M = D
the A-Instruction
@value //where value is either a non-negative decimal number or a symbol referring to sum number.
the C-Instruction
dest = comp;jump //Either the dest or jump fields may be empty.If dest is empty,the "=" is ommitted.If jump is empty,the ";"is omitted.
the leftmost bit is the C-instruction code,which is 1.The next two bits are not used.The remaining bits form three fields that correspond to the three parts of the instruction's symbolic representation.
comp:what to compute
dest:where to store the computed value(ALU output)
jump:specify a jump condition,namely,which command to fetch and execute next
Now we can use Hack Language to multiply two numbers and then store them in a specific register.
Input/Output Handling
Screen :the screen's contents are starts at RAM address 16384(0x4000). The corresponding bit in the RAM-resident memory map:1 = black,0 = white.
To set pixel(row,col)on/off:
1.word = Screen[32 row + col / 16] , word = RAM[16384 + 32 row + col / 16].
2.Set the(col%16)th bit of word to 0 or 1.
3.Commit word to the RAM.
Keyboard:The interfaces between computer and keyboard are located in RAM address 24576(0x6000).Whenever a key is pressed on the physical keyboard,its 16-bit ASCII code appears in RAM[24576].When no key is pressed,the code 0 appears in this location.In the addition to the usual ASCII codes,the Hack keyboard recognizes the keys shown in the following picture.
Output
Screen memory map A designated memory area,dedicated to manage a display unit. The physical display is continuously refreshed from the memory map,many times per second. Output is effected by writing code that manipulates the screen memory map.
Housework:
1.Mult.asm
Before we write down the Machine Code , we should think about how to achieve this goal by pseudo code.so the following is the pseudo code:
a = R0
b = R1
R2 = 0
while(a > 0){
R2 = R2 + b
a --
}
code:
@R0
D=M
@a //this step can prevent the value of R0 doesn't change after the program
M=D // a = R0
@R1
D=M
@b //this step can prevent the value of R1 doesn't change after the program
M=D // b = R1
@0
D=A
@R2
M=D // R2 = 0,reset the sum
(LOOP)
// begin of loop condition
@a
D=M // load R0 for loop condition
@END
D;JLE // if a <= 0 goto END
// end of loop condition
// begin body of while
@b
D=M // D = b
@R2
M=D+M // sum = sum + b
@1
D=A // D = 1
@a
M=M-D // a = a - 1
// end body of while
@LOOP
0;JMP // jump to loop start
(END)
@END
0;JMP // Infinite loop (program end)
2.fill.asm
pseudo code:
if(KBD > 0) goto BLACK
else goto WHITE
(RESTART)
@SCREEN
D=A
@0
M=D //PUT SCREEN START LOCATION IN RAM0
///////////////////////////
(KBDCHECK)
@KBD
D=M
@BLACK
D;JGT //JUMP IF ANY KBD KEYS ARE PRESSED
@WHITE
D;JEQ //ELSE JUMP TO WHITEN
@KBDCHECK
0;JMP
///////////////////////////
(BLACK)
@1
M=-1 //WHAT TO FILL SCREEN WITH (-1=11111111111111)
@CHANGE
0;JMP
(WHITE)
@1
M=0 //WHAT TO FILL SCREEN WITH
@CHANGE
0;JMP
//////////////////////////
(CHANGE)
@1 //CHECK WHAT TO FILL SCREEN WITH
D=M //D CONTAINS BLACK OR WHITE
@0
A=M //GET ADRESS OF SCREEN PIXEL TO FILL
M=D //FILL IT
@0
D=M+1 //INC TO NEXT PIXEL
@KBD
D=A-D //KBD-SCREEN=A
@0
M=M+1 //INC TO NEXT PIXEL
A=M
@CHANGE
D;JGT //IF A=0 EXIT AS THE WHOLE SCREEN IS BLACK
/////////////////////////
@RESTART
0;JMP