4.3.机器语言
Machine Language
1.Background knowledge of Hack Computer
Hardware
- Data memory(RAM): It is a sequence of 16-bit values.And each of these values is going to be stored in a memory register
- Instruction memory(ROM): It is a separate memory space that is also a sequence of 16 bit values.
- CPU: It is a device which is capable of manipulating 16 bit values using mostly the ALU which resides inside the CPU.
- Instruction buses, data buses, address buses: It enables us to move data from one place to another.
The Hack machine language recognizes three registers:
- D holds a 16-bit value
- A holds a 16-bit value
- M represents the 16-bit RAM register addressed by A
Language
The Hack machine language includes:
- 16-bit A-instructions
- 16-bit C-instructions
Register
The hack computer recognizes three registers:
- D register: It holds a 16-bit data which represent a piece of data.
- A register: It holds a 16-bit data which represent either a data value or an address.
- M register. It is called selected memory register.
2.Syntax
A
instruction
Syntax:
@value
, value is either:- a non-negative decimal constant or
- a symbol referring to such a constant
Semantics:
- Sets the A register to value
- Side effect: RAM[A]becomes the selected RAM register.
We always need A instruction before we operate on the memory.
1 | //set RAM[100] to -1 |
syntax:`dest=comp; jump.
comp
contains:0
,1
,-1
,D
,A
,-D
,!A
,D-A
,D+1
,D-M
, etc.dest
: We have 8 possible destination:null
,D
,M
,MD
,A
,AM
,AD
,AMD
(M
refers to RAM)null
means we don't want to to store the results of a computation at all.- We can store the result in RAM register or D-register and simultaneously both in M and in D. That is, we can simultaneously in more than one containers, and the programmer is free to use any he wants.
jump
:null
,JGT
,JEQ
,JGE
,JLT
,JNE
,JLE
,JMP
. They always compare the result of the computation to 0. Andif (comp jump 0)
jumps to execute the instruction in RAM[A].
- If we want to do unconditional jump, we do
0; JMP
.
Semantics:
- Compute the value of comp
- Stores the result in dest:
- If the Boolean expression (comp jump B)is true,
- jumps to execute the instruction stored in ROM[A].
1 | //if (D-1==0) jump to execute the instruction stored in ROM[56] |