60 likes | 131 Views
Solution 2 nd Exam. f = f+A [2]; LW $ t0, 8($s6) ADD $s0, $s0, $ t0 B[8] = A[ i ] + A[j]; SLL $t0, $s3, 2 ADD $t0, $s6, $t0 LW $t0, 0($t0) SLL $t1, $s4, 2 ADD $t1, $s6, $t1 LW $t1, 0($t1) ADD $t2, $t0, $t1 SW $t2, 32($s7). for (i = 0; i < 10 ; i++)
E N D
f=f+A[2]; LW $t0, 8($s6) ADD $s0, $s0, $t0 • B[8] = A[i] + A[j]; SLL $t0, $s3, 2 ADD $t0, $s6, $t0 LW $t0, 0($t0) SLL $t1, $s4, 2 ADD $t1, $s6, $t1 LW $t1, 0($t1) ADD $t2, $t0, $t1 SW $t2, 32($s7)
for (i = 0; i < 10; i++) a = a + b + 1; add $t0, $zero, $zero LOOP: slti $t1, $t0, 10 beq $t1, $zero, EXIT add $s0, $s0, $s1 addi $s0, $s0, 1 addi $t0, $t0, 1 j LOOP EXIT: 7 instructions total number is 1 + 10*6 +2 = 63
int compare(int a, int b) { if (a < b) return 1; else return 0; } COMPARE: slt $t0, $a0, $a1 addi $v0, $zero, 1 bne$t0, $zero, EXIT addi $v0, $zero, 0 EXIT: jr$ra
int compare(int a, int b) { if (subit (a, b) <0) return 1; else return 0; } int subit (int a, int b) { return a-b; }
COMPARE: addi $sp, $sp, -4 sw $ra, 0($sp) jal SUBIT addi $t1, $zero, 1 slti $t0, $v0, 0 #subit(a,b)<0 -> $t0=1 bne $t0, $zero, EXIT addi $t1, $zero, 0 EXIT: add $v0, $t1, $zero lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra SUBIT: sub $v0, $a0, $a1 jr $ra