Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; MIPS - Euclidean Recursive
- ;
- ; @author Mr. Polywhirl
- ; @description The following program is an implementation of the Euclidean
- ; Greatest Common Divisor algorithm C function below.
- ;
- ; int gcd_recursive(int a, int b) {
- ; if (b == 0)
- ; return a;
- ; else
- ; return gcd_recursive(b, a % b);
- ; }
- ;
- ; @registers $t0 - Stores an integer value for 'a'
- ; $t1 - Stores an integer value for 'b'
- ; $t2 - The remainder from the division of a / b
- .data ;; DECLARATIONS
- ans: .word 0x0000 ; Answer place holder
- a: .word 0x00000162 ; a = 354
- b: .word 0x00000084 ; b = 132
- .text ;; INSTRUCTIONS
- main: lwu $t1, b($zero) ; load b into $t1
- lwu $t0, a($zero) ; load a into $t0
- gcd: beqz $t1, res ; if b == 0, jump to result
- div $t0, $t1 ; a / b
- mfhi $t2 ; Retrieve and store remainder from HI
- or $t0, $zero, $t1 ; a = b
- or $t1, $zero, $t2 ; b = remainder (a % b)
- bnez $t1, gcd ; if b != 0
- res: sd $t0, ans($zero) ; Store result to memory: 6
- exit: halt ; Exit program.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement