assembly programming
TRANSCRIPT
Introduction to Computer Organization
Assembly Programming
Amey Karkare [email protected] Department of CSE, IIT Kanpur
MIPS Assembly CS220, CO 1
Acknowledgement
The material in the following slides is based on the following
references:
Spim documentation (Appendix A) from the third edition of
Computer Organization and Design: The Hardware/Software
Interface by Patterson & Hennessy
MIPS Assembly Language Programming: CS50 Discussion and
Project Book by Daniel J Ellard
MIPS Assembly CS220, CO 2
Why Assembly / High Level Languages?
MIPS Assembly CS220, CO 3
Why Assembly / High Level Languages?
Instructions as Binary Strings
Easy for computers to understand
Natural and efficient to manipulate
Humans have difficulty in understanding Binary Strings
Humans read/write symbols(words) much better than long
sequence of digits
How can Humans communicate with Computers effectively?
Humans read/write strings of symbols (Programs in
Assembly/High level languages).
Automated tools process these programs and convert them to
binary strings (object code/executable code).
Computers read and execute binary stings.
MIPS Assembly CS220, CO 4
xxN.c xxN.s xxN.o
. . . . . . . . . . exec-
utable xx2.c xx2.s xx2.o
xx1.c xx1.s xx1.o Program
Libraries
assembler
compiler
assembler
compiler
linker
assembler
compiler
MIPS Assembly 5 CS220, CO
Typical Flow: High Level Language Program
Typical Flow: Assembly Program
xxN.s xxN.o
. . . . . . exec-
utable xx2.s xx2.o
xx1.s xx1.o Program
Libraries
assembler
assembler
linker
assembler
MIPS Assembly CS220, CO 6
When to use Assembly Programs
A new architecture has come up.
Non-availability of tools for high level languages for the platform.
Boot-strapping
To develop high level language tools in assembly.
In reality, goes down to assembly - obj code level as well.
MIPS Assembly CS220, CO 7
When to use Assembly Programs
When the requirements are critical.
Time to respond.
Size of code.
Architecture specific operations.
Fine grain control over execution is required.
To avoid surprises
Unexpected compiler optimizations.
Unwanted re-ordering of instructions.
MIPS Assembly CS220, CO 8
When to use Assembly Programs
Commercial application use Hybrid Approach.
Most of the code is in high level language.
Resource/Time critical code is written in assembly.
MIPS Assembly CS220, CO 9
Drawbacks of Assembly Programming
Assembly programs are machine specific.
Need to be re-written for a different architecture.
Can not automatically make use of advances in architecture.
Assembly programs are longer.
Expansion factor of more than 3 compared to same program
written in high level language.
Low productivity of programmers.
Empirical studies have shown that programmers write nearly the
same number of lines of code per day, irrespective of the language.
Productivity goes down by X if X is the expansion factor.
MIPS Assembly CS220, CO 10
Drawbacks of Assembly Programming
Programs are difficult to understand.
More bugs.
Difficult to maintain.
There are no strictly forced rules to program in assembly.
No type checking.
No scopes.
No fixed rules for parameter passing.
While guidelines/conventions exist, it is up to the
programmer to follow them or ignore them.
MIPS Assembly CS220, CO 11
Assembler
Assembler translates assembly program into object code.
Object code contains binary machine instructions.
plus some bookkeeping information.
Object code is not same as executable code.
Obj code can contain references to external symbol (say, call to
printf function).
Only local symbols are visible while assembling a file.
Another tool called linker resolves cross-file dependencies and
produces executable code.
MIPS Assembly CS220, CO 12
Linker
Resolves external references among files (cross-file
references).
Searches program libraries to find and link library routines
used by the program.
Determines memory locations that code from each
function will occupy and relocates its instructions by
adjusting absolute references.
If linking is successful, output of linker is the executable –
file that is ready to execute.
MIPS Assembly CS220, CO 13
MIPS Architecture
Register architecture.
Arithmetic and logic operations involve only registers or immediate
constants.
Load-store architecture.
Data is loaded from memory into registers or stored to memory
from registers.
No direct manipulation of memory contents.
MIPS Assembly CS220, CO 14
MIPS Register Set
32 registers, each 32 bit wide.
Register 0 is hardwired to contain value 0 all the time.
Remaining 31 registers are general purpose registers,
Theoretically, these registers can be used interchangeably.
General purpose register 31 is used as the link register for jump
and link instructions.
MIPS programmers have developed set of conventions
to use these registers.
These calling conventions are maintained by the tool-chain
softwares, but these are not enforced by the hardware.
MIPS Assembly CS220, CO 15
MIPS Register Set
Register
Number
Common
Name
Usage
0 zero Hardwired value 0. Any writes to this reg-
ister are ignored.
1 at Assembler temporary.
2–3 v0–v1 Function result registers
4–7 a0–a3 Function argument registers that hold the
first four arguments.
8–15,24–25 t0–t9 Temporary registers.
16–23 s0–s7 Saved registers to use freely.
26–27 k0–k1 Reserved for use by the operating system
kernel and for exception return.
28 gp Global pointer.
29 sp Stack pointer.
30 fp Frame Pointer.
31 ra Return address register.
MIPS Assembly CS220, CO 16
SPIM Instruction Set
Arithmetic and Logic Instructions
add(u), sub(u), mul(u), div(u), abs, . . .
rol, ror, sll, srl, sra, and, or, not, . . .
Comparison Instructions
seq, sne, sge(u), sgt(u), sle(u), slt(u)
Branch and Jump Instructions
b, beq, bne, bge(u), beqz, bnez, bgezal, . . .
j, jr, jal, jalr
MIPS Assembly CS220, CO 17
SPIM Instruction Set
Load, Store and Data Movement
la, lb(u), lh(u), lw, lwl, lwr, li, . . .
sb, sh, sw
ulh(u), ulw, ush, usw, swl, swr (unaligned load/store)
move, mfhi, mflo, mthi, mtlo
Exception Handling
rfe, syscall, break, nop
Refer to MIPS documentation for details of individual instructions.
MIPS Assembly CS220, CO 18
Addressing Modes
• Bare Machine: imm(reg)
• imm: Immediate Constant
• reg: Register containing address
• Address computation: Contents of reg + imm constant
MIPS Assembly 19 CS220, CO
Addressing Modes
• Virtual Machine:
MIPS Assembly 20 CS220, CO
Format Address Computation
(reg) Contents of reg
imm Immediate constant
imm(reg) Immediate + contents of reg
label Address of label
label ± imm Address of label ± immediate
Label ± imm(reg) Address of label ± (immdiate + content of reg)
System Calls • Frequently used system calls
MIPS Assembly 21 CS220, CO
Service Code Argument Result
Print int 1 a0 --
Print string 4 a0 --
Read int 5 -- v0
Read string 8 a0(address) a1(length)
--
sbrk 9 a0(amount) v0
exit 10 -- --
Iterative Factorial Function
MIPS Assembly CS220, CO 22
#
# Compute the factorial of given
# number and print the result
#
# $t0: holds the input
# $t1: holds the result: Factorial($t0)
#
Iterative Factorial Function
MIPS Assembly CS220, CO 23
# fact.asm
.text
.globl main
main:
# read the number...
# first the query
la $a0, query_msg
li $v0, 4
syscall
# now read the input
li $v0, 5
syscall
# and store in $t0
move $t0, $v0
# store the base value in $t1
li $t1, 1
fact:
blez $t0, done
mul $t1, $t1, $t0
sub $t0, $t0, 1
b fact
done: # print the result
# first the message
la $a0, result_msg
li $v0, 4
syscall
# then the value
move $a0, $t1
li $v0, 1
syscall
# then newline
la $a0, nl_msg
li $v0, 4
syscall
# exit
li $v0, 10
syscall
.data
query_msg: .asciiz "Input? "
result_msg: .asciiz “Factorial = "
nl_msg: .asciiz "\n"
Procedure Calls
In a high level language like C, the compiler provides several useful abstractions, for e.g.
Mapping of actuals to formals.
Allocation and initialization of temporary local storage.
Each invocation of procedure gets its own copy of local variables.
Required to support recursion.
MIPS Assembly CS220, CO 24
Procedure Calls
In assembly language, programmer has to explicitly maintain most of the procedure’s environment (local variables, actual to formal mapping, return value, return address etc.)
Use of stack to store environment for each procedure.
When procedure A calls procedure B, programmer has to write code to:
save A’s environment on the stack
jump to B
when B returns, restore A’s environment from stack
MIPS Assembly CS220, CO 25
Layout of Memory
Data Segment
Stack
Dynamic
– – – – – – – – – – – – –
Static
Text Segment
Reserved
MIPS Assembly CS220, CO 26
Layout of Stack Frame
High Memory Address
$fp
Stack Grows
$sp Lower Memory Address
$fp points to the first word in the active procedure’s stack frame.
$sp points to the last word in the active procedure’s stack frame.
Arguments 1. . . 4 are stored in $a0. . . $a3.
. . .
Arg # 6
Arg #5
Saved
Registers
Local
Variables
MIPS Assembly CS220, CO 27
Building Stack Frames
No fixed sequence.
Only conventions.
Caller and Callee must agree on the sequence of steps.
Calling sequences
Consists of Call sequence and Return sequence.
MIPS Assembly CS220, CO 28
Placement of Calling Sequences
Call Sequence
Immediately before caller invokes callee.
Just as the callee starts executing.
Return Sequence
Immediately before the callee returns.
Just as the control reaches caller.
A possible sequence of steps is detailed next.
MIPS Assembly CS220, CO 29
Call Sequence: Caller
Save Caller-Saved Registers
As the called procedure is free to use $a0. . . $a3, $t0. . . $t9; caller
must store them on stack before the call, if they are required after
the call.
Pass Arguments
First four arguments are copied to $a0. . . $a3.
Remaining arguments are stored on stack. These appear just
above the callee’s stack frame.
Jump to Callee
Execute JAL instruction.
Stores the return address in $ra.
MIPS Assembly CS220, CO 30
Call Sequence: Callee
Allocate Memory for current callee invocation on the stack
(stack frame of callee).
Save Callee-Saved Registers
If callee code can change any of the registers $s0. . . $s7, $fp, $ra,
it needs to store it in the stack frame before changing.
Caller expects to see these registers “unmodified”.
Establish new values for $fp, $sp.
MIPS Assembly CS220, CO 31
Return Sequence: Callee
If value is to be returned put it in $v0.
Restore $s0. . . $s7, $fp, $ra (callee saved registers).
Restore $sp (pop out the callee’s stack frame).
Return by jumping to $ra address.
MIPS Assembly CS220, CO 32
Return Sequence: Caller
Restore caller saved registers.
Cleanup (pop) the arguments on the stack, if passed that
way.
Move the return value from $v0 to appropriate register.
MIPS Assembly CS220, CO 33
Recursive Factorial Function
MIPS Assembly CS220, CO 34
#
# Compute the factorial of given
# number and print the result
#
# $t0: holds the input
# $t1: holds the result: Factorial($t0)
#
Recursive Factorial Function
MIPS Assembly CS220, CO 35
# main.asm
.text
.globl main
main:
# read the number...
# first the query
la $a0, query_msg
li $v0, 4
syscall
# now read the input
li $v0, 5
syscall
# and store in $t0
move $t0, $v0
# call the recursive factorial
jal fact
# result in $v0, copy to $t1
move $t1, $v0
done: # print the result
# first the message
la $a0, result_msg
li $v0, 4
syscall
# then the value
move $a0, $t1
li $v0, 1
syscall
# then newline
la $a0, nl_msg
li $v0, 4
syscall
# exit
li $v0, 10
syscall
.data
query_msg: .asciiz "Input? "
result_msg: .asciiz “Factorial = "
nl_msg: .asciiz "\n"
Recursive Factorial Function
MIPS Assembly CS220, CO 36
# fact.asm
# Compute and return the factorial of input
# Implementation:
# fact(n) {
# if (n <= 0) return 1;
# return n * fact(n-1);
# }
# $a0: parameter => n
# $v0: return value => factorial(n)
#
.global fact
.text
Recursive Factorial Function
MIPS Assembly CS220, CO 37
# attempt 1
fact:
li $v0, 1
blez $a0, fact_return
addi $a0, $a0, -1
jal fact
# fact (n-1) in $v0, n-1 in $a0
addi $a0, $a0, 1
mul $v0, $v0, $a0
# fact(n) in $v0 at this point
fact_return:
jr $ra
Recursive Factorial Function
MIPS Assembly CS220, CO 38
# attempt 2
fact:
addi $sp, $sp, -4 #space for a word
sw $ra, 0($sp) #save $ra for later use
li $v0, 1
blez $a0, fact_return
addi $a0, $a0, -1
jal fact
# fact (n-1) in $v0, n-1 in $a0
addi $a0, $a0, 1
mul $v0, $v0, $a0
# fact(n) in $v0 at this point
fact_return:
lw $ra, 0($sp) #restore $ra
addi $sp, $sp, 4 #restore $sp
jr $ra
Recursive Factorial Function:
MIPS Assembly CS220, CO 39
# attempt 3: Hand Optimized
fact:
li $v0, 1
blez $a0, fact_return
addi $sp, $sp, -4 #space for a word
sw $ra, 0($sp) #save $ra for later use
addi $a0, $a0, -1
jal fact
# fact (n-1) in $v0, n-1 in $a0
addi $a0, $a0, 1
mul $v0, $v0, $a0
lw $ra, 0($sp) #restore $ra
addi $sp, $sp, 4 #restore $sp
# fact(n) in $v0 at this point
fact_return:
jr $ra
Recursive Factorial Function:
MIPS Assembly CS220, CO 40
# attempt 4: Close to compiler generated
fact:
addi $sp, $sp, -8 #space for 2 words
sw $a0, 4($sp) #save n for later use
sw $ra, 0($sp) #save $ra for later use
li $v0, 1
blez $a0, fact_return
addi $a0, $a0, -1
jal fact
# fact (n-1) in $v0, need n
lw $a0, 4($sp)
mul $v0, $v0, $a0
fact_return: #fact(n) in $v0 here
lw $ra, 0($sp) #restore $ra
addi $sp, $sp, 8 #restore $sp
jr $ra
Arrays
.space directive is used to reserve contiguous space in memory
Only “raw” memory is reserved, no sense of type of elements
.data
mylist: .space 100 #reserve 100 bytes
MIPS Assembly 41 CS220, CO
Arrays
.data
mylist: .space 100 #reserve 100 bytes
mylist denotes the address of the
first byte in the reserved space
Rest of the elements can be accessed by adding appropriate offsets.
MIPS Assembly 42 CS220, CO
Array with Initialization
.data
vowels: .byte ‘a’, ‘e’, ‘i’, ‘o’, ‘u’
pow2: .word 1, 2, 4, 8, 16, 32, 64
• Address Computation
vowels[k] = vowels + k
pow2[k] = pow2 + k*4
MIPS Assembly 43 CS220, CO
97
101
105
111
117
1
2
1004000
1004001
1004002
1004003
1004004
1004005
1004006
1004007
1004008
1004009
1004010
1004011
1004012
Memory
a
lloc
for v
ow
els
allo
c fo
r po
w2
Array Bounds
.data
vowels: .byte ‘a’, ‘e’, ‘i’, ‘o’, ‘u’
pow2: .word 1, 2, 4, 8, 16, 32, 64
la $t0, vowels
lb $t1, 5($t0) # t1 = vowels[5]
• What is the value in $t1?
• Unpredictable!
MIPS Assembly 44 CS220, CO
97
101
105
111
117
1
2
1004000
1004001
1004002
1004003
1004004
1004005
1004006
1004007
1004008
1004009
1004010
1004011
1004012
Memory
allo
c fo
r vo
we
ls
a
lloc
for p
ow
2
Strings
.data
vowels1: .ascii “AEIOU”
vowels2: .asciiz “AEIOU”
MIPS Assembly 45 CS220, CO
A
E
I
O
U
A
E
I
O
U
‘\0’
1004000
1004001
1004002
1004003
1004004
1004005
1004006
1004007
1004008
1004009
1004010
Memory
a
lloc
for v
ow
els
1
a
lloc
for v
ow
els
2
MIPS Assembly CS220, CO
Example: Palindromes
A palindrome is a word or sentence that spells exactly the same thing both forward and backward.
able was i ere i saw elba
anna
nitin
46
Space for String
Reserve space for string to be tested and messages
.data
string_space: .space 1024
is_palin_msg: .asciiz “YES”
not_palin_msg: .asciiz “NO”
MIPS Assembly CS220, CO 47
The main Program
.text
main:
la $a0, string_space
li $a1, 1024
li $v0, 8 # read_string
syscall
la $t1, string_space
la $t2, string_space
MIPS Assembly CS220, CO 48
The main Program (contd…)
# $t1 and $t2 at the start
# move $t2 to the end
length_loop:
lb $t3, ($t2) # byte at $t2
beqz $t3, end_loop # end found
addu $t2, $t2, 1 # else move
b length_loop # and repeat
end_loop:
subu $t2, $t2, 2 # ignore \n, \0
MIPS Assembly CS220, CO 49
The main Program (contd…)
# $t1 at start, $t2 at the end
test_loop:
bge $t1, $t2, is_palin
# matched all the way to center
lb $t3, $(t1) # load bytes
lb $t4, $(t2) # at current marks
# and compare
bne $t3, $t4, not_palin # Chars
# do not match => not palindrom
MIPS Assembly CS220, CO 50
The main Program (contd…)
# Chars match, move $t1 forward
# $t2 backward
addu $t1, $t1, 1
subu $t2, $t2, 1
b test_loop # continue testing…
MIPS Assembly CS220, CO 51
The main Program (contd…)
# Palindrome test successful
# print success msg and exit
is_palin:
la $a0, is_palin_msg
li $v0, 4
syscall
b exit
MIPS Assembly CS220, CO 52
The main Program (contd…)
# Palindrome test unsuccessful
# print failure msg and exit
not_palin:
la $a0, not_palin_msg
li $v0, 4
syscall
b exit
exit:
li $v0, 10
syscall MIPS Assembly CS220, CO 53
Structures
Consider a C struct typedef struct _tree_t {
int val; /* the value of this node. */
struct _tree_t *left; /* ptr to the
left child. */
struct _tree_t *right; /* ptr to the
right child. */
} tree_t;
MIPS Assembly 54 CS220, CO
Structures
There is no equivalent construct in SPIM.
User to manage things at lower level
3-continuous words to represent 3-values: val, left, right
Need to keep track of each word and what it represents
MIPS Assembly 55 CS220, CO
Structures Suppose RS contains the address (base) of a structure variable foo
Value of val will be at offset 0
foo->val : (RS) + 0
Value of left will be at offset 4, since
int is of size 4 bytes
foo->left: (RS) + 4
Value of right will be at offset 8, for
pointers (addresses) are 4 bytes
foo->right: (RS) + 8
MIPS Assembly 56 CS220, CO
Structures
# MIPS assembly : C equivalent:
lw $s0, 0($t1) # a = foo->val;
lw $s1, 4($t1) # b = foo->left;
lw $s2, 8($t1) # c = foo->right;
sw $s0, 0($t1) # foo->val = a;
sw $s1, 4($t1) # foo->left = b;
sw $s2, 8($t1) # foo->right = c;
MIPS Assembly 57 CS220, CO
Structures
We do not have the luxury of type-checking to prevent silly mistakes, e.g. mixing val and left/right fields
Comments are essential when writing complex programs involving arrays and structures
MIPS Assembly 58 CS220, CO
Structures
Changing structure layout invalidates all the code using the structure
Any part that depends on the layout of a structure should mention this fact explicitly
if you change the representation later
you'll know exactly which parts to
change.
MIPS Assembly 59 CS220, CO
Dynamic Memory
Structures are most useful when we can dynamically allocate/de-allocate
System call sbrk provided to
“allocate” memory
MIPS Assembly 60 CS220, CO
Dynamic Memory
li $a0, 8 # need 8 bytes
li $v0, 9 # 9 is sbrk code
syscall
Extends the data segment by amount in
$a0
Returns the location of
previous end in $v0
MIPS Assembly 61 CS220, CO
Stack
Dynamic
– – – – – – – – – – – – –
Static
Text Segment
Reserved
Data
Segm
ent E
nd
$v0
$a0
Dynamic Memory
System call sbrk can only allocate
memory, does not give it back
We do not have free or delete at
machine level
Provide libraries for explicit
management
Typically operating system provides wrapper system calls, e.g. malloc/free
MIPS Assembly 62 CS220, CO