Sie sind auf Seite 1von 17

Procedures and arrays in MIPS/SPIM

Dealing with arrays Same idea using pointers Moving everything you can outside the loop

Summer 2002

ICOM 4206 Functions and arrays

Comparing array and pointer code


MIPS code for clearing an array using indices move $t0,$zero loop1: add $t1,$t0,$t0 add $t1,$t1,$t1 add $t2,$a0,$t1 sw $zero,0($t2) addi $t0,$t0,1 slt $t3,$t0,$a1 bne $t3,$zero,$loop1
MIPS code for clearing an array using pointers move $t0,$a0 loop1: sw $zero,0($t0) addi $t0,$t0,4 add $t1,$a1,$a1 add $t1,$t1,$t1 add $t2,$a0,$t1 slt $t3,$t0,$t2 bne $t3,$zero,$loop1

Summer 2002

ICOM 4206 Functions and arrays

Optimizing by moving unnecessary code outside of the loop


MIPS code for clearing an array using pointers move $t0,$a0 loop1: sw $zero,0($t0) addi $t0,$t0,4 add $t1,$a1,$a1 add $t1,$t1,$t1 add $t2,$a0,$t1 slt $t3,$t0,$t2 bne $t3,$zero,$loop1
Faster MIPS code for clearing an array using pointers move $t0,$a0 add $t1,$a1,$a1 add $t1,$t1,$t1 add $t2,$a0,$t1 loop1: sw $zero,0($t0) addi $t0,$t0,4 slt $t3,$t0,$t2 bne $t3,$zero,$loop1

Summer 2002

ICOM 4206 Functions and arrays

What you need to be able to do


Run the assembler/simulator (SPIM)
Try it with a known good file first test1.asm from ece/~bvelez is a good one

Write code for simple expressions


Try it with registers only - $s4=($s2-$s1)-($s3-4) and a few variations again it is like working with your high school handheld

Write HLL control statements using set instructions, b and conditional branches
Be able to do if, while, do while, for

Write and call functions


Simple leaf function Factorial this shows recursive functions

lr, etc. are procedure calls jump and store the return address

Summer 2002

ICOM 4206 Functions and arrays

New assignment
Floating point in SPIM
Will be graded in happy hour
Happy hour is diagnostic as well as grading, if you arent sure, ask

You need to know SPIM and how to operate it


Breakpoints Running a program Interpreting memory and register contents Explaining floating-point formats to some extent

Remember to:
Surf the web Embed program in a loop of
Get data Calculate Display data

And remember to always use prompts for input data


Summer 2002 ICOM 4206 Functions and arrays

Programming functions
Leaf functions
These do not call other functions You can use registers $t0-9 At the beginning
Allocate stack if you need more space

Nonleaf functions
These do call other functions At the beginning
Allocate stack
for workspace and registers

After you are done


Place return variables in $v0-1 Deallocate stack if you used it jr $ra

Save $ra and $a0-3 Do computation Restore $ra and $a0-3 Place return variables in $v0-1 Deallocate stack, you used it Jr $ra

Summer 2002

ICOM 4206 Functions and arrays

Writing a leaf function


int sumsq(int a{}, int n) { int sum=0; for(int i=0;I,n;i=i+1) {sum=sum+a[i]}; return sum; } .text .align move move sll move lw mul addu addui addui bgt jr

sumsq:

Loop:

2 $t0,0 # t0 will contain 4i $t1,$a0 # $t1 will contain the address of a[I] $t2,$a1,2 # $t2 will contain 4*n for comparison $v0,0 # might as well keep sum in $v0 for returning $t3,0($t2) # This is a[I] $t3,$t3,$t3 # a[I] squared $v0,$v0,$t3 # adding to sum $t1,$t1,4 # stepping pointer to a[I] $to,$to,4 # stepping 4*I $t2,$t0,Loop # until 4*I >= 4*n $ra ICOM 4206 Functions and arrays

Summer 2002

Making a stack
Decide what you need
Data to be saved/restored
$ra and $fp

Temporary data How much space

Building the save/restore code


Allocate Save
Then insert the business code here

Copy the save code and replace sws with lws Deallocate

Building a tester for functions


Can be a separate piece of code in same program run it from simulator menu
Summer 2002 ICOM 4206 Functions and arrays

Writing and calling a nonleaf factorial


The HLL (C) code for factorial main() { printf(The factorial of 10 is %d\n,fact(10)); } int fact(int n) { if(n < 1 ) return(1); else return(n * fact (n 1); } The SPIM code for fact() .text subu sw sw addu sw lw bgtz li j lw subu move jal lw mul lw lw addu j

fact:

L2:

L1:

$sp, $sp, 32 $ra, 20($sp) $fp, 16($fp) $fp, 28 $a0, 0($fp) $v0, 0($fp) $v0, L2 $$v0,1 L1 $v1, 0($fp) $v0, $v1, 1 $a0, $v0 fact $v1, 0($fp) $v0, $v0, $v1 $ra, 20($sp) $fp, 16($fp) $sp,$sp,32 $ra

Summer 2002

ICOM 4206 Functions and arrays

Calling functions
Before calling the function
Remember
$t0-9 may be destroyed dont depend on them in code using a function call

Passing parameters
Up to four are passed in $a0-3 Additional ones are passed on the stack

Calling the function is done by jal (this has no parameters and always uses $ra Retrieving the results
These are always in $v0-1 or on the stack if too large.

Passing parameters by reference takes a little thought.


You should understand C or C++ before trying it.

Summer 2002

ICOM 4206 Functions and arrays

The main() function


The HLL (C) code for factorial main() { printf(The factorial of 10 is %d\n); } int fact(int n) { if(n < 1 ) return(1); else return(n * fact (n 1); } Old sp New fp Old ra Old fp LC: The MIPS code for main() .text .globl subu sw sw addu li jal la move jal lw lw addu jr .rdata .asciiz

main:

main $sp,$sp,32 $ra,20($sp) $fp,16($fp) $fp,$sp,28($sp) $a0,10 fact $a0,LC $a1,$v0 printf $ra,20($sp) $fp,16($fp) $sp,$sp,32 $ra The..

New sp
Summer 2002 ICOM 4206 Functions and arrays

The main function again, with comments


The MIPS code for main() .text .globl subu sw sw addu li jal la move jal lw lw addu jr .rdata .asciiz # what follows is text main # So windows can find main $sp,$sp,32 # Allocating 32 bytes (8 words) on the stack $ra,20($sp) # Saving the return address $fp,16($sp) # Saving the old frame pointer, not to worry $fp,28($sp) # The new frame pointer points to new stack $a0,10 # Passing the value 10 into fact(10) fact # Calling fact(10) $a0,LC # la is a pseudoop that loads address, not value $a1,$v0 # loading the return value from fact () into a1 printf # Calling printf(format, data) $ra,20($sp) # restoring $ra note this is copied from above $fp,16($sp) # restoring $fp type from the outside in $sp,$sp,32 # deallocating what we allocated $ra # adios, sayonara # .rdata read-only data The.. # The format string itself

main:

LC:

Summer 2002

ICOM 4206 Functions and arrays

The fact function again, with comments


The SPIM code for fact() .text subu sw sw addu sw lw bgtz li j lw subu move jal lw mul lw lw addu j

fact:

L2:

L1:

$sp, $sp, 32 # Allocating 8 words on the stack $ra, 20($sp) # Saving the return address $fp, 16($sp) # Saving the old frame pointer $fp, $sp,28 # fp is needed for reentrant code $a0, 0($fp) # saving what we were called with $v0, 0($fp) # and loading what we may return $v0, L2 # L2 if this is not fact(0) $v0,1 # This is fact(0) get ready to return 1 L1 # go to the clean up and exit code $v1, 0($fp) # Now the recursive call, $v1 is a temporary $v0, $v1, 1 # setting up for the recursive fact (n-1) $a0, $v0 # fact # calling fact(n-1) $v1, 0($fp) # getting n (temporary) back $v0, $v0, $v1 # multiplying by it $ra, 20($sp) # the function return sequence $fp, 16($sp) $sp,$sp,32 # Deallocating $ra # And going home
ICOM 4206 Functions and arrays

Summer 2002

Making control statements

if (expr) stmt1 else stmt2


expr s2: end: =0 stmt1 s2

code for expr seq $t1,$t1,0 bne $t1,s2 code for stmt1 b end code for stmt2

Note: $s2 and s2 are not the same why work out the logic for the seq and bne, door prize for simplification Now work out the flow chart and then the code For while and dowhile

stmt2 end Summer 2002

ICOM 4206 Functions and arrays

Making control statements - while

while (expr) stmt1

S1

expr

S1 end:

code for expr result in $t1 beq $t1,$0,end code for stmt1 b S1

=0 stmt1

end

Summer 2002

ICOM 4206 Functions and arrays

Arrays
The tools
The la instruction actually a pseudo-op
This loads the address of an array rather than the first element into a register

How you use the address you just put there


Nondestructive use
Constant subscript use displacement Nonconstant subscript use another temporary register

Passing an array
Passing the pointer in a $a register means the function modifies your only copy If you want to do this, for example if the function just reads the array, OK

Returning an array
This is a bit trickier you have to allocate the space before you call the function for C or Java programmers, you are using the systems new or malloc beyond scope of what we are doing

Allocating space for an array (but not initializing)


Name: .space la size*n $a0,Name #example .space 80 for a 20 word array

To pass this array by reference (the function can fill it or just use it)

Summer 2002

ICOM 4206 Functions and arrays

Preparations and announcements


Lab assignment 1 is on the webpage
Due Monday (now Tuesday)

Quiz I is also now Tuesday


Some dependence on the lab Open notes and instruction list and notes printouts No textbook copies (not fair to those who dont have them) No palm pilots or celulares (or other communicating gadgets)

Accounts on amadeus will be available after today


Some amadeus time will be available mostly just before and after class If you had accounts before of the normal type they should still be valid If your name is on the sheet, please pick a name

Textbook (Hennessy)copies will be available at $2 per chapter

Summer 2002

ICOM 4206 Functions and arrays

Das könnte Ihnen auch gefallen