Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- asknumber1: .asciiz "Please insert the first number: "
- asknumber2: .asciiz "Please insert the second number: "
- .text
- main:
- la $a0, asknumber1 # load this variable to be able to print the string
- li $v0, 4
- syscall # syscall code 4 to print a string
- li $v0, 5
- syscall # syscall code 5 to read an integer
- move $s0, $v0 # store a copy of $v0 into $s0
- la $a0, asknumber2 # load this variable to be able to print the string
- li $v0, 4
- syscall # syscall code 4 to print a string
- li $v0, 5
- syscall # syscall code 5 to read an integer
- move $s1, $v0 # store the argument in $s1
- move $v0, $s0 # restore $v0 to $s0
- jal gcd # jump and link to the gcd procedure
- remainder:
- move $t0, $s0 # store the argument into $t0
- subu $t0, $t0, $s1 # prediction whether the subtraction will go sub-zero
- blt $t0, 0, finish_remainder # while a is not below 0
- subu $v0, $v0, $s1 # subtract a with b
- move $s0, $v0 # update $s0
- j remainder # repeat the loop
- finish_remainder:
- move $s0, $s1 # y becomes x
- move $s1, $v0 # store the result in y , now: (y, remainder(x,y))
- j gcd
- gcd:
- addi, $sp, $sp, -4 # allocate the stack pointer
- sw $ra, 0($sp) # store the return address
- beq $s1, 0, basecase # base case
- blt $s0, $s1, finish_gcd # in case x >= y, continue loop
- jal remainder # jump and link to the remainder procedure
- jal gcd # jump and link to gcd (recursive)
- basecase:
- move $v0, $s0 # keep the result in $v0
- finish_gcd:
- move $a0, $v0 # store the result (x), in $a0 for printing
- li $v0, 1
- syscall # syscall code 1 to print an integer
- lw $ra, 0($sp) # load the return address
- exit:
- li $v0, 10
- syscall # syscall code 10 for exit
- # end of oef2.asm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement