Question: C to MIPS: How would I go about converting the following if-else statement? I know how to do simpler ones, but this one is more

C to MIPS: How would I go about converting the following if-else statement? I know how to do simpler ones, but this one is more complex. Full C code of nested for loop and if-else below, assembly (nested loops) I have so far also included.

C:

for(i=0; i= y1 && i <= y2 && j >= x1 && j <= x2){ printf("%d %d %d ", r, g, b); //if row/column is inside box, print original rgb values }else{

printf("%d %d %d ", gray, gray, gray);//if row/column is outside box, print gray values } } }

MIPS:

loop1: bge $s0, $t1, end # for (i = 0; i < rows; i++) addi $s0, $s0, 1 # i++ li $t9, 0 # resets j to 0 j loop2 # execute inside loop loop2: bge $t9, $t0, loop1 # for (j = 0; j < columns; j++)

li $v0, 5 # reads red (integer) from the user syscall add $a0, $v0, $0 # stores red in $a0 li $v0, 5 # reads green (integer) from the user syscall add $a1, $v0, $0 # stores green in $a1 li $v0, 5 # reads blue (integer) from the user syscall add $a2, $v0, $0 # stores blue in $a2

jal rgb_to_gray #procedure to calculate gray value #insert if else statement here? addi $v0, $0, 1 # call to print grayscale value (1st time) add $a0, $0, $t3 # store value returned from rgb_to_gray in $a0 syscall addi $v0, $0, 4 # prepare to print new line la, $a0, newline # print new line addi $v0, $0, 1 # call to print grayscale value (2nd time) add $a0, $0, $t3 # store value returned from rgb_to_gray in $a0 syscall addi $v0, $0, 4 # prepare to print new line la, $a0, newline # print new line addi $v0, $0, 1 # call to print grayscale value (3rd time) add $a0, $0, $t3 # store value returned from rgb_to_gray in $a0 syscall

addi $v0, $0, 4 # prepare to print new line la, $a0, newline # print new line syscall addi $t9, $t9, 1 # increment j by 1 j loop2

end: ori $v0, $0, 10 # system call 10 for exit syscall # exit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!