In: Computer Science
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values.
//q -> $s0 //y -> $s1 //x -> $s2
int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
Note: Input values, statement data and label names are arbitrary. These can be changed according the user's choice. Please refer to code snippets for a better understanding of comments and indentations.
IDE Used: MARS
Program Code
.data
if_state : .asciiz "inside if"
elseif_state : .asciiz "inside elseif"
else_state : .asciiz "inside else"
.text
# initialise registers
li $s0, 11
li $s1, -1
li $s2, 77
# check if $s0 < 9, if true then branch to
# if segment
blt $s0, 9, if_seg
# elseif check $s1 > 0 or $s2 < 10
# if anyone true then branch to elseif segment
bgt $s1, 0, elseif_seg
blt $s2, 10, elseif_seg
# else directly branch to else segment
else_seg :
# print else statement
li $v0, 4
la $a0, else_state
syscall
# end the progrsm
li $v0, 10
syscall
# if segment label
if_seg :
# print the if
statement
li $v0, 4
la $a0, if_state
syscall
# end the progrsm
li $v0, 10
syscall
# elseif segment branch
elseif_seg :
# print the else if
statement
li $v0, 4
la $a0, elseif_state
syscall
# end the progrsm
li $v0, 10
syscall
Code Snippets
Sample Output Run
for $s0 = 5, $s1 = 17, $s2 = 77
for $s0 = 11, $s1 = -1, $s2 = 5
for $s0 = 11, $s1 = -1, $s2 = 77