Assembly Language Basics and Syntax
Assembly Language Basics and Syntax
[Link]
Download the pdf of the book
videos
3
What is Assembly Language
A low level programming language uses simple statements that
correspond to typically just one machine instruction. These
languages are specific to the ISA.
The term “assembly language” refers to a family of low-level
programming languages that are specific to an ISA. They have a
generic structure that consists of a sequence of assembly
statements.
Typically, each assembly statement has two parts: (1) an
instruction code that is a mnemonic for a basic machine
instruction, and (2) and a list of operands.
4
Why learn Assembly
Language ?
Software developers' perspective
Write highly efficient code
Suitable for the core parts of games, and mission critical software
5
Assemblers
6
Hardware Designers Perspective
7
Machine Model – Von Neumann Machine
with Registers
CPU
Registers
ALU
8
View of Registers
Registers → named storage locations
in ARM : r0, r1, … r15
in x86 : eax, ebx, ecx, edx, esi, edi
Memory
One large array of bytes
Each location has an address
The address of the first location is 0, and increases by 1 for each
subsequent location
The program is stored in a part of the memory
The program counter contains the address of the current
instruction
10
Storage of Data in Memory
Data Types
char (1 byte), short (2 bytes), int (4 bytes), long int (8 bytes)
11
Little Endian vs Big Endian
Big endian
87 65 43 21
0 1 2 3
0x87654321
Little endian
21 43 65 87
0 1 2 3
12
Storage of Arrays in Memory
Single dimensional arrays. Consider an array of integers : a[100]
13
Row Major vs Column Major
Row Major (C, Python)
Store the first row as an 1D array
Then store the second row, and so on...
Column Major (Fortran, Matlab)
Store the first column as an 1D array
Then store the second column, and so on
Multidimensional arrays
Store the entire array as a sequence of 1D arrays
14
Outline
15
Assembly File Structure : GNU
Assembler
Assembly File
.file
.text
.data
16
Meaning of Different Sections
.file
name of the source file
.text
contains the list of instructions
.data
data used by the program in terms of read only variables, and
constants
17
Structure of a Statement
instruction
textual identifier of a machine instruction
operand
constant (also known as an immediate)
register
memory location
18
Examples of Instructions
19
Generic Statement Structure
Label : Directive @ Comment
Constant /* Comment */
Assembly
instruction
label → identifier of a statement
directive → tells the assembler to do something like declare
a function
constant → declares a constant
20
Generic Statement Structure - II
21
Types of Instructions
Data Processing Instructions
add, subtract, multiply, divide, compare, logical or, logical and
Branch instructions
branch to a given label
Special instructions
interact with peripheral devices, and other programs, set machine specific
parameters
22
Nature of Operands
Classification of instructions
If an instruction takes n operands, then it is said to be in the n-address
format
Example : add r1, r2, r3 (3 address format)
Addressing Mode
The method of specifying and accessing an operand in an assembly
statement is known as the addressing mode.
23
Register Transfer Notation
This notation allows us to specify the semantics of
instructions
r1 ← r2
transfer the contents of register r2 to register r1
r1 ← r2 + 4
add 4 to the contents of register r2, and transfer the contents to register r1
r1 ← [r2]
access the memory location that matches the contents of r2, and store the
data in register r1
24
Addressing Modes
Let V be the value of an operand, and let r1, r2 specify registers
Immediate addressing mode
V ← imm , e.g. 4, 8, 0x13, -3
Register indirect
V ← [r1]
25
Register Indirect Mode
V ← [r1]
r1
value
register file
memory
26
Base-offset Addressing Mode
V ← [r1+offset]
r1
offset
value
register file
memory
27
Addressing Modes - II
Base-index-offset
V ← [r1 + r2 + offset]
example: 100[r1,r2] (V ← [r1 + r2 + 100])
Memory Direct
V ← [addr]
example : [0x12ABCD03]
PC Relative
V ← [pc + offset]
example: 100[pc] (V ← [pc + 100])
28
Base-Index-Offset Addressing Mode
V ← [r1+r2 +offset]
r1
offset
r2
value
register file
memory
29
Outline
30
SimpleRisc
Simple RISC ISA
Contains only 21 instructions
We will design an assembly language for SimpleRisc
Design a simple binary encoding,
and then implement it ...
31
Survey of Instruction Sets
ISA Type Year Vendor Bits Endianness Registers
VAX CISC 1977 DEC 32 little 16
RISC 1986 Sun 32 big 32
SPARC
RISC 1993 Sun 64 bi 32
RISC 1992 Apple,IBM,Motorola 32 bi 32
PowerPC
RISC 2002 Apple,IBM 64 bi 32
RISC 1986 HP 32 big 32
PA-RISC
RISC 1996 HP 64 big 32
CISC 1979 Motorola 16 big 16
m68000
CISC 1979 Motorola 32 big 16
RISC 1981 MIPS 32 bi 32
MIPS
RISC 1999 MIPS 64 bi 32
Alpha RISC 1992 DEC 64 bi 32
CISC 1978 Intel,AMD 16 little 8
x86 CISC 1985 Intel,AMD 32 little 8
CISC 2003 Intel,AMD 64 little 16
RISC 1985 ARM 32 bi(little default) 16
ARM
RISC 2011 ARM 64 bi(little default) 31
32
Registers
SimpleRisc has 16 registers
Numbered : r0 … r15
r14 is also referred to as the stack pointer (sp)
r15 is also referred to as the return address register (ra)
View of Memory
Von Neumann model
One large array of bytes
Special flags register → contains the result of the last comparison
flags.E = 1 (equality), [Link] = 1 (greater than)
33
mov instruction
mov r1,r2 r1 ¬ r2
mov r1,3 r1 ¬ 3
34
Arithmetic/Logical Instructions
SimpleRisc has 6 arithmetic instructions
add, sub, mul, div, mod, cmp
Example Explanation
add r1, r2, r3 r1 ¬ r2 + r3
add r1, r2, 10 r1 ¬ r2 + 10
sub r1, r2, r3 r1 ¬ r2 – r3
mul r1, r2, r3 r1 ¬ r2 × r3
div r1, r2, r3 r1 ¬ r2/r3 (quotient)
mod r1, r2, r3 r1 ¬ r2 mod r3 (remainder)
cmp r1, r2 set flags
35
Examples of Arithmetic Instructions
mov r0, 3
mov r1, 5
cmp r0, r1
flags.E = 0, [Link] = 0
38
Compare Instruction
Compare 5 and 3, and print the value of
the flags
a=5
b=3
compare a and b
mov r0, 5
mov r1, 3
cmp r0, r1
flags.E = 0, [Link] = 1
39
Compare Instruction
Compare 5 and 5, and print the value of
the flags
a=5
b=5
compare a and b
mov r0, 5
mov r1, 5
cmp r0, r1
flags.E = 1, [Link] = 0
40
Example with Division
SimpleRisc
mov r1, 31
mov r2, 29
div r3, r1, r2
sub r4, r3, 50
41
Logical Instructions
Compute (a | b). Assume that a is stored in r0, and b is stored in r1. Store
the result in r2.
Answer:
SimpleRisc
or r2, r0, r1
42
Shift Instructions
Logical shift left (lsl) (<< operator)
0010 << 2 is equal to 1000
(<< n) is the same as multiplying by 2n
Arithmetic shift right (asr) (>> operator)
0010 >> 1 = 0001
1000 >> 2 = 1110
same as dividing a signed number by 2n
43
Shift Instructions - II
Example Explanation
lsl r3, r1, r2 r3 r1 << r2 (shift left)
lsl r3, r1, 4 r3 r1 << 4 (shift left)
lsr r3, r1, r2 r3 r1 >>> r2 (shift right logical)
lsr r3, r1, 4 r3 r1 >>> 4 (shift right logical)
asr r3, r1, r2 r3 r1 >> r2 (arithmetic shift right)
asr r3, r1, 4 r3 r1 >> r2 (arithmetic shift right)
44
Example with Shift Instructions
45
Example - II
46
Load-store instructions
ld r1, 10[r2] r1 [r2 +10]
st r1, 10[r2] [r2+10] r1
47
Load-Store
(a) (b)
48
Example – Load/Store
Translate :
int arr[10];
arr[3] = 5;
arr[4] = 8;
arr[5] = arr[4] + arr[3];
49
Branch Instructions
Unconditional branch instruction
50
Conditional Branch Instructions
51
Examples
If r1 > r2, then save 4 in r3, else save 5 in r3
cmp r1, r2
bgt .gtlabel
mov r3, 5
...
...
.gtlabel:
mov r3, 4
52
Example - II
Answer: Compute the factorial of the variable num.
C
int prod = 1;
int idx;
for(idx = num; idx > 1; idx --) {
prod = prod * idx
}
53
Write a SimpleRisc assembly program to find the
smallest number that is a sum of two cubes in
two different ways → 1729
54
Modifiers
We can add the following modifiers to an instruction
that has an immediate operand
Modifier :
default : mov → treat the 16 bit immediate as a signed number
(automatic sign extension)
(u) : movu → treat the 16 bit immediate as an unsigned number
(h) : movh → left shift the 16 bit immediate by 16 positions
55
Mechanism
56
More about Modifiers
57
Examples
Move : 0x FF FF A3 2B in r0
mov r0, 0xA32B
Move : 0x 00 00 A3 2B in r0
movu r0, 0xA32B
Move : 0x A3 2B 00 00 in r0
movh r0, 0xA32B
58
Example
Set r0 ← 0x 12 AB A9 2D
movh r0, 0x 12 AB
addu r0, 0x A9 2D
59
Outline
60
Implementing Functions
function foo
61
Implementing Functions - II
62
Notion of the Return Address
callee
caller
p function call
p+
4 return address
ret
.foo:
add r2, r0, r1
ret
.main:
mov r0, 3
mov r1, 5
call .foo
add r3, r2, 10
64
Problems with this Mechanism
* Space Problem
* We have a limited number of registers
* We cannot pass more than 16 arguments
* Solution : Use memory also
* Overwrite Problem
* What if a function calls itself ? (recursive call)
* The callee can overwrite the registers of the caller
* Solution : Spilling
65
Register Spilling
* The notion of spilling
* The caller can save the set of registers its needs
* Call the function
* And then restore the set of registers after the function returns
* Known as the caller saved scheme
* callee saved scheme
* The callee saves, the registers, and later restores them
66
Spilling
Caller Caller
Save registers
Callee Callee
Save registers
Restore registers
Restore registers
67
Problems with our Approach
68
Activation Block
Activation block
int foo(int arg1) { Arguments
int a, b, c; Return address
a = 3; b = 4;
Register spill area
c = a + b + arg1;
return c;
} Local variables
69
How to use activation blocks ?
Assume caller saved spilling
Before calling a function : spill the registers
Allocate the activation block of the callee
Write the arguments to the activation block of the callee, if
they do not fit in registers
Call the function
70
Using Activation Blocks - II
In the called function
Read the arguments and transfer to registers (if required)
Save the return address if the called function can call other
functions
Allocate space for local variables
Execute the function
Once the function ends
Restore the value of the return address register (if required)
Write the return values to registers, or the activation block of the
caller
Destroy the activation block of the callee 71
Using Activation Blocks - III
Once the function ends (contd …)
Call the ret instruction
and return to the caller
The caller :
Retrieve the return values from the registers of from its
activation block
Restore the spilled registers
continue ...
72
Organising Activation Blocks
All the information of an executing function is stored in its
activation block
These blocks need to be dynamically created and destroyed
– millions of times
What is the correct way of managing them, and ensuring
their fast creation and deletion ?
Is there a pattern ?
73
Pattern of Function Calls
test test2
74
Pattern of Function Calls
Last in First Out
Use a stack to store activation blocks
Stack
foo foo foo foo
foobarbar
Overwrite problem
Solved by activation blocks
77
call and ret instructions
79
Factorial in SimpleRisc
.factorial:
cmp r0, 1 /* compare (1,num) */
beq .return
bgt .continue
b .return
.continue:
sub sp, sp, 8 /* create space on the stack */
81
Outline
82
Encoding Instructions
Encode the SimpleRisc ISA using 32 bits.
We have 21 instructions. Let us allot each instruction an
unique code (opcode)
83
Basic Instruction Format
5 27
opcode rest of the instruction
84
0-Address Instructions
32
opcode
85
1-Address Instructions
32
opcode offset
op offset
5 27
Instructions – call, b, beq, bgt
Use the branch format
Fields :
5 bit opcode
27 bit offset (PC relative addressing)
Since the offset points to a 4 byte word address
The actual address computed is : PC + offset * 4
86
3-Address Instructions
Instructions – add, sub, mul, div, mod, and, or, lsl, lsr, asr
Generic 3 address instruction
<opcode> rd, rs1, <rs2/imm>
Let us use the I bit to specify if the second operand is an
immediate or a register.
I = 0 → second operand is a register
I = 1 → second operand is an immediate
Since we have 16 registers, we need 4 bits to specify a register
87
Register Format
32
5 1 4 4 4
opcode → type of the instruction
I bit → 0 (second operand is a register)
dest reg → rd
source register 1 → rs1
source register 2 → rs2
88
Immediate Format
32
90
cmp, not, and mov
32
5 1 4 4 18
32
5 1 4 4 18
32
5 1 4 4 18
91
Load and Store Instructions
ld rd, imm[rs1]
rs1 → base register
Use the immediate format.
32
5 1 4 4 18
92
Store Instruction
Strange case of the store inst.
st reg1, imm[reg2]
has two register sources, no
register destination, 1 immediate
Cannot fit in the immediate format, because the second operand
can be either be a register OR an immediate (not both)
Should we define a new format for store
instructions ?
Maybe not
93
Store Instruction
Let us make an exception and use the immediate format.
We use the rd field to save one of the source registers
st rd, imm[rs1]
32
5 1 4 4 18
94
Summary of Instruction Formats
Format Definition
branch op (28-32) offset (1-27)
register op (28-32) I (27) rd (23-26) rs 1(19-22) rs 2(15-18)
immediate op (28-32) I (27) rd (23-26) rs 1(19-22) imm (1-18)
op → opcode, offset → branch offset, I → immediate bit, rd → destination register
rs1 → source register 1, rs2 → source register 2, imm → immediate operand
95
THE END
96