Beruflich Dokumente
Kultur Dokumente
Dealing with arrays Same idea using pointers Moving everything you can outside the loop
Summer 2002
Summer 2002
Summer 2002
Write HLL control statements using set instructions, b and conditional branches
Be able to do if, while, do while, for
lr, etc. are procedure calls jump and store the return address
Summer 2002
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
Remember to:
Surf the web Embed program in a loop of
Get data Calculate Display data
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
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
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
Copy the save code and replace sws with lws Deallocate
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
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.
Summer 2002
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
main:
LC:
Summer 2002
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
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
S1
expr
S1 end:
code for expr result in $t1 beq $t1,$0,end code for stmt1 b S1
=0 stmt1
end
Summer 2002
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
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
To pass this array by reference (the function can fill it or just use it)
Summer 2002
Summer 2002