Beruflich Dokumente
Kultur Dokumente
Assembly Programming
Amey Karkare karkare@cse.iitk.ac.in Department of CSE, IIT Kanpur
MIPS Assembly
CS220, CO
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
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
xxN.c . .
xx2.c
compiler
xxN.s . .
xx2.s
assembler
xxN.o . . linker
xx2.o
. .
compiler
. .
assembler assembler
executable
xx1.c
compiler
xx1.s
xx1.o
Program Libraries
MIPS Assembly
CS220, CO
xxN.s . .
assembler
xxN.o . . executable
. .
assembler assembler
linker
xx2.s
xx1.s
xx2.o
xx1.o
Program Libraries
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
10
MIPS Assembly
CS220, CO
11
Assembler
Assembler translates assembly program into object code. Object code contains binary machine instructions.
plus some bookkeeping information.
MIPS Assembly
CS220, CO
12
Linker
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.
MIPS Assembly
CS220, CO
14
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
Register Number
Common Name
Usage
Hardwired value 0. Any writes to this register are ignored. Assembler temporary. Function result registers Function argument registers that hold the first four arguments. T emporary registers. Saved registers to use freely. Reserved for use by the operating system kernel and for exception return. Global pointer. Stack pointer. Frame Pointer. Return address register.
CS220, CO 16
Comparison Instructions
seq, sne, sge(u), sgt(u), sle(u), slt(u)
MIPS Assembly
CS220, CO
17
Exception Handling
rfe, syscall, break, nop
MIPS Assembly
CS220, CO
18
Addressing Modes
Bare Machine: imm(reg)
MIPS Assembly
CS220, CO
19
Addressing Modes
Virtual Machine:
Format (reg) imm imm(reg) label label imm Label imm(reg) Address Computation Contents of reg Immediate constant Immediate + contents of reg Address of label Address of label immediate Address of label (immdiate + content of reg)
MIPS Assembly
CS220, CO
20
System Calls
Frequently used system calls
Service Print int Print string Read int Read string Code 1 4 5 8 Argument a0 a0 -a0(address) a1(length) Result --v0 --
sbrk exit
9 10
a0(amount) --
v0 --
MIPS Assembly
CS220, CO
21
# # # # # # #
Compute the factorial of given number and print the result $t0: holds the input $t1: holds the result: Factorial($t0)
MIPS Assembly
CS220, CO
22
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"
CS220, CO 23
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.
MIPS Assembly
CS220, CO
25
Layout of Memory
Stack
Dynamic
Static
Data Segment
Text Segment
Reserved
MIPS Assembly
CS220, CO
26
$fp
Stack Grows
$sp
Local V ariables
$fp points to the first word in the active procedures stack frame. $sp points to the last word in the active procedures stack frame. Arguments 1. . . 4 are stored in $a0. . . $a3.
MIPS Assembly
CS220, CO
27
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
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.
MIPS Assembly
CS220, CO
29
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 callees stack frame. Jump to Callee Execute JAL instruction. Stores the return address in $ra.
MIPS Assembly
CS220, CO
30
MIPS Assembly
CS220, CO
31
MIPS Assembly
CS220, CO
32
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
# # # # # # #
Compute the factorial of given number and print the result $t0: holds the input $t1: holds the result: Factorial($t0)
MIPS Assembly
CS220, CO
34
# call the recursive factorial jal fact # result in $v0, copy to $t1 move $t1, $v0
MIPS Assembly
CS220, CO
# 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
MIPS Assembly
CS220, CO
36
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
MIPS Assembly
CS220, CO
37
CS220, CO
38
CS220, CO
39
CS220, CO
40
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
CS220, CO
41
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
CS220, CO
42
1004005 1004006
Address Computation
vowels[k] = vowels + k pow2[k] = pow2 + k*4
2
MIPS Assembly
CS220, CO
43
Array Bounds
.data vowels: .byte a, e, i, o, u pow2: .word 1, 2, 4, 8, 16, 32, 64
1004010
1004011 1004012
Unpredictable!
CS220, CO
MIPS Assembly
44
Strings
.data vowels1: .ascii AEIOU vowels2: .asciiz AEIOU
Memory alloc for vowels1
A E I O U A
E I O U \0
1004010
MIPS Assembly
CS220, CO
45
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
MIPS Assembly
CS220, CO
46
MIPS Assembly
CS220, CO
47
(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
(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
(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
(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
(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
CS220, CO
54
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
CS220, CO
55
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
CS220, CO
56
Structures
# MIPS assembly lw $s0, 0($t1) lw $s1, 4($t1) lw $s2, 8($t1) : # # # C a b c equivalent: = foo->val; = foo->left; = foo->right;
Structures
We do not have the luxury of typechecking 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
CS220, CO
58
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.
CS220, CO 59
MIPS Assembly
Dynamic Memory
Structures are most useful when we can dynamically allocate/deallocate System call sbrk provided to allocate memory
MIPS Assembly
CS220, CO
60
Dynamic Memory
li $a0, 8 # need 8 bytes li $v0, 9 # 9 is sbrk code syscall
Stack
$a0
Extends the data segment by amount in $a0 Returns the location of previous end in $v0
MIPS Assembly
Dynamic
Static
$v0
Text Segment
Reserved
61
CS220, CO
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
CS220, CO 62
MIPS Assembly