Advertisement
MrPolywhirl

gcd.asm

Oct 8th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; MIPS - Euclidean Recursive
  2. ;
  3. ; @author       Mr. Polywhirl
  4. ; @description  The following program is an implementation of the Euclidean
  5. ;               Greatest Common Divisor algorithm C function below.
  6. ;
  7. ;               int gcd_recursive(int a, int b) {
  8. ;                   if (b == 0)
  9. ;                       return a;
  10. ;                    else
  11. ;                        return gcd_recursive(b, a % b);
  12. ;                }
  13. ;
  14. ; @registers    $t0 - Stores an integer value for 'a'
  15. ;               $t1 - Stores an integer value for 'b'
  16. ;               $t2 - The remainder from the division of a / b
  17.  
  18.         .data                   ;; DECLARATIONS
  19. ans:    .word 0x0000            ; Answer place holder
  20. a:      .word 0x00000162        ; a = 354
  21. b:      .word 0x00000084        ; b = 132
  22.         .text                   ;; INSTRUCTIONS
  23. main:   lwu   $t1, b($zero)     ; load b into $t1
  24.         lwu   $t0, a($zero)     ; load a into $t0
  25. gcd:    beqz  $t1, res          ; if b == 0, jump to result
  26.         div   $t0, $t1          ; a / b
  27.         mfhi  $t2               ; Retrieve and store remainder from HI
  28.         or    $t0, $zero, $t1   ; a = b
  29.         or    $t1, $zero, $t2   ; b = remainder (a % b)
  30.         bnez  $t1, gcd          ; if b != 0
  31. res:    sd    $t0, ans($zero)   ; Store result to memory: 6
  32. exit:   halt                    ; Exit program.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement