Professional Documents
Culture Documents
9.3 Assembly Language
9.3 Assembly Language
3 Assembly Language
9.3 Assembly Language
• Machine Language
• Assembly Language
Machine Language
For example, this instruction adds the contents of two registers, R[2] and R[5],
and records the result in another register, R[B]:
opcode instruction
ANY 16-bit (4 hex digit) value de nes a TOY instruction.
0 halt
1 add
First hex digit speci es which instruction.
2 subtract
3 bitwise and
Each instruction changes machine state in a well-de ned way. 4 bitwise xor
5 shift left
6 shift right
category opcodes implements changes 7 load address
8 load
operations 123456 data-type operations registers 9 store
A load indirect
data data moves between registers,
789AB B store indirect
movement registers and memory memory
C branch if zero
conditionals, loops, and D branch if positive
ow of control 0CDEF PC
functions E jump register
F jump and link
4
fl
fi
fi
fi
TOY reference card
5
.
1A75 as a TOY instruction? A. Add R[7] to R[5] and put the result in R[A].
8
fl
Pop quiz 2 on TOY machine instructions
9
fl
Pop quiz 3 on TOY machine instructions
10 7 C 0 A R[C] = 1010
11 7 1 0 1 R[1] = 1
12 7 2 0 1 R[2] = 1
10
Pop quiz 3 on TOY machine instructions
10 7 C 0 A R[C] = 1010
11 7 1 0 1 R[1] = 1
12 7 2 0 1 R[2] = 1
11
9.3 Assembly Language
• Machine Language
• Assembly Language
Assembly Language
As we’ve seen, reading and writing machine language in its natural form - binary
numbers - is extremely dif cult and annoying
• And likely to lead to many errors
• So we use assembly language instead.
A typical assembly language makes it easier to write programs by letting you use
• Symbolic opcodes, such as ADD and XO
• Labels for memory references, such as INDEX, ONE and AFTERLOO
• Pseudo-ops, which tell the assembler useful things, such as .DATA, .BEGIN, .END
13
fi
.
For learning purposes we are using TOY, which has a simpli ed language that provides
• 16 instructions
• 16 registers.
And to convert a program written in assembler language into machine language, we need to
use an assembler.
14
.
fi
:
First pass:
• Assembles as much of the program as possible
• Builds a symbol table for memory references.
Second pass:
• Completes assembly of the remaining instructions using the symbol table.
15
fi
fi
fi
.
16
:
HL Stop execution.
• ends progra
pseudocode end
• make sure that this is present!
machine 0 0 0 0
assembly HLT 0 0 0
ADD d s t Add value in R[D] to value in R[C] and store result in R[9].
• adds two numbers s and
pseudocode R[9] = R[D] + R[C]
• stores result in a register d
machine 1 9 D C
assembly ADD 9 D C
SUB d s t Subtract R[8] from R[3] and store the result in R[6].
• subtracts t from
pseudocode R[6] = R[3] - R[8]
• stores result in a register d
machine 2 6 3 8
assembly SUB 6 3 8 17
T
XOR useful for swapping of values Swap the contents of R[1] and R[2] using XOR.
AND is useful for comparing values and
assembly XOR 1 1 2
masking.
XOR 2 1 2
https://en.wikipedia.org/wiki/Mask_(computing) XOR 1 1 2 18
t
SHL d s t Shift R[2] to the left R[3] places and store result in R[A].
• bitwise shifts s R[t] places to the lef
pseudocode R[A] = R[2] << R[3]
• stores result in register d
machine 5 A 2 3
assembly SHL A 2 3
SHR d s t Shift R[5] to the right R[A] places and store result in R[5].
• bitwise shifts s R[t] places to the righ
pseudocode R[5] = R[5] >> R[A]
• stores result in register d
machine 6 5 A 5
assembly SHR 5 A 5
19
t
Load and store allow us to move data between main memory and the CPU, by
copying the data to and from registers.
21
Load and Store with Indirection
The regular load command LD 1 0 B will: The load-with-indirection command LDI 1 0 B will
• look in memory location 0B • look in register B
• nd the stored value FF • nd the value A7
• load the value FF into R[1]. • then look in memory location A7
• nd the value D4
• load the value D4 into R[1].
22
fi
fi
fi
,
23
Flow of control: branch operations
BZ d x Set PC to 2A if R[8] = 0.
• sets the program counter to x if R[d]
pseudocode if R[8]=0: PC = 2A
equals zero
machine C 8 2 A
assembly BZ 8 2 A
JR d Set PC to R[F].
• sets program counter to value in R[d]
pseudocode PC = E0
machine E F 0 0
assembly JR F 0 0
fl
.
address assembly
Process
00 LD 1 21
1. Fetch the rst value and store it in register 1
01 LD 2 22
2. Fetch the second value and store it in register 2
02 ADD 3 1 2
03 ST 3 20 3. Add the values and store in register 3
… 5.Hal
20 0000 Outcome
21 0005
• the value held at address 0020 is set to 001116 (1710).
22 000C
26
t
fi
.
27
Example Program 2: conditional ow of events
28
:
fl
Example Program 3: iteration (looping)
29
:
fi
l
31
fi
fi
.
Required Readin
• Course textbook chapter 6.
Suggested learning activities
• Practice writing small algorithms
• First in pseudocode
• Then translate to assembly
If you’re interested in Assembly Language
• You can see the language for a processor popular in the 1980s, the Motorola 68000
• And compare that with the language for the X86 family of processors that are still in use today
32
g