Você está na página 1de 39

Introduction to Computer Organization

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

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

Typical Flow: High Level Language Program

xxN.c . . xx2.c xx1.c

compiler

xxN.s . . xx2.s xx1.s

assembler

xxN.o . . linker xx2.o xx1.o


Program Libraries

. .
compiler

. .
assembler assembler

executable

compiler

MIPS Assembly

CS220, CO

Typical Flow: Assembly Program

xxN.s . . xx2.s xx1.s

assembler

xxN.o . . xx2.o xx1.o executable

. .
assembler assembler

linker

Program Libraries

MIPS Assembly

CS220, CO

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

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

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

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

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

10

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

11

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

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.

No direct manipulation of memory contents.

MIPS Assembly

CS220, CO

13

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

14

MIPS Register Set

Register Number

Common Name

Usage

0 1 23 47

zero at v0v1 a0a3

815,2425 1623 2627


28 29 30 31
MIPS Assembly

t0t9 s0s7 k0k1


gp sp fp ra

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

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

16

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

17

Addressing Modes
Bare Machine: imm(reg)

imm: Immediate Constant reg: Register containing address

Address computation: Contents of


reg + imm constant

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

Iterative Factorial Function

# # # # # # #

Compute the factorial of given number and print the result


$t0: holds the input $t1: holds the result: Factorial($t0)

MIPS Assembly

CS220, CO

21

Iterative Factorial Function


# 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
MIPS Assembly

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.

When procedure A calls procedure B, programmer has to write code to:


save As environment on the stack jump to B when B returns, restore As environment from stack

MIPS Assembly

CS220, CO

24

Layout of Memory

Stack

Dynamic

Static

Data Segment

Text Segment Reserved


MIPS Assembly

CS220, CO

25

Layout of Stack Frame

$fp

... Arg # 6 Arg #5 Saved Registers

High Memory Address

Stack Grows

$sp

Local Variables

Lower Memory Address

$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

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

27

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

28

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 callees stack frame.

Jump to Callee
Execute JAL instruction. Stores the return address in $ra.

MIPS Assembly

CS220, CO

29

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

30

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 callees stack frame). Return by jumping to $ra address.

MIPS Assembly

CS220, CO

31

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

32

Recursive Factorial Function

# # # # # # #

Compute the factorial of given number and print the result


$t0: holds the input $t1: holds the result: Factorial($t0)

MIPS Assembly

CS220, CO

33

Recursive Factorial Function


# 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 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"
34

# call the recursive factorial jal fact # result in $v0, copy to $t1 move $t1, $v0

MIPS Assembly

CS220, CO

Recursive Factorial Function

# 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

Recursive Factorial Function

# 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

Recursive Factorial Function


# 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
MIPS Assembly

CS220, CO

37

Recursive Factorial Function:


# 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
MIPS Assembly

CS220, CO

38

Recursive Factorial Function:


# 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
MIPS Assembly

CS220, CO

39