Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# File: mlIfElse.asm # Author: Landon Stobart # Date: 09/13/2017 # Purpose: Prac

ID: 3872107 • Letter: #

Question

#    File: mlIfElse.asm
# Author: Landon Stobart
#    Date: 09/13/2017
# Purpose: Practice if-else logic
#----------------------------------------------
# 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. " << 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. " << 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. " << 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. " << 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. "
    prob2_label:    .asciiz "2. "
    prob3_label:    .asciiz "3. "
    prob4_label:    .asciiz "4. "

.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 - 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. " << result << endl;

    sb $zero, 0($s4)
   li $t1, 3   # x==3?
    beq $s5, $t1, ddd1  # if yes then branch
        li $t1, 4   # x==4?
        beq $s5, $t1, ddd1  # if yes then branch
    #else
        move $a0, $s4  # x not 3 and x not 4
        la      $a1, tx_eee
        jal concatstr  #result = result plus "eee"
        j prt1
    #if is true
ddd1:
move $a0, $s4  #x =3 or x=4
la $a1, tx_ddd
jal concatstr
prt1:
la $a0, prob1_label
li $v0, 4
syscall
move $a0, $s4  #print result
li $v0, 4
syscall
la $a0, endl
li $v0, 4
syscall
#   //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. " << result << endl;

addi $t0, $zero, 5
addi $t1, $zero, 2

slt $t2, $s5, $t0
beq $t2, $zero, eee2
slt $t3, $s5, $t1
bne $t3, $zero, eee2
---------------------------------------------This is as far as a I am, problem 1 is done, i need 2,3 and 4

Explanation / Answer

// Problem-2
#include <iostream>
using namespace std;

int main()
{
    string result="";
    int x;
    cout << "Enter an integer: ";
    cin >> x;
// Check if x is between 5 and 2
    if ( x <5 && x>2)
    {
    //check if provided number is even
        if(x%2==0){
            result=result+"ddd";
            cout << result << endl;
        }
    //if x lies between 2 and 5 and not an even number then execute below loop
        else{
            result=result+"ccc";
            cout << result << endl;
        }
    }
// if x is not lessthan 5 and greater than 2 then below loop will be executed
    else
    {
        result=result+"eee";
        cout << result << endl;
    }


    return 0;
}