N15 – HACK Machine Language
Credit: Jiayao (Amy) Li ’18 for typewriting my handwritten notes
Let’s have a quick refresher of the HACK computer from the perspective of its machine language and high-level block diagram of its architecture.
HACK MACHINE ARCHITECTURE
HACK is a 16-bit computer based on HARVARD architecture with split data and program memory as shown below –
Hardware: The data memory is also referred to as RAM and is a sequence of 16-bit registers e.g. RAM[0], RAM[1], etc. The program or instruction memory is also referred to as ROM and is a sequence of 16-bit registers e.g. ROM[0], ROM[1], etc. The CPU performs 16-bit instructions read from the ROM, one at a time. More on this when we build our HACK computer. The CPU, ROM, RAM exchange data/instructions on 16-bit buses.
Software: The software view is realized via a machine language with 2 types of instructions – A and C instruction representing the theme of address (A) and computational (C) respectively. HACK program is nothing but a sequence of A and C instructions.
Control: The instruction memory, ROM is loaded with HACK program and then Reset is asserted leading to program execution from the first line of code.
Registers: HACK programming paradigm has three designated registers –
1. A-register: One register designated as A-register that is used to store data or address
memory
2. D-register: One register designated as D-register that is used to store data.
3. M: Several registers that combine to form the RAM, collectively noted as M and
individually referenced through the contents of A register (that holds the pointer to specific location in the RAM).
HACK MACHINE LANGUAGE
HACK has two instructions in its repertoire:
(1) A-Instruction with mnemonic
– Example: @27 – Bit format:
– A-instruction is named so largely because it serves the purpose of holding address of memory location in the A-register, although it can also hold data for typical arithmetic & logical computation.
@
0 n14 n13……n1 n0
→ Notice the leading 0 followed by the number
→ Also note that we can represent a number as low as 0 and as high as 215-1
(2) C-Instruction with mnemonics like (eg.) D=A+D
Or M=D or D; JLE etc. Note that the complete reference of HACK C instruction can be found in the textbook and lecture slides for Machine Language.
Generically, C instruction can be written as: DEST=COMP; JUMP
Where DEST represents the destination of computation COMP COMP is the computation performed by the instruction
JUMP represents the class of JUMP instructions in the broad category of flow control operations
→ Note: DEST and JUMP fields are optional but NOT BOTH Examples
→ A=A+D shows DEST is A register &
COMP is A+D, the sum of contents of A&D register
→ D; JLE shows there is no specified destination of this instruction and that the operation is to test the value of D and Jump to location pointed by A register if D≤0
A C-Instruction bit pattern categories are as shown:
← COMP → A–Instruction bit pattern is only one type as shown:
1××ac5c4c3c2c1c0d2d1d0j2j1j0
0n14 n13……n1 n0
C-Instruction
C- Instructio n
A-Instruction
On your right is a very high-level block diagram of the HACK compute (sans the I/0).
-The Instruction memory holds instructions.
Which instruction will be executed next is determined by the address loaded in the program counter, PC
-The Data Memory holds data that can be read by or written to the CPU
-CPU has two registers A&D
-Width of all memory blocks and the registers is 16-bits
Hack computer has two registers –A&D. The D Register only serves the purpose of holding data. In the event, the D register is holding a computer memory address, its contents must first be moved to the A register before they can be used as memory address (example 4 below)
Example1:
@9
D=A loads the number 9 into register D
Example2: D=D+A updates D with sum of A and D content
Example3:
@9
D=M loads contents of memory M[9] into register D
Example4:
A=D
M=M+1 implements the function M[A]=M[A]+1
The A Register, on the other hand, can serve multiple purposes.
(a) The A register can hold data (see Example 1 above)
(b) The A register acts as pointer to data memory location (see Example 3 and 4 above) (c) The A register also acts as ROM ptr to implementflow control
Example5:
@9
JUMP Program control transfer unconditionally to instruction at Memory (ROM) location 9
Example6:
@127
D; JLE
This results in transfer of program control to location 127 in instruction Memory if content of D register is ≤0
A Register D Register
HACK computer @High Level
Input/Output
In HACK, the communication with input and output devices is managed through memory (RAM) mapping. Such mapping is known as memory mapped IO. The details are going to be left to a OS centric discussion in a future CS course, but it suffices to state that both the input and output devices are mapped to regions in the RAM.
HACK has one input device source in the form of Keyboard and one output device in the form of Display. One 16-bit register location M[24576] is dedicated to the keyboard. When no key is pressed, the contents of this memory location is to be expected 0. To check if a key is pressed, we just have to probe the contents of register M[24576] to see which key is pressed.
HACK has one output device in the form of Display. The mapping of the display in the RAM is as follows: Display is modeled as a X x Y checkerboard with total 32 columns and 256 rows. The region of memory dedicated to the display starts at location M[16384] and ends at location M[24575]. Each column is 16 bits thereby modeling the display as 16×32 = 512 bits for each row. With this organization, one can picture 32 registers per row. In that respect each bit in the display region corresponds to a pixel.