4.3.Machine Language

1.Background knowledge of Hack Computer

  a.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

  b.Language

  • The Hack machine language includes:

    • 16-bit A-instructions
    • 16-bit C-instructions

  c.Register

  The hack computer recognizes three registers:

  1. D register: It holds a 16-bit data which represent a piece of data.
  2. A register: It holds a 16-bit data which represent either a data value or an address.
  3. M register. It is called selected memory register.

2.Syntax

  a.A instruction

  • Syntax: @value, value is either:

    • a non-negative decimal constant or
    • a symbol referring to such a constant
  • Semantics:

    1. Sets the A register to value
    2. Side effect: RAM[A]becomes the selected RAM register.
  • We always need A instruction before we operate on the memory.

  e.g.

1
2
3
//set RAM[100] to -1
@100 //A = 100
M = -1 //RAM[100] = -1, M denotes register number 100 in the memory unit
####   b.C instruction

  • 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. And if (comp jump 0) jumps to execute the instruction in RAM[A].
    • If we want to do unconditional jump, we do 0; JMP.
  • Semantics:

    1. Compute the value of comp
    2. Stores the result in dest:
    3. If the Boolean expression (comp jump B)is true,
    4. jumps to execute the instruction stored in ROM[A].

  e.g.

1
2
3
//if (D-1==0) jump to execute the instruction stored in ROM[56]
@56
D-1; JEQ