C to MIPS: How would I go about converting the following if-else statement? I kn
ID: 3721968 • Letter: C
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 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<rows; i++) {
for(j=0; j<cols; j++) {
scanf("%d%d%d", &r, &g, &b); //reading in rgb values
gray = rgb_to_gray(r, g, b, ppm_max);//calculate gray value calling rgb_to_gray
if (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
Explanation / Answer
Here is your answer:
slt $t0, $t1, $s0
beq $t0, $zero, else # t0==0 if i >= y1 or !(i < y1)
slt $t0, $s2, $s1
beq $t0, $zero, else # t0 == 0 if i <= y2 or !( i > y2)
slt $t1, $s3, $s4
beq $ t1, $zero, else # t1 == 0 if j >= x1 or !( j < x1)
slt $t1, $s5, $s3
beq $t1, $zero, else # t1 == 0 if j <= x2 or !( j > x2)
addi $v0, $0, 1 # call to print grayscale value
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
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
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
else:
addi $v0, $0, 1 # call to print grayscale value
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
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
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
N.B:
slt : set on less than
beq: branch on equal
Logic :
i = $ s1 , j = $s3 , y1 = $s0 , y2 = $s2 , x1 = $s4 , x2 = $s4
now ,
i <= y1 : slt $t0, $s1, $s0
beq $t0, $zero, else
if the $t0 value will not be zero then the else block will be executed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.