943,965 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 8297
  • Assembly RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 27th, 2007
0

Binary GCD algorithm in MIPS

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikki_2000b@hot is offline Offline
4 posts
since Nov 2007
Nov 28th, 2007
0

Re: Binary GCD algorithm in MIPS

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:
Assembly Syntax (Toggle Plain Text)
  1. [ ]
  2. [ ]
  3. [ ] <-- $sp
  4. [-7]
  5. [12]
If we push 42 onto the stack, we get:
Assembly Syntax (Toggle Plain Text)
  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:
Assembly Syntax (Toggle Plain Text)
  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:
Assembly Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 28th, 2007
0

Re: Binary GCD algorithm in MIPS

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikki_2000b@hot is offline Offline
4 posts
since Nov 2007
Nov 28th, 2007
0

Re: Binary GCD algorithm in MIPS

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.
Assembly Syntax (Toggle Plain Text)
  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...
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 1st, 2007
0

Re: Binary GCD algorithm in MIPS

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Striker9 is offline Offline
1 posts
since Dec 2007
Dec 1st, 2007
0

Re: Binary GCD algorithm in MIPS

All the information you need is in the second post of this thread. Please read it.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 5th, 2007
0

Re: Binary GCD algorithm in MIPS

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikki_2000b@hot is offline Offline
4 posts
since Nov 2007
Dec 5th, 2007
0

Re: Binary GCD algorithm in MIPS

It looks like you are trying to dereference a NULL pointer.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 5th, 2007
0

Re: Binary GCD algorithm in MIPS

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikki_2000b@hot is offline Offline
4 posts
since Nov 2007
Dec 5th, 2007
1

Re: Binary GCD algorithm in MIPS

Assembly Syntax (Toggle Plain Text)
  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.
Reputation Points: 13
Solved Threads: 1
Newbie Poster
bStiffler582 is offline Offline
10 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: Simple EEPROM Programmer Through Parallel PC Port (also posted in Developers lounge)
Next Thread in Assembly Forum Timeline: DATASTAR DATABASE divide overflow





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC