Chapter 03 Assembly Language
Chapter 03 Assembly Language
Slides
PROPRIETARY MATERIAL. 2014 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the
prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. PowerPoint Slides are being provided only to
authorized professors and instructors for use in preparing for classes using the affiliated textbook. No other use or distribution of this PowerPoint slide is permitted. The PowerPoint slide may not be sold and may not
be distributed or be used by any student or any other third party. No part of the slide may be reproduced, displayed or distributed in any form or by any means, electronic or otherwise, without the prior written
permission of McGraw Hill Education (India) Private Limited.
These slides are meant to be used along with the book:
Computer Organisation and Architecture, Smruti Ranjan
Sarangi, McGrawHill 2015
Visit:
http://www.cse.iitd.ernet.in/~srsarangi/archbooksoft.html
Outline
3
What is Assembly Language
4
Why learn Assembly
Language?
Software developers' perspective
Write highly efficient code
Suitable for the core parts of games, and mission critical
software
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
9
View of Memory
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
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]
a[0] a[1] a[2]
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
20
Generic Statement Structure - II
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), flags.GT = 1 (greater than)
33
mov instruction
mov r1,r2 r1 r2
mov r1,3 r1 3
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, flags.GT = 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, flags.GT = 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, flags.GT = 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
44
Example with Shift
Instructions
Compute 101 * 6 with shift operators
mov r0, 101
lsl r1, r0, 1
lsl r2, r0, 2
add r3, r1, r2
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
49
Branch Instructions
Unconditional branch instruction
b .foo branch to .foo
50
Conditional Branch
Instructions
beq .foo branch to .foo if flags.E = 1
bgt .foo branch to .foo if flags.GT = 1
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
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+
return address
4
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
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
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