Machine Language
Where hardware and software meet.
Why Study Machine Language?
- A computer can be understood through its hardware construction or through the instructions it can execute.
- Low-level programs show both how to control a computer and why its hardware is designed as it is.
- Machine language is the interface where symbolic instructions become physical operations in hardware.
- It prepares us to build, in the next chapter, a computer that executes programs using the chips from Chapters 1–3.
Design goal: direct execution on—and precise control of—a particular hardware platform.
Next: identify the hardware abstractions visible to a programmer.
The Three Main Abstractions
A machine language is an agreed-upon formalism for manipulating memory using a processor and a set of registers.
- Stores data and instructions.
- Each fixed-width word has a unique address.
Memory[address],RAM[address], andM[address]denote a selected word.
- Performs arithmetic, logic, memory-access, and control operations.
- Reads operands from registers or selected memory locations.
- Stores results in registers or memory.
- Each register holds one value close to the processor.
- Access is faster and instructions can be shorter than memory access.
- Using registers reduces costly memory operations.
Next: see how operations are coded as instructions.
A Program Is a Series of Coded Instructions
Consider a possible instruction for a 16-bit computer:
1010 0011 0001 1001For example, addition.
Register R3.
Register R1.
Register R9.
The meaning depends entirely on the hardware platform's instruction set. Under one possible specification, this could mean R3 ← R1 + R9.
From Binary Codes to Assembly Language
1010001100011001Directly represented as bits, but difficult for people to read and write.
ADD R3,R1,R9Uses mnemonics: short symbolic names that suggest the operation or hardware element.
Different computers have different syntax, registers, and operations. Nevertheless, their machine languages support similar kinds of commands.
Next: study the common command families.
Arithmetic and Logic Operations
Every processor provides a fixed collection of arithmetic and Boolean operations.
ADD R2,R1,R3
// R2 ← R1 + R3
ADD R2,R1,foo
// R2 ← R1 + Memory[foo]
AND R1,R1,R2
// R1 ← R1 AND R2
- Operands may come from registers or selected memory locations.
- Results may be written to a register or a selected memory location.
- Typical operations include addition, subtraction, bitwise negation, AND, and shifting.
Memory Access and Flow of Control
- Load: memory to register.
- Store: register to memory.
- Three common modes are direct, immediate, and indirect addressing.
- Unconditional jump: always go to the named location.
JMP beginWhile - Conditional jump: jump only when a Boolean condition holds.Jump to
JNG R1,endWhileendWhilewhen the conditionR1 < 0is satisfied.
Next: compare the principal ways of specifying a memory address.
Identify the Kind of Command
For each command, decide whether its main purpose is arithmetic/logic, memory access, or flow of control.
ADD R2,R1,R3
LOAD R1,67
JMP beginWhile
JNG R1,endWhile
Direct and Immediate Addressing
LOAD R1,67
// R1 ← Memory[67]
LOAD R1,bar
// if bar denotes address 67,
// R1 ← Memory[67]LOADI R1,67
// R1 ← 67The number 67 is treated as a constant, not as an address.
Indirect Addressing
The instruction identifies a register or memory location that holds the required address.
LOAD* R2,R1 // R2 ← Memory[R1]
Indirect addressing is essential for pointers and arrays, whose target addresses are calculated while the program runs.
Next: translate one array access step by step.
Worked Example: Accessing an Array Element
Assume each array element occupies one word. Then foo[j] is stored at address foo + j.
x = foo[j]
// equivalently in C notation:
x = *(foo + j)ADD R1,foo,j
// R1 ← foo + j
LOAD* R2,R1
// R2 ← Memory[R1]
STR R2,x
// x ← R2The first instruction computes an address; the second follows that address; the third stores the retrieved value.
Next: specialize these ideas to the Hack computer.
Identify the Addressing Mode
Name the addressing mode used in each case.
LOAD R1,67
LOADI R1,67
LOAD* R2,R1
After computing foo+j, which mode retrieves foo[j]?
Memory Address Spaces
- Hack is a 16-bit computer with a CPU, instruction memory, data memory, screen, and keyboard.
- The screen and keyboard are memory-mapped input/output devices.
- Instruction memory and data memory are separate address spaces.
Contains the program executed by the CPU. Programs are loaded from outside the running computer.
Contains program data and the memory maps used to communicate with the screen and keyboard.
A 15-bit address selects one of 2^15 = 32K words in either address space.
The A and D Registers
Used to hold and manipulate data values.
Assembly examplesD=!A
D=D+1Its contents may be interpreted as a value, a data-memory address, or an instruction-memory address.
Assembly exampleA=D-1
D=MWhen A contains a data-memory address, M denotes the selected word RAM[A]. Thus, the valid assembly instruction D=M copies that word into D. M is not a third register.
A Hack instruction cannot contain both a full operation code and a 15-bit address. One instruction sets A; the next uses the address stored in A.
The Special Command @value
@value stores the specified number in the A register. The value may be written as a number or as a symbol that represents a number.
@17Stores 17 in the A register.
@sumIf sum denotes address 17, this also stores 17 in A.
Therefore, @17 and @sum have the same effect when sum refers to 17.
Next: use A to select a data-memory word.
Using A to Select Data Memory
Recall: M denotes RAM[A], the data-memory word whose address is currently stored in A. It is not a separate register.
First read the two-line assembly program as a single unit.
@516
D=M-1
@516Store 516 in the A register. This selects data-memory address 516.
D=M-1After line 1, A=516, so M denotes RAM[516]. Therefore, compute D=Memory[516]-1.
Writing an Unconditional Goto
To continue execution at instruction address 35, use two assembly instructions:
@35
0;JMP
@35Store 35 in A. The A register now contains the destination address.
0;JMPJMP means jump unconditionally to the instruction address in A. Nothing is stored.
A Hack compute-and-jump instruction contains a computation before the semicolon. Here its result is discarded, and JMP does not test it. Any legal computation—such as 1, D, or A—could replace 0. We conventionally write 0;JMP because 0 is simple and does not depend on stored data.
Next lecture: define A- and C-instructions precisely.
Lecture 12: Hack Machine Language
Instructions, symbols, and memory-mapped input/output.