Question: Please help with this code, I need questions 3 and 4 answered please. I have 1 and 2 done. Thanks # WRITE MIPS CODE FOR

Please help with this code, I need questions 3 and 4 answered please. I have 1 and 2 done. Thanks

# WRITE MIPS CODE FOR THE FOLLOWING C++ CODE: #include #include #using namespace std; #void main() #{ # //declare variables # string result; # int x; # //prompt for, read in a value for x # cout << "Please enter a value for x (integer): "; # cin >> x; # # ========================================================== # //PROBLEM1 - if x has a value of 3 or 4, print "ddd", else print "eee" # result = ""; # if(x==3 || x==4) result = result + "ddd"; # else result = result + "eee"; # cout << "1.\t" << result << endl; # # ============================================================= # //PROBLEM2 - if x is less than 5 and greater than 2, # // then check to see if it is even. If it is even, # // print "ddd". If its not even, the original "ccc" is printed. # // But if x was not less than 5 and greater than 2, # // print "eee". # result = "ccc"; # if (x < 5 && x > 2) # { # if (x%2 == 0) result = "ddd"; # } # else result = "eee"; # cout << "2.\t" << result << endl; # # ========================================================== # //PROBLEM3 - if x has a value if 2, print "bbb" # // if x has a value of 3, print "ccc" # // if x has a value of 4, print "ddd" # // if x has a value other than 2, 3, or 4 print "eee" # result = ""; # switch (x) # { # case 2: result = result + "bbb"; break; # case 3: result = result + "ccc"; break; # case 4: result = result + "ddd"; break; # default: result = result + "eee"; # } # cout << "3.\t" << result << endl; # # ========================================================= # //PROBLEM4 - if x has a value of 4, print "ddd" # // if x has a value of 3, print "ccc" # // any other value (not 3 or 4), print "eee" # result = ""; # if (x==4) result = result + "ddd"; # else if (x==3) result = result + "ccc"; # else result = result + "eee"; # cout << "4.\t" << result << endl; # # }//end main()

.data .eqv SYS_PRINT_WORD 1 #word, byte, character .eqv SYS_PRINT_FLOAT 2 #float .eqv SYS_PRINT_DOUBLE 3 #double .eqv SYS_PRINT_TEXT 4 #text (zero terminated) .eqv SYS_INPUT_WORD 5 #input word .eqv SYS_EXIT 10 #terminate

# //declare variables # string result; # int x; result: .asciiz "0123456789012345" #max of 16 chars x: .word 0 # copy of $s5 endl: .asciiz " " tx_prompt: .asciiz "Please enter a value for x (integer): " tx_bbb: .asciiz "bbb" tx_ccc: .asciiz "ccc" tx_ddd: .asciiz "ddd" tx_eee: .asciiz "eee" prob1_label: .asciiz "1.\t" prob2_label: .asciiz "2.\t" prob3_label: .asciiz "3.\t" prob4_label: .asciiz "4.\t"

.text .globl main main: # //prompt for, read in a value for x # cout << "Please enter a value for x (integer): "; # cin >> x; la $a0, tx_prompt # load address of prompt for syscall li $v0, SYS_PRINT_TEXT # specify Print String service syscall # print the prompt string li $v0, SYS_INPUT_WORD # specify Read Integer service syscall # Read the number. After this instruction, the number read is in $v0. add $s5, $v0, $zero # transfer the number to s5 la $t0, x sw $s5, 0($t0) # s5 = x = input word number la $s4, result # s4 = address of result string

# //PROBLEM1

la $t0, x

lw $t0, 0($t0) # load x into t0

beq $t0, 3, PROB1_TRUE # check if x is equal to 3

beq $t0, 4, PROB1_TRUE # check if x is equal to 4

j PROB1_FALSE

la $a0, tx_ddd # load address of ddd string

j PROB1_EXIT

la $a0, tx_eee # load address of eee string

li $v0, SYS_PRINT_TEXT # specify Print String service

syscall # print the string

la $a0, prob1_label # load address of prob1_label string

li $v0, SYS_PRINT_TEXT # specify Print String service

syscall # print the string

la $a0, endl # load address of endl string

li $v0, SYS_PRINT_TEXT # specify Print String service

syscall # print the endl string

# //PROBLEM2

la $t0, x

lw $t0, 0($t0) # load x into t0

slti $t1, $t0, 5 # check if x < 5

beq $t1, 0, PROB2_FALSE # if x >= 5, jump to PROB2_FALSE

sgti $t1, $t0, 2 # check if x > 2

beq $t1, 0, PROB2_FALSE # if x <= 2, jump to PROB2_FALSE

if (x%2 == 0) result = "ddd";

addi $t2, $t0, -2 # t2 = x - 2

div $t0, $t2, 2 # t0 = t2 / 2

mfhi $t1 # t1 = remainder

bne $t1, 0, PROB2_EXIT # if t1 != 0, jump to PROB2_EXIT

la $a0, tx_ddd # load address of ddd string

j PROB2_EXIT

la $a0, tx_eee # load address of eee string

j PROB2_EXIT

# //PROBLEM3

# //PROBLEM4

#---- terminate --- exit: li $v0, SYS_EXIT syscall #------------------------------------------------------------- # concatstr - concatenate $a1 to $a0 string # input $a0 - address of result string # input $a1 - address of string to concatenate to result # output $a0 - address of end of string \0 # output $a1 - address of end of string \0 #------------------------------------------------------------- concatstr: #find \0 in a0 result string findend: lbu $t0, 0($a0) beq $t0, $zero, docopy # branch if result.at(a0) == '\0' addiu $a0, $a0, 1 j findend # move to next char and keep looking # concatenate a1 chars to a0 chars docopy: lbu $t0, 0($a1) # get current char from a1 sb $t0, 0($a0) # copy current char to a0 beq $t0, $zero, concatstr_exit # if char == '\0', branch & stop if char was '\0' addiu $a0, $a0, 1 # advance to next char addiu $a1, $a1, 1 # advance to next char j docopy concatstr_exit: jr $ra #.end main

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!