Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

9.

3 Assembly Language
9.3 Assembly Language
• Machine Language
• Assembly Language
Machine Language

The language of the machine is represented using binary numbers


Typically a machine instruction consists of an opcode, followed by 0 to 3 register
or address elds.

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 address1 address2 address3

0001 1011 0010 0101


machine language
1 B 2 5

ADD B 2 5 assembly languag


(coming later)
3
fi
e

TOY machine language instructions: Recap

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

opcode operation format pseudocode


0 halt — halt
Format RR
1 add RR R[d] = R[s] + R[t] 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

2 subtract RR R[d] = R[s] - R[t]


opcode destination d source s source t
3 bitwise and RR R[d] = R[s] & R[t]
4 bitwise xor RR R[d] = R[s] ^ R[t] Format A
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
5 shift left RR R[d] = R[s] << R[t]
6 shift right RR R[d] = R[s] >> R[t] opcode destination d address ADDR
7 load addr A R[d] = addr
8 load A R[d] = M[addr]
ZERO R[0] is always 0.
9 store A M[addr] = R[d]
A load indirect RR R[d] = M[R[t]] STANDARD INPUT Load from FF.

B store indirect RR M[R[t]] = R[d] STANDARD OUTPUT Store to FF.


C branch zero A if (R[d] == 0) PC = addr
D branch positive A if (R[d] > 0) PC = addr
M[x] means ‘value held at memory location x’
E jump register RR PC = R[d]
Example: If memory location 5E holds the value 7,
F jump and link A R[d] = PC + 1; PC = addr
then M[5E] = 7

5
.

Pop quiz 1 on TOY machine instructions

Q. What is the interpretation of:

1A75 as a TOY instruction?

1A75 as a two's complement integer value?

0FFF as a TOY instruction?

0FFF as a two's complement integer value?

8888 as a TOY instruction?

8888 as a two's complement integer value? (Answer in base 16).


6
Pop quiz 1 on TOY machine instructions

Q. What is the interpretation of:

1A75 as a TOY instruction? A. Add R[7] to R[5] and put the result in R[A].

1A75 as a two's complement integer value? A. 1×163 + 10×162 + 7×16 +


= 4096 + 2560 + 112 + 5 = 6773.

0FFF as a TOY instruction? A. Halt.

0FFF as a two's complement integer value? A. 1×163 − 1 = 4095.

8888 as a TOY instruction?


A. Load M[88] into R8.

8888 as a two's complement integer value? (Answer in base 16). A. −777816.


7
5

Pop quiz 2 on TOY machine instructions

Q. How does one ip all the bits in a TOY register ?

8
fl
Pop quiz 2 on TOY machine instructions

Q. How does one ip all the bits in a TOY register ?

A. XOR with FFFF.


x y x XOR y
0 1 0 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0
0 1 1
XOR 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1
= 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 1 0

9
fl
Pop quiz 3 on TOY machine instructions

Q. What does the following TOY program leave in R[2] ?

10 7 C 0 A R[C] = 1010

11 7 1 0 1 R[1] = 1

12 7 2 0 1 R[2] = 1

13 1 2 2 2 R[2] = R[2] + R[2]


14 2 CC 1 R[C] = R[C] - 1
15 DC 1 3 if (R[C] > 0) PC = 13
16 0 0 0 0 HALT

10
Pop quiz 3 on TOY machine instructions

Q. What does the following TOY program leave in R[2]?

10 7 C 0 A R[C] = 1010

11 7 1 0 1 R[1] = 1

12 7 2 0 1 R[2] = 1

13 1 2 2 2 R[2] = R[2] + R[2]


14 2 CC 1 R[C] = R[C] - 1
15 DC 1 3 if (R[C] > 0) PC = 13
16 0 0 0 0 HALT

A. 210 = 102410 = 040016.

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
.

Machine to Assembly Language

Every processor or processor family has its own machine language.

…and for each machine language there is an accompanying assembly language.

…so there are many different assembly languages.

For learning purposes we are using TOY, which has a simpli ed language that provides
• 16 instructions
• 16 registers.

