Question

Section 2: Using the MARS or SPIM simulator develop a program that will implement the following...

Section 2:

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. Hint: There is a remainder pseudoinstruction for the MIPS architecture that you can use to determine if the value is even or odd or you can look at bit 0 to determine if the value is even or odd.

section 2 work(correct)

.text
.global main
main:
li $v0, 4
la $a0, msg1
syscall

li $v0,5
syscall
move $t0,$v0
and $t1,$t0,0x01
beq $t1,1,odd
j even

odd:
mul $t0,$t0,3
add $t0,$t0,1
j exit

even:
divu $t0,$t0,2
exit:
li $v0, 4
la $a0, msg2
syscall
  
li $v0, 1
move $a0, $t0
syscall
  
li $v0, 10
syscall

.data
msg1: .asciiz "Enter a number: "
msg2: .asciiz "result = "

Section 3:
You are to take the conditional from the previous section and build a loop around it to find the Collatz sequence. The structure of this would be:
while (n > 1) {
If (n is even) {


Section 4:
n = n / 2; } else {
n = 3 * n + 1; }
cout << n; }
You are to write a leaf subprogram that will output the following information: Your Name
Your favorite color
Your favorite sports team
The main program should call your leaf routine and then exit using the system call.


Section 5:
Write a program with a leaf subprogram that will take two values in $a0 and $a1 and compute their greatest common divisor. The greatest common divisor should be returned in the $v0 register. The main program should input the values for $a0 and $a1 using system calls, call your subprogram, and then output the result using a system call.   


it is all in assembly language

for three i currently have section .text
global _start
_start:
mov bx, data
mov ds, bx

lea si, msg
call print

mov ah, 01h
int 21h

WHILE:
sar al, 01
jc ODD

lea si, msgE
call print
mov bl, 2
div al, bl
jump loop
  
ODD:
lea si, msgO
call print
mov bl, 3
mul al, bl
mov bl, 1
add al, bl

loop:
cmp al, 00
jg WHILE

terminate:
mov ah, 4ch
int 21h

print proc
mov dx, si
mov ah, 09h
int 21h
ret
print endp

section .data
msg db 10, 13, 'Enter a number = $'
msgE db 10, 13, 'Number is even $'
msgO db 10, 13, 'Number is odd $'

It has errors with the anything that has an h in it and i dont know why

thanks!

Homework Answers

Answer #1

this code is for collatz sequence

.data  

prompt:        .asciiz "Enter an integer\n"
neq1Message:   .asciiz "N == 1"
nevenMessage:  .asciiz "Integer is even : "
noddMessage:   .asciiz "Integer has been multiplied by 3 and added by 1 : "

.text  
main:

    #print prompt
    la $a0 prompt
    addi $v0 $zero 4
    syscall

    #read integer into $t0
    addi $v0 $zero 5
    syscall
    move $t0 $v0 

    loop:

        # quit loop if n == 1
        addi $t1 $zero 1
        beq  $t0 $t1 loopEnd

        #skip to even if n != 1
        addi $t1 $zero 1
        bne  $t0 $t1 neven

        neq1:

            # print n is 1
            la $a0 neq1Message
            addi $v0 $zero 4
            syscall

            j loop

        neven:

            # skip to odd if n not even
            andi $t1 $t0 1
            bne  $t1 $zero nodd

            # print n is even
            la $a0 nevenMessage
            addi $v0 $zero 4
            syscall

            # print n
            move $a0 $t0
            addi $v0 $zero 1
            syscall

            # print newline
            addi $a0 $zero 10
            addi $v0 $zero 11
            syscall

            # n = n / 2
            srl $t0 $t0 1

            j loop

        nodd:

            # n = 3 * n + 1
            addi $t1 $zero 3
            mul  $t0 $t0 $t1
            addi $t0 $t0 1

            # print n is odd
            la $a0 noddMessage
            addi $v0 $zero 4
            syscall

            # print n
            move $a0 $t0
            addi $v0 $zero 1
            syscall

            # print newline
            addi $a0 $zero 10
            addi $v0 $zero 11
            syscall

            j loop

    loopEnd:

jr $ra

to find gcd of two numbers

    .data
str1:   .asciiz " give 2 integers "
str2:   .asciiz " the gcd of "
str3:   .asciiz " and "
str4:   .asciiz "is: "
str5:   .asciiz "\n"

   .text
main:   addi $sp, $sp, -4   #create a stack frame
   sw $ra, 0($sp)       #save the return address

again:   la $a0, str1       #apseudo assembly instruction
   li $v0, 4       #print str1
   syscall

   li $v0, 5       #get 1st num
   syscall           #and put into $v0
   move $s0, $v0          
   bltz $s0, again       # if $s0<=0 go to again
  

gcd:   addi $sp, $sp, -4   #create 4 word long stack frame
   sw $ra, 0($sp)       #save the retur address  
   beqz $a1, exit_gcd   # if $a1=0 go to exit_gcd
   div $a0, $a1       #lo= $a0/$a1 ; hi=$a0 mod $a1
   mfhi $ti       # $ti=hi
   move $a0, $a1      
   move $a1, $t1
   jal gcd           #recursive call

   move $a0, $s0       # $a0=$s0
   move $a1, $s1       # $a1=$s1
   jal gcd           # go to gcd
   move $t0, $v0       # $t0=$v0

exit_gcd:
   move $v0, $a0
   lw $ra, 0($sp)       #restore return address
   addi $sp, $so, 4   #adjust stack pointer
   jr $ra           #return to caller

   la $a0, str5          
   li $v0, 4       # prints str5
   syscall
      
   lw $ra, 0($sp)       #restore the return address
   addi $sp, $sp, 4   #eliminate the stack frame
   jr $ra

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions