Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Greatest Common Divisor (GCD)

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
0
0
US
Hi forum,
I was trying to implement the GCD algorithm in Assembly, but something seems to be going wrong when the execution phase reaches the 'div' instruction. I get an integer overflow error but I dont see why!?! Below is my GCD proc.
;;;;;;;;;;;;;
.data?
x dword ?
y dword ?
;;;;;;;;;;;;;;;;;;
GCD proc uses eax ebx edx
mov eax, 0
mov edx, 0
do:
cmp y, 0
jna finish
mov eax, x
mov ebx, y
div ebx ; this is the troublesome line!!
mov x, ebx
mov y, edx
jmp do
finish:
ret
GCD endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
The EDX reg contained 7FFE0304 at the start of the program. just before calling the GCD proc EDX contains 00405014. Using the msdev debugger, when i step through the code it stops at the div instruction with unhandled exception integer overflow error 0xC000095. Just after the error edx contains 00000004. As input i entered 1000 and 12 for x and y respectively.
I'm clueless!
Can somebody plz help me???
Kunal.
 
I used MASM, and ran your code. It works perfectly.
Variable y is 0, and it always jump to finish.

Are you using inline asm or ... ?
Did you see what is the value of x & y ?

Try to swap your code so you can see what is x and y when you call it

do:
mov eax, x ; eax = ??
mov ebx, y ; ebx = ??
cmp y, 0
jna finish


-- AirCon --
 
Sorry, I misread your message.

You mentioned that X=1000, Y=12
So I ran again and I see the problem. The problem is EDX must be zero before DIV

GCD proc uses eax ebx edx
do:
cmp y, 0
jna finish
mov eax, x
mov ebx, y
mov edx, 0 ; edx must be 0 before division
div ebx ; now it works
mov x, ebx
mov y, edx
jmp do
finish:
ret
GCD endp


-- AirCon --
 
Status
Not open for further replies.

Similar threads

Replies
1
Views
29

Part and Inventory Search

Sponsor

Back
Top