In fact, real-world assembly languages are not much bigger.

And to convert a program written in assembler language into machine language, we need to
use an assembler.
14
.

fi
:

Assemblers: what do they do?

An assembler is a program that converts your assembly language program into


the binary instructions of the machine language
It reads source les and produces an object le in two passes:

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.

The output is an object program le which contains machine language instructions.

15
fi
fi
fi
.

TOY Assembly Language

opcode assembly pseudocode


0 HLT halt Note
1 ADD d s t R[d] = R[s] + R[t]
2 SUB d s t R[d] = R[s] - R[t] An assembler for TOY is not
3 AND d s t R[d] = R[s] & R[t] yet available
4 XOR d s t R[d] = R[s] ^ R[t]
So we can’t write TOY
5 SHL d s t R[d] = R[s] << R[t]
programs directly in assembly
6 SHR d s t R[d] = R[s] >> R[t]
7 LDA d x R[d] = x But we can do this
8 LD d x R[d] = M[x]
9 ST d x M[x] = R[d] 1. Design the program in
A LDI d Rt R[d] = M[R[t]] pseudocode
2. Create the assembly code
B STI d Rt M[R[t]] = R[d]
3. Convert the assembly to
C BZ d x if (R[d] == 0) PC = x
machine language
D BP d x if (R[d] > 0) PC = x manually
E JR d PC = R[d] 4. Keep the assembly code as
F JL d x R[d] = PC + 1; PC = x code comments.

16
:

Halt, Add and Subtract

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

Bitwise AND and XOR

AND d s Perform R[2] AND R[3] and store result in R[A].


• performs bitwise s AND
pseudocode R[A] = R[2] AND R[3]
• stores result in a register d
machine 3 A 2 3
assembly AND A 2 3

XOR d s t Perform R[D] XOR R[8] and store result in R[5].


• performs bitwise s XOR
pseudocode R[5] = R[D] XOR R[8]
• stores result in a register d
machine 4 5 D 8
assembly XOR 5 D 8

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

Shift left and right

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

Useful for multiplication and division.

19
t

Basic Load and Store

Load and store allow us to move data between main memory and the CPU, by
copying the data to and from registers.

LDA d x Load the value 2C into R[5].


• loads value x directly into register
pseudocode R[5] = 2C
• also useful for array indexing
machine 7 5 2 C
assembly LDA 5 2 C

LD d x Load the value at address 2C into R[5].


• reads value from a memory address x
pseudocode R[5] = M[2C]
and loads it into register d
machine 8 5 2 C
assembly LD 5 2 C
20
d

Basic Load and Store

ST d x Store the value in R[3] into M[F7].


• stores a copy of the value from register d
pseudocode M[F7] = R[3]
into memory address x
machine 9 3 F 7
assembly ST 3 F 7

21
Load and Store with Indirection

Indirection is very useful if we want to dynamically alter where an


instruction looks to in main memory in order to load or store data.

Example: suppose that


• Register B holds the value A7 (so R[B] = A7)
• Memory location 0B holds the value FF (so M[0B] = FF)
• Memory location A7 holds the value D4 (so M[A7] = D4).

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
,

Load and Store with Indirection

LDI d x Load the value held at M[R[C]] into R[5].


• indirectly loads from memory location
pseudocode R[5] = M[R[C]]
held in R[x] into register d
machine A 5 C 0
assembly LDI 5 C 0

SDI d x Store the value in R[7] into M[R[3]].


• indirectly stores from register d into
pseudocode M[R[3]] = R[7]
memory location held in R[x]
machine B 7 3 0
assembly SDI 7 3 0

23
Flow of control: branch operations

Allows conditional ow of control by allowing manual change of the program counter


Provides rudimentary if-then functionality
Also good for implementing beginning of for and while loops.

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

BP d x Set PC to 4 if R[8] > 0.


• sets the program counter to x if R[d] is
pseudocode if R[8]>0: PC = 4
positive
machine D 8 0 4
assembly BP 8 0 4
24
fl
.

Flow of control: jump operations

Allows conditional ow of control by allowing manual change of the program counter


Provides rudimentary goto and return functionality
Also good for implementing end of for and while loops.

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

JL d x Store (current_PC + 1) in R[2] and set PC to 48.


• sets program counter to x, and records
pseudocode R[2] = PC+1; PC = 48
the PC + 1 in R[d
machine F 2 4 8
• allows ‘call and return’ functionality
assembly JL 2 4 8
25
]

fl
.

Example Program 1: add two values in memory and store result

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

04 HLT 4.Store the value from register 3 in memory

… 5.Hal
20 0000 Outcome
21 0005
• the value held at address 0020 is set to 001116 (1710).
22 000C

26
t

fi
.

Example Program 1: add two values in memory and store result

address assembly machine


00 LD 1 21 8121
01 LD 2 22 8222
02 ADD 3 1 2 1312
03 ST 3 20 9320
04 HLT 0000
… …
20 0000 0000
21 0005 0005
22 000C 000C

27
Example Program 2: conditional ow of events

address assembly code comment


Process
00 LD 1 40 #R[1] ← A
01 LD 2 41 #R[2] ← B if A > B
02 SUB 3 1 2 #R[3] = A-B output
03 BP 3 06 #if result > 0 goto 06
04 ST 2 FF #M[FF] ← B (output B) else

05 HLT #halt output


06 ST 1 FF #M[FF] ← A (output A)
Outcome
07 HLT #halt
… • A < B, so the value 014C is output.
40 007A #A
41 014C #B

28
:

fl
Example Program 3: iteration (looping)

address assembly code comment


00 LDA 1 01 #R[1] ← 1 Process
01 LDA 2 04 #R[2] ← 04 set INDEX to
02 LD 3 20 #R[3] ← MAXINDEX
03 LD 4 21 #R[4] ← INDEX while (INDEX ≤ MAXINDEX)
04 SUB 5 4 3 #R[5] = MAXINDEX-INDEX
set INDEX to INDEX +
05 BZ 4 09 #if R[5]==0: PC = 09
06 ADD 4 4 1 #else: INDEX = INDEX+1 Outcome
07 ST 4 21 #M[21] ← R[4]
08 JR 2 #loop back to PC = 04 • The program increments the value held at
INDEX until it equals 0010.
09 HLT #halt

20 0010 #MAXINDEX
21 0000 #INDEX

29
:

Example Program 4: sum a list of values


Process
address assembly code comment total =
00 LDA 1 01 #R[1] ← 01
start with rst value in lis
01 LDA 6 3 #R[6] = 3 for loopback
while value != -1
02 LDA 2 20 #R[2] ← 20 (location)
add value to tota
03 LDI 3 2 #R[3] ← M[R[2]] (value)
04 SUB 4 0 3 #R[4] ← 0-R[3] move to next value

05 BP 4 09 #if R[3] < 0: PC = 09 output tota


06 ADD 5 5 3 #else add to total Plan
07 ADD 2 2 1 #point to next value
store value 0001 in R[1] to use when incrementin
08 JR 6 #loop back to PC = 01
store location of next value in list in R[2
09 ST 5 FF #output the result
use indirection to fetch the next value into R[3
0A HLT #hlt
… test if the value is negative using R[4] = 0-R[3

20 0002 #value 1 store running total in R[5


21 0004 #value 2 store loopback address in R[6
22 0003 #value 3 Outcome
23 1111 #end of list (-1)
• The program adds the list of numbers to get a total of 0009.
30
:

fi
l

Does anyone still use assembly language?

Rough rule: 10% of code uses 90% of CPU time


Usually modern compilers for high-level languages are good at optimising code
• But sometimes they are not.

So sometimes identifying CPU intensive code and replacing it with hand-coded


assembly can see a signi cant performance gain
This was very common in the 1980s and 1990s
• Some high-level languages even directly accommodated it
• Where they didn't, it was time for 'peek and poke’.

Embedded systems often bene t from code written in assembly


• Due to time and space criticality.

31
fi
fi
.

Reading and References

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

You might also like