3 Replies - 2190 Views - Last Post: 23 September 2012 - 06:27 PM

#1 btfrenchy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 12

problem with finding GCD

Posted 19 September 2012 - 06:08 PM

I'm having some trouble with my program to find the GCD of two user-inputted numbers. I think my understanding of how to divide numbers in Assembly may be the issue. I am very new to assembly, so any help I can get is very much appreciated.
;project 2

include irvine16.inc

.data
prompt byte "Enter a positive divisor: ", 0ah, 0dh, 0
prompt2 byte "Enter a positive dividend, larger than the divisor: ", 0ah, 0dh, 0
mess byte " is the GCD ", 0ah, 0dh, 0
notPrime byte "This integer is not prime.", 0ah, 0dh, 0
prime byte "This integer is prime, therefore the GCD of this integer and any other is: 1", 0ah, 0dh, 0
prime2 byte "This integer is prime, therefore the GCD is: 1", 0ah, 0dh, 0

num1 dword ?	;variable to hold the user's first number
num2 dword ?	;variable to hold the user's second number




.code
main proc

getFirstNum:

mov ax, @data
mov ds, ax

mov edx, offset prompt
call writeString

call ReadInt ;have to write this
cmp eax, 2
je exitLabel

;compare the number to its half
mov ebx, eax ;copy num

mov num1, eax ;copy num again so there is a space available for the second inputed num


shr ebx, 1 ;ebx == last, divides in half
mov ecx, 2 ;eax == 2



testifdone:
cmp ecx, ebx
ja alldone
xor edx, edx
div ebx ;leaves the remainder in edx
cmp edx, 0

je alldone
inc ecx
jmp testifdone

alldone:
cmp ecx, ebx  ;compare the counter with last
ja primemessage
mov edx, offset notprime
;the number is not prime so we can try to find the
;gcd of this num and another num
call writestring
jmp getSecondNum

primemessage:
mov edx, offset prime
jmp print

;----------------------------------------------------------------------------------------------

getSecondNum:
mov edx, offset prompt2
call writestring


call ReadInt ;have to write this
cmp eax, 2
je exitLabel

;compare the number to its half
mov ebx, eax ;copy num
mov num2, eax

shr ebx, 1 ;ebx == last, divides in half
mov ecx, 2 ;eax == 2

testifdone2:
cmp ecx, ebx
ja alldone2
xor edx, edx
div ebx ;leaves the remainder in edx
cmp edx, 0

je alldone2
inc ecx
jmp testifdone2

alldone2:
cmp ecx, ebx  ;compare the counter with last
ja primemessage2
mov edx, offset notprime
call writestring
jmp stuff
;the number is not prime so we can try to find the
;gcd of this num and another num



primemessage2:
mov edx, offset prime2
jmp print


stuff:
xor eax, eax
xor ecx, ecx
xor edx, edx
mov eax, num1
mov ecx, num2

getGCD:

div ecx  ;puts the remainder into edx
cmp edx, 0
je gotGCD
mov eax, ecx
mov ecx, edx
xor edx, edx
jmp getGCD


gotGCD:
mov eax, ecx ;this is where the GCD needs to get stored

mov edx, offset mess

call writedec
jmp exitLabel




print:
call writeString
       jmp exitLabel




;GCD(12,30) = 12/30 = 2R6; 6/12 = 2R0; therefore the GCD is 6



exitLabel:
	exit
	main ENDP
	END main


I think my problem is occurring at the stuff label and down. Again, thank you for any and all help.

Is This A Good Question/Topic? 0
  • +

Replies To: problem with finding GCD

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,286
  • Joined: 28-March 11

Re: problem with finding GCD

Posted 19 September 2012 - 06:17 PM

Moving on over to the Assembly forum...

So, what is the problem? What output are you expecting/getting?
Was This Post Helpful? 0
  • +
  • -

#3 btfrenchy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 12

Re: problem with finding GCD

Posted 19 September 2012 - 06:29 PM

sorry i thought i had this over in assembly. when i try to do the formula for GCD it doesnt go through the rest of the program, but stops at the getGCD label. i'm confused as to how to do division with the 32-bit registers and the user-input. i am trying to use the euclidean algorithm to find the greatest common divisor of the two numbers.

here is what i'm getting as of now

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 scurveedog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-September 12

Re: problem with finding GCD

Posted 23 September 2012 - 06:27 PM

It looks like you are loading eax with num1 (the divisor) and dividing it with num2 the dividend.
Just the way you have it in your comment on line 150 which should read '30/12 = 2r6 12/6 = 2r0'.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1