Binary GCD algorithm in MIPS

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 4
Reputation: nikki_2000b@hot is an unknown quantity at this point 
Solved Threads: 0
nikki_2000b@hot nikki_2000b@hot is offline Offline
Newbie Poster

Binary GCD algorithm in MIPS

 
0
  #1
Nov 27th, 2007
Hey there,
I am writing MIPS assembly for computing the gcd of two given numbers (recursively), but am struggling!
I vaguely understand changing the frame point counter, stack pointer etc. but I'm really at sea with how to implement the algorithm recursively (e.g. how to check if each number is even or odd, then somehow call the code again and again until input1=input2)
Any assistance will be very appreciated!
Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is online now Online
Posting Virtuoso

Re: Binary GCD algorithm in MIPS

 
0
  #2
Nov 28th, 2007
The stack is just a piece of memory where you can push values on the end or pop values from the end. The $sp register always points to the next available spot.
Visually:
  1. [ ]
  2. [ ]
  3. [ ] <-- $sp
  4. [-7]
  5. [12]
If we push 42 onto the stack, we get:
  1. [ ]
  2. [ ] <-- $sp
  3. [42]
  4. [-7]
  5. [12]
Popping a value works in reverse.

MIPS doesn't have any "push" or "pop" commands, you just use lw and sw and directly add or subtract to change the $sp value.

Every procedure/function will look similar:
  1. proc_name: sub $sp, $sp, 4 # push the return address...
  2. sw $ra, 4($sp) # ...on the stack
  3.  
  4. # (do the subroutine stuff here)
  5.  
  6. # (stick any return values in $v0 (and $v1 if needed))
  7.  
  8. lw $ra, 4($sp) # pop the return address...
  9. add $sp, $sp, 4 # ...off the stack
  10. jr $ra
Because register 31 ($ra) is preserved on the stack during the function, you can easily call other functions, or even recurse without worry. To call the function, just do the usual:
jal proc_name

What follows is for your edification, but I don't think you'll need either to do your assignment:

Passing arguments
In MIPS, registers $a0..$a3 are expected to be used as arguments to functions. This is not entirely necessary, just convenient. You could push arguments onto the stack before calling the function as well. If you have more than four arguments, you'll need to push the extra ones anyway. For example, say we want to push $s0 and $s4 as arguments:
  1. sub $sp, $sp, 8
  2. sw $s0, 8($sp)
  3. sw $s4, 4($sp)
  4. jal my_proc
The order in which arguments are pushed is up to you, but on MIPS machines it is usually higher addresses first, lower addresses last (in this example, $s0 is argument 1 and $s4 is argument 2).
Also, whether your subroutine removes parameters from the stack or whether it is left to the caller is up to you. (C, for example, expects the caller to remove parameters, whereas Pascal expects the routine to remove the parameters.)

Local variables
You can also store local data on the stack once the routine begins. Once you have started the subroutine and stored the return value on the stack, just subtract more space from the stack for room to keep local variable values. Before you return, restore the stack pointer to its proper state, pop the return address (and maybe parameters if that's how you are doing things) and
jr $sp
to return.

Hope this helps.
Last edited by Duoas; Nov 28th, 2007 at 12:33 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: nikki_2000b@hot is an unknown quantity at this point 
Solved Threads: 0
nikki_2000b@hot nikki_2000b@hot is offline Offline
Newbie Poster

Re: Binary GCD algorithm in MIPS

 
0
  #3
Nov 28th, 2007
Thanks for the stack information - very helpful!
However, now due to the algorithm being binary, I have to create subroutines to determine whether or not an inputted number is even or odd, and then divide them accordingly. I think being odd, the binary representation of that number should have an extra 1..? Not sure how to implement the checking, any advice would be appreciated,
thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is online now Online
Posting Virtuoso

Re: Binary GCD algorithm in MIPS

 
0
  #4
Nov 28th, 2007
Your teacher is having you implement the Binary GCD Algorithm in assembly? Yoinks.

Well, you can test any positive number for even or odd by looking at the LSB of the number.
  1. # $s0 : number to check for odd/even
  2. # $t0 : result: 1 = odd, 0 = not odd
  3. andi $t0, $s0, 1
Use a branch instruction against $t0 immediately after to choose what to do.

In a high level language, that's the same as dividing by 2 and seeing if there is any remainder.

Hope this helps.

P.S. The Binary GCD Algorithm is a little complex. Make sure you have a good idea of what you want to do either in pseudocode or some high level language, then decompose each high-level statement down into the assembly instructions necessary to do it...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1
Reputation: Striker9 is an unknown quantity at this point 
Solved Threads: 0
Striker9 Striker9 is offline Offline
Newbie Poster

Re: Binary GCD algorithm in MIPS

 
0
  #5
Dec 1st, 2007
I have started to implement the binary GCD algorithm into assembly. However I'm facing difficulties how to use recursion.
Can anyone help me plse . I
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is online now Online
Posting Virtuoso

Re: Binary GCD algorithm in MIPS

 
0
  #6
Dec 1st, 2007
All the information you need is in the second post of this thread. Please read it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: nikki_2000b@hot is an unknown quantity at this point 
Solved Threads: 0
nikki_2000b@hot nikki_2000b@hot is offline Offline
Newbie Poster

Re: Binary GCD algorithm in MIPS

 
0
  #7
Dec 5th, 2007
Thanks for all the help I've received so far!
However, when parsing my code, SPIM gives me this error: 'Bad address in data/stack read: 0x00000000' at the point when I'm testing the LSB of the inputs. Any advice?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is online now Online
Posting Virtuoso

Re: Binary GCD algorithm in MIPS

 
0
  #8
Dec 5th, 2007
It looks like you are trying to dereference a NULL pointer.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: nikki_2000b@hot is an unknown quantity at this point 
Solved Threads: 0
nikki_2000b@hot nikki_2000b@hot is offline Offline
Newbie Poster

Re: Binary GCD algorithm in MIPS

 
0
  #9
Dec 5th, 2007
Yes, that's what I think, I am trying to test the LSB of the input, which I have stored in $a1. Do the $a registers stay put over subroutines?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 10
Reputation: bStiffler582 is an unknown quantity at this point 
Solved Threads: 1
bStiffler582 bStiffler582 is offline Offline
Newbie Poster

Re: Binary GCD algorithm in MIPS

 
1
  #10
Dec 5th, 2007
  1. # CSIT 311: MIPS - Euclidean Iterative
  2. #
  3. #int gcd_recursive(int a, int b)
  4. #{
  5. # if ( b == 0 )
  6. # return a;
  7. #
  8. # else
  9. # return gcd_recursive(b, a % b);
  10. #}
  11.  
  12. .text
  13. .globl main
  14.  
  15. main:
  16.  
  17. # Prompt for user input
  18. la $a0, prompt # $a0 holds prompt
  19. li $v0, 4 # print string in $a0
  20. syscall
  21.  
  22. # read in the integers
  23. li $v0, 5 # "read integer" code
  24. syscall
  25. move $a0, $v0 # store input in A
  26.  
  27. li $v0, 5
  28. syscall
  29. move $a1, $v0 # store B
  30.  
  31. base:
  32. bne $a1, $zero, rec1
  33. la $a0, answer
  34. li $v0, 4
  35. syscall
  36. lw $a0, A # load A to be displayed
  37. li $v0, 1 # display A
  38. syscall
  39. li $v0, 10
  40. syscall
  41. rec1:
  42. sub $sp, $sp, 12 # push stack
  43. sw $ra, 0($sp) # save return address
  44. sw $a0, 4($sp) # save registers
  45. sw $a1, 8($sp)
  46. move $t0, $a1 # move A to temp before operation
  47. rem $a1, $a0, $a1 # calc remainder
  48. sw $t0, A # store previous A for output
  49. jal base
  50. lw $ra, 0($sp)
  51. addi $sp, $sp, 12
  52. jr $ra
  53.  
  54. .data
  55. A: .word 0 # create blank A/B
  56. B: .word 0
  57.  
  58. prompt: .asciiz "Please type 2 integers, A and B; Enter after each:\n"
  59. answer: .asciiz "\nGCD = " # answer =

This code works, but does not implemement the stack.
Last edited by bStiffler582; Dec 5th, 2007 at 6:21 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC