Sie sind auf Seite 1von 62

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?

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

T ypical Flow: High Level Language Program

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

T ypical Flow: Assembly Program

xxN.s . .

assembler

xxN.o . . executable

. .
assembler assembler

linker

xx2.s
xx1.s

xx2.o
xx1.o
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

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 1 23 47 815,2425 1623 2627 28 29 30 31


MIPS Assembly

zero at v0v1 a0a3 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 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

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

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

22

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 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.

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

25

Layout of Memory

Stack

Dynamic

Static

Data Segment

Text Segment
Reserved
MIPS Assembly

CS220, CO

26

Layout of Stack Frame

$fp

... Arg # 6 Arg #5 Saved Registers

High Memory Address

Stack Grows

$sp

Local V ariables

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

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 callees 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 callees 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

# # # # # # #

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

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"
35

# 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

36

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

37

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

38

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

39

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

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

Array with Initialization


.data vowels: .byte a, e, i, o, u pow2: .word 1, 2, 4, 8, 16, 32, 64

Memory alloc for vowels

1004000 1004001 1004002 1004003


1004004

97 101 105 111 117

1004005 1004006

Address Computation
vowels[k] = vowels + k pow2[k] = pow2 + k*4

1004007 1004008 alloc for pow2 1004009 1004010 1004011 1004012

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

Memory alloc for vowels

1004000 1004001 1004002 1004003 1004004 1004005 1004006

97 101 105 111 117

1004007 la $t0, vowels lb $t1, 5($t0) # t1 = vowels[5] 1004008 1004009

alloc for pow2

What is the value in $t1?

1004010

1004011 1004012

Unpredictable!
CS220, CO

MIPS Assembly

44

Strings
.data vowels1: .ascii AEIOU vowels2: .asciiz AEIOU
Memory alloc for vowels1

1004000 1004001 1004002 1004003 1004004 1004005 1004006 1004007 1004008


1004009

A E I O U A

alloc for vowels2

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

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, li $a1, li $v0, syscall la $t1, la $t2,
MIPS Assembly

string_space 1024 8 # read_string string_space string_space


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

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;

sw $s0, 0($t1) sw $s1, 4($t1) sw $s2, 8($t1)


MIPS Assembly

# foo->val = a; # foo->left = b; # foo->right = c;


CS220, CO 57

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

Data Segment End

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

Das könnte Ihnen auch gefallen