Lecture 11 | Machine Language

Machine Language

Where hardware and software meet.

Machine Language

Why Study Machine Language?

Design goal: direct execution on—and precise control of—a particular hardware platform.

Next: identify the hardware abstractions visible to a programmer.

Machine Language

The Three Main Abstractions

A machine language is an agreed-upon formalism for manipulating memory using a processor and a set of registers.

MemoryAn addressed array of words
  • Stores data and instructions.
  • Each fixed-width word has a unique address.
  • Memory[address], RAM[address], and M[address] denote a selected word.
ProcessorCentral Processing Unit (CPU)
  • Performs arithmetic, logic, memory-access, and control operations.
  • Reads operands from registers or selected memory locations.
  • Stores results in registers or memory.
RegistersSmall, fast local 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.

Machine Language

A Program Is a Series of Coded Instructions

Consider a possible instruction for a 16-bit computer:

1010 0011 0001 1001
1010operation

For example, addition.

0011operand

Register R3.

0001operand

Register R1.

1001operand

Register R9.

The meaning depends entirely on the hardware platform's instruction set. Under one possible specification, this could mean R3 ← R1 + R9.

Machine Language

From Binary Codes to Assembly Language

Binary instruction1010001100011001

Directly represented as bits, but difficult for people to read and write.

Symbolic instructionADD R3,R1,R9

Uses mnemonics: short symbolic names that suggest the operation or hardware element.

assembly languageassemblerbinary machine instructions

Different computers have different syntax, registers, and operations. Nevertheless, their machine languages support similar kinds of commands.

Next: study the common command families.

Machine-Language Commands

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.
Machine-Language Commands

Memory Access and Flow of Control

Memory accessMove or operate on stored values
  • Load: memory to register.
  • Store: register to memory.
  • Three common modes are direct, immediate, and indirect addressing.
Flow of controlChoose the next instruction
  • Unconditional jump: always go to the named location.
    JMP beginWhile
  • Conditional jump: jump only when a Boolean condition holds.
    JNG R1,endWhile
    Jump to endWhile when the condition R1 < 0 is satisfied.

Next: compare the principal ways of specifying a memory address.

Exercise 1

Identify the Kind of Command

For each command, decide whether its main purpose is arithmetic/logic, memory access, or flow of control.

A

ADD R2,R1,R3

Arithmetic
B

LOAD R1,67

Memory access
C

JMP beginWhile

Flow of control
D

JNG R1,endWhile

Flow of control
Memory Access

Direct and Immediate Addressing

Direct addressingThe instruction names a memory address
LOAD R1,67
// R1 ← Memory[67]

LOAD R1,bar
// if bar denotes address 67,
// R1 ← Memory[67]
Immediate addressingThe instruction contains the value itself
LOADI R1,67
// R1 ← 67

The number 67 is treated as a constant, not as an address.

Memory Access

Indirect Addressing

The instruction identifies a register or memory location that holds the required address.

1R1 contains 67
2Use R1 as an address
3Read Memory[67]
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.

Memory Access

Worked Example: Accessing an Array Element

Assume each array element occupies one word. Then foo[j] is stored at address foo + j.

High-level view
x = foo[j]

// equivalently in C notation:
x = *(foo + j)
Possible machine-language translation
ADD R1,foo,j
// R1 ← foo + j

LOAD* R2,R1
// R2 ← Memory[R1]

STR R2,x
// x ← R2

The first instruction computes an address; the second follows that address; the third stores the retrieved value.

Next: specialize these ideas to the Hack computer.

Exercise 2

Identify the Addressing Mode

Name the addressing mode used in each case.

A

LOAD R1,67

Direct addressing
B

LOADI R1,67

Immediate addressing
C

LOAD* R2,R1

Indirect addressing
D

After computing foo+j, which mode retrieves foo[j]?

Indirect addressing
The Hack Computer: Overview

Memory Address Spaces

Instruction memory32K words × 16 bits

Contains the program executed by the CPU. Programs are loaded from outside the running computer.

Data memory32K words × 16 bits

Contains program data and the memory maps used to communicate with the screen and keyboard.

Address width15 bits

A 15-bit address selects one of 2^15 = 32K words in either address space.

The Hack Computer: Overview

The A and D Registers

D registerA 16-bit data register

Used to hold and manipulate data values.

Assembly examples
D=!A
D=D+1
A registerData and address register

Its contents may be interpreted as a value, a data-memory address, or an instruction-memory address.

Assembly example
A=D-1
D=M
Using A to address data memory

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

Why does A have several roles?

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.

Hack Machine Language Specification

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.

Numeric value@17

Stores 17 in the A register.

Symbolic value@sum

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

Hack Machine Language Specification

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
Line 1@516

Store 516 in the A register. This selects data-memory address 516.

Line 2D=M-1

After line 1, A=516, so M denotes RAM[516]. Therefore, compute D=Memory[516]-1.

Hack Machine Language Specification

Writing an Unconditional Goto

To continue execution at instruction address 35, use two assembly instructions:

@35
0;JMP
Line 1@35

Store 35 in A. The A register now contains the destination address.

Line 20;JMP

JMP means jump unconditionally to the instruction address in A. Nothing is stored.

Why is the computation 0?

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.

Next Lecture | Hack Machine Language

Lecture 12: Hack Machine Language

Instructions, symbols, and memory-mapped input/output.