Using the MARS or SPIM simulator develop a program that will

Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If(n is even) {n = n/2;} else {n = 3 * n + 1;} In this case, n is to be input by the user (assume they input a non-negative value), the conditional is performed, and the resulting n is to be output. Again, use the system calls for input, output, and exiting the program.

Solution

.data

prompt: asciiz \"Enter a positive number\"

message : asciiz \" result\"

.text

#prompt the user

li $ v0,4

la $a0,prompt

syscall

#get number

li $v0,5

syscall

#store number in $t0 and $t3

move $t0,$v0

move $t3,$v0

#and $t0 with 1 and store it in $t1

andi $t1,$t0,1

#if it is even jump to ISEVEN

beq $t1,$zero,ISEVEN

#if it is odd do following

muli $t3,$t3,3

addi $t3,$t3,1

#display the string result and then number n

display: li $v0,4

la $a0,message

syscall

li $vo,1

move $a0,$t3

syscall

  

#end the program

li $v0,10

syscall

#if it is even do following

ISEVEN: div $t3,$t3,2

j display

 Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If(n is even) {n = n/2;} else {n = 3 * n + 1;} In t
 Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If(n is even) {n = n/2;} else {n = 3 * n + 1;} In t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site