Docsity

MIPS Assembly Problem Set 3 Solutions, Assignments of Computer Architecture and Organization

Toggle sidebar
bg1
COMP 411
Problem Set #3 Solutions
Note: There are multiple approaches/solutions to many of the problems.
Problem 1: (30 points total, 5 points each)
A.) lw $t1,x #load x
lw $t2,y #load y
sub $t2,$t2,$t1 #subtract x from y
sw $t2,y #store result in y
B.) la $t1,a #load address of array a
lw $t2,i #load i (location in array)
addi $t3,$t2,-1
sll $t3,$t3,2
add $t3,$t1,$t3 #computing offset (i-1)
lw $t4,0($t3) #a[i-1]
addi $t3,$t2,-2
sll $t3,$t3,2
add $t3,$t1,$t3 #computing offset (i-2)
lw $t5,0($t3) #a[i-2]
add $t5,$t5,$t4 #a[i-1] + a[i-2]
sll $t3,$t2,2
add $t3,$t1,$t3 #location of a[i]
sw $t5,($t3) #save result of a[i-1] + a[i-2]
C.) lw $t0,x #load value of x in $t0
lw $t1,y #load value of y in $t1
slt $t2,$t1,$t0 #if y < x, $t2 = 1, if not, $t2=0
beq $t2,$0, else
sub $t0,$t0,$t1 #x-y
beq $0,$0,end
else:
sub $t0,$t1,$t0 #y-x
beq $0,$0,end
end:
sw $t0, x #store result in x
pf3
pf4
pf5

Partial preview of the text

Download MIPS Assembly Problem Set 3 Solutions and more Assignments Computer Architecture and Organization in PDF only on Docsity!

COMP 411

Problem Set #3 Solutions Note: There are multiple approaches/solutions to many of the problems.

Problem 1: (30 points total, 5 points each)

A.) lw $t1,x #load x lw $t2,y #load y sub $t2,$t2,$t1 #subtract x from y sw $t2,y #store result in y

B.) la $t1,a #load address of array a lw $t2,i #load i (location in array) addi $t3,$t2,- sll $t3,$t3, add $t3,$t1,$t3 #computing offset (i-1) lw $t4,0($t3) #a[i-1] addi $t3,$t2,- sll $t3,$t3, add $t3,$t1,$t3 #computing offset (i-2) lw $t5,0($t3) #a[i-2] add $t5,$t5,$t4 #a[i-1] + a[i-2] sll $t3,$t2, add $t3,$t1,$t3 #location of a[i] sw $t5,($t3) #save result of a[i-1] + a[i-2]

C.) lw $t0,x #load value of x in $t lw $t1,y #load value of y in $t slt $t2,$t1,$t0 #if y < x, $t2 = 1, if not, $t2= beq $t2,$0, else sub $t0,$t0,$t1 #x-y beq $0,$0,end else: sub $t0,$t1,$t0 #y-x beq $0,$0,end end: sw $t0, x #store result in x

D.)

lw $t0, j #load j in $t bne $t0,$0,loop #while (j! = 0), loop beq $0,$0,end #if j = 0, go to end loop: sll $t0,$t0,1 #j = j << 1 bne $t0,$0,loop #while (j! = 0), loop end: sw $t0,j #store result in j

E.)

la $t0,a #load address of beg. of array in $t lw $t1,i #load value of i in $t1 (9) addi $t2,$0,-1 #value of -1 in $t bne $t1,$t2,loop #until i reaches -1 (must not jump if i = 0), loop loop: sll $t3,$t1,2 #scaling to word size add $t3,$t0,$t3 #saving place of effective address in array to $t add $t4,$t1,$t1 #i+i addi $t4,$t4,1 #result + 1 sw $t4,($t3) #store result in a[i] addi $t1,$t1,-1 #decrement i bne $t1,$t2,loop #until i reaches -1 (must not jump if i = 0), loop end: b end

F.) la $t1,a #load address of beg. of array a in $t lw $t2,x #load value of x in $t sll $t2,$t2, add $t2,$t1,$t2 #go to effective address in array lw $t3,($t2) #load in value at effective address in $t sll $t2,$t3, add $t2,$t1,$t2 #go to effective address in array sw $t3,($t2) #store a[a[x]]

Problem 2: (20 points total, 5 points each)

A.) and $t0,$t0,$0 #clear $t and $t1,$t1,$0 #clear $t and $t2,$t2,$0 #clear $t and $t3,$t3,$0 #clear $t

Problem 4: (20 points total)

Code: ps-3-4.s

.globl stopa .data stopa: .word 40 #end of array (bytes) a: .word -3,17,12,101,-51,42,17,2,0,-

.globl main .text

main: la $t0,a #keep the current address of the array in $t lw $t1,stopa add $t1,$t1,$t0 #address of end of array add $t2,$0,$0 #$t2 = value of index in array; later result of andi add $t3,$0,$0 #$t3 = total of odd numbers in array

loop: lw $t2,($t0) #loads the value in the memory location = $t andi $t2,$t2, add $t3,$t2,$t3 #if value was odd, add to total of odd numbers addi $t0,$t0,4 #increment index in array bne $t0,$t1,loop #if haven't reached end of array, repeat loop

end: j $

SPIM Output:

Note: Register $t3 contains the result of the count