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

I\'m writing a MIPS Mars program to find the lowest common multiple of two integ

ID: 3728570 • Letter: I

Question

I'm writing a MIPS Mars program to find the lowest common multiple of two integers. The function takes 4 values from 4 registers, $a0, $a1, $a2, and $a4. I've attached my code below. The program works for fine, except for one if statement. I've commented out the block that's producing an error.

It looks like I'm getting an error when it hits the bgt $a2, $a3, else line. It works fine for when the numbers are equal, and when the first number is bigger than the second number. It breaks when the first number is less than the second number, which is from the bgt line. When I run the program it gives this error message in the console;

Assemble: assembling /Users/name/Desktop/school/class/assignment/LCM_Recursive.asm

Assemble: operation completed successfully. Go: running LCM_Recursive.asm

Error in /Users/name/Desktop/school/class/assignment/LCM_Recursive.asm line 59: Runtime exception at 0x0040010c: address out of range 0x7fbffffc

Go: execution terminated with errors.

This is a screenshot of line 59;

# Function: 1cm recursive # Argument: a0 Ve integer number m $al +ve integer number n $a2temporary LCM by increamenting m, initial is m a3temporary LCM by increamenting n, initial is n # Returns # Purpose: Implementing LCM function using recursive call. lcm_recursive: # Store frame addi $sp, $sp, -28 SW SW Sw SW SW Sw addi $fp, $sp, 28 $fp, 28($sp) $ra, 24($sp) $a0, 20($sp) $a1, 16($sp) $a2, 12($sp) $a3, 8($sp) # Body beq bgt add jal lcm_recursive if lcm-m 1cm-n GOTO 1cm-recursive-end $a2, $a3, lcm_recursive_end $a2, $a3, else $a2, $a2, $a0 (works does not work) (works no issues) # no issues) - else: add jal $a3, $a3, $al lcm_recursive lcm_recursive_end: move $V0, $a2 lcm recursive ret lcm_recursive_ret: # Restore frame $fp, 28($sp) $ra, 24($sp) $a0, 20($sp) $a1, 16($sp) $a2, 12($sp) $a3, 8($sp) Lw LW Lw LW addi $sp, $sp, 28 $ra # jump to caller

Explanation / Answer

I have fixed the code. Please use the following updated code and let me know if any issues. You need to add j lcm_recursvie_ret for both if and else cases. Please do rate the answer if it helped.


lcm_recursive:
addi $sp, $sp, -28
sw $fp, 28($sp)
sw $ra, 24($sp)
sw $a0, 20($sp)
sw $a1, 16($sp)
sw $a2, 12($sp)
sw $a3, 8($sp)
addi $fp, $sp, 28

beq $a2, $a3, lcm_recursive_end
bgt $a2, $a3, else
add $a2, $a2, $a0
jal lcm_recursive
j lcm_recursive_ret
else:
add $a3, $a3, $a1
jal lcm_recursive
j lcm_recursive_ret

lcm_recursive_end:
move $v0, $a2

lcm_recursive_ret:

lw $fp, 28($sp)
lw $ra, 24($sp)
lw $a0, 20($sp)
lw $a1, 16($sp)
lw $a2, 12($sp)
lw $a3, 8($sp)
addi $sp, $sp, 28
jr $ra