Escolar Documentos
Profissional Documentos
Cultura Documentos
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
compiler
assembler
. .
compiler
. .
assembler assembler
executable
compiler
MIPS Assembly
CS220, CO
assembler
. .
assembler assembler
linker
Program Libraries
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
MIPS Assembly
CS220, CO
10
Assembler
Assembler translates assembly program into object code. Object code contains binary machine instructions.
plus some bookkeeping information.
MIPS Assembly
CS220, CO
11
Linker
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
12
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
13
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
14
Register Number
Common Name
Usage
0 1 23 47
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 15
Comparison Instructions
seq, sne, sge(u), sgt(u), sle(u), slt(u)
MIPS Assembly
CS220, CO
16
Exception Handling
rfe, syscall, break, nop
MIPS Assembly
CS220, CO
17
Addressing Modes
Bare Machine: imm(reg)
MIPS Assembly
CS220, CO
18
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
19
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
20
# # # # # # #
MIPS Assembly
CS220, CO
21
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 22
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
23
Procedure Calls
In assembly language, programmer has to explicitly maintain most of the procedures 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
24
Layout of Memory
Stack
Dynamic
Static
Data Segment
CS220, CO
25
$fp
Stack Grows
$sp
Local Variables
$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
26
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
27
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
28
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
29
MIPS Assembly
CS220, CO
30
MIPS Assembly
CS220, CO
31
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
32
# # # # # # #
MIPS Assembly
CS220, CO
33
# 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
35
# 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
MIPS Assembly
CS220, CO
36
CS220, CO
37
CS220, CO
38
CS220, CO
39