Section 2: Using the MARS or SPIM simulator develop a program that will implemen
ID: 3793818 • Letter: S
Question
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!
Explanation / Answer
this code is for collatz sequence
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 " "
.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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.