Docsity

Practice Assignment with Solutions - Computer Architecture | CS 472, Assignments of Computer Architecture and Organization

Toggle sidebar
bg1
int createArray(int x) {
int i, b, a[12];
for(i=0; i<12; i++) {
a[i] = i;
}
b = sum(a, x);
return b;
}
createArray:
addiu $sp, $sp, -80 #14 local variables, 4 word arg. build,
#$ra, pad above $ra
add $t0, $zero, $zero #init i to 0
addi $t3, $zero, 12 #$t3 holds value to check i against to
#determine end of loop
addi $t2, $sp, 32 #t2 starts out holding location of a[0]
loop: sw $t0, 0($t2) #store i into array
addi $t0, $t0, 1 #increment i
addi $t2, $t2, 4 #$t2 points to next place in the array
bne $t0, $t3, loop #branch if i < 12
sw $ra, 16($sp) #need to call sum
sw $a0, 80($sp) #store our argument in slot provided by our caller
addi $a0, $sp, 32 #put starting location of a[0] into $a0
lw $a1, 80($sp) #put arg0 of createArray into $a1 for sum
jal sum
sw $v0, 28($sp) #store return in b (not really necessary)
#return from sum is also our return, leave in $v0
lw $ra, 16($sp)
addiu $sp, $sp, 80
jr $ra
sp + 4
sp
sp + 8
sp + 12
sp + 16
sp + 20
sp + 24
sp + 28
sp + 32
sp + 36
sp + 40
sp + 52
sp + 56
sp + 60
sp + 64
sp + 68
sp + 72
sp + 76
sp + 44
sp + 48
old sp
(arg0)
(arg1)
(arg2)
a[0]
ra
padding
b
i
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
a[10]
a[11]
(arg3)
(arg0)
(arg1)
(arg2)
(arg3)
createArray
stack frame
store x here
local / temp
area starts
on a
double word
boundary
(24 is
divisible by
8)
See my MIPS solution
below. I provided stack
storage for local variables I
didn't optimize anything.
Of course, there are other
solutions using different
registers and ordering the
local variables differently
than I did or not providing
storage in the stack for local
variables when you didn't
have to.
pf2

Partial preview of the text

Download Practice Assignment with Solutions - Computer Architecture | CS 472 and more Assignments Computer Architecture and Organization in PDF only on Docsity!

int createArray(int x) {

int i, b, a[12];

for(i=0; i<12; i++) {

a[i] = i;

b = sum(a, x);

return b;

createArray: addiu $sp, $sp, -80 #14 local variables, 4 word arg. build, #$ra, pad above $ra add $t0, $zero, $zero #init i to 0 addi $t3, $zero, 12 #$t3 holds value to check i against to #determine end of loop addi $t2, $sp, 32 #t2 starts out holding location of a[0] loop: sw $t0, 0($t2) #store i into array addi $t0, $t0, 1 #increment i addi $t2, $t2, 4 #$t2 points to next place in the array bne $t0, $t3, loop #branch if i < 12 sw $ra, 16($sp) #need to call sum sw $a0, 80($sp) #store our argument in slot provided by our caller addi $a0, $sp, 32 #put starting location of a[0] into $a lw $a1, 80($sp) #put arg0 of createArray into $a1 for sum jal sum sw $v0, 28($sp) #store return in b (not really necessary) #return from sum is also our return, leave in $v lw $ra, 16($sp) addiu $sp, $sp, 80 jr $ra

sp + 4

sp

sp + 8

sp + 12

sp + 16

sp + 20

sp + 24

sp + 28

sp + 32

sp + 36

sp + 40

sp + 52

sp + 56

sp + 60

sp + 64

sp + 68

sp + 72

sp + 76

sp + 44

sp + 48

old sp

(arg0)

(arg1)

(arg2)

a[0]

ra

padding

b

i

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

a[10]

a[11]

(arg3)

(arg0)

(arg1)

(arg2)

(arg3)

createArray

stack frame

store x here

local / temp

area starts

on a

double word

boundary

(24 is

divisible by

See my MIPS solution

below. I provided stack

storage for local variables I

didn't optimize anything.

Of course, there are other

solutions using different

registers and ordering the

local variables differently

than I did or not providing

storage in the stack for local

variables when you didn't

have to.

int sum(int arr[], int n) {

int i, total=0;

for(i=0; i<=n; i++) {

total = addThese(total, arr[i]);

return total;

sum: addiu $sp, $sp, -32 #2 local variables, 4 word arg. build, #$ra, pad above $ra add $t0, $zero, $zero #init i to 0 add $t1, $zero, $zero #init total to 0 sw $t1, 28($sp) #store in total's mem. slot sw $ra, 16($sp) loop2: sw $t0, 24($sp) #save i across function call sw $a0, 32($sp) #save arg0 (mem loc. of element of arr[]) in slot #allotted by our caller sw $a1, 36($sp) #save arg1 (n) in slot allotted by our caller lw $a0, 28($sp) #put total into arg lw $a1, 32($sp) #put mem loc of next element of arr into arg lw $a1, 0($a1) #put contents of that mem loc into arg jal addThese sw $v0, 28($sp) #store result into total lw $a0, 32($sp) #restore array pointer addi $a0, $a0, 4 #add 4 to array pointer lw $t0, 24($sp) #restore i addi $t0, $t0, 1 #add 1 to i lw $a1, 36($sp) #restore n addi $t2, $a1, 1 #add 1 to n bne $t0, $t2, loop2 #if i <= n branch to loop lw $v0, 28($sp) #put total into return reg. $v lw $ra, 16($sp) addiu $sp, $sp, 32 jr $ra

int addThese(int c, int d) {

return c+d;

addThese: #simple leaf procedure, no stack frame necessary add $v0, $a0, $a jr $ra

sp + 4

sp

sp + 8

sp + 12

sp + 16

sp + 20

sp + 24

sp + 28

old sp

(arg0)

(arg1)

(arg2)

(arg0)

ra

padding

total

i

(arg3)

(arg1)

sum

stack frame

top of

createArray

stack frame