preview

Nt1310 Unit 1 Research Paper

Satisfactory Essays

.data s1: .asciiz "Enter first integer: " s2: .asciiz "Enter second integer: " s3: .asciiz "\nThe least common multiple of the two integers is: "
.text
.globl main main: li $v0, 4 la $a0, s1 syscall li $v0, 5 syscall move $a1,$v0 #puts integer 1 into $a1 li $v0, 4 la $a0, s2 syscall li $v0, 5 syscall move $a2, $v0 #puts integer 2 into $a2
#call for gcd addi $sp, $sp, -8 # makes the stack availible for 2 elements sw $a1, 4($sp) # stores integer 1 in $a1 sw $a2, 0($sp) jal gcd # jump to gcd function lw $a2, 0($sp) # loads integer 1 into $a1 lw $a1, 4($sp) addi $sp, $sp, 4 # pop 1 element from the stack and the space is reused for $s0 move $s0, $v0 # $s0 is the result from gcd sw $s0, 0($sp) # the …show more content…

syscall li $v0, 1 la $a0, ($s1) #$s1 is the output syscall li $v0, 10 # end main syscall #gcd function gcd: addi $sp, $sp, -8 # makes the stack availible for 2 elements sw $ra, 4($sp) # saves the return address for the second element in the stack div $a1, $a2 # interested in the remainder in $hi when dividing mfhi $s0 # $s0 is the remained from the modulus sw $s0, 0($sp) # stores the remainder $s0 bne $s0,$zero,loop # if $s0 is not equal to 0, it will go to the loop move $v0, $a2 # store $a2 in $v0 addi $sp, $sp, 8 # pop 2 elements from the stack jr $ra # jump to the return address loop: move $a1, $a2 # makes $a1 equal to $a2 lw $s0, 0($sp) # loads the remainder into $s0 add $a2, $s0, $zero # $a2 now has the remainder jal gcd # jumps back to the gcd function lw $ra, 4($sp) # loads the previous return address addi $sp, $sp, 8 # pop 2 elememts from the stack jr $ra # jumps to the return address
#function for the lcm lcm: addi $sp, $sp, -12 # makes the stack availible for 3

Get Access