Here's my modified program:
.586
.model flat, c
.stack 100h
.data
; gcd(10, 4), gcd(31415, 14142), gcd(3241343, 948324),
; gcd(31243,68455), gcd(75324,729345), gcd(327256,647384)
left dword 10, 31415, 3241343, 31243, 75324, 327256
right dword 4, 14142, 948324, 68455, 729345, 647384
.code
doit proc
xor ecx,ecx
doagain:
push left[ecx]
push right[ecx]
call gcd
add esp,8
add ecx,4
cmp ecx,24
jle doagain
ret
doit endp
gcd proc
mov eax,[esp + 8] ; eax is left
mov ebx,[esp + 4] ; ebx is right
again:
xor edx,edx ; zeroing out edx to prevent Integer overflow
div ebx ; divides left by right
cmp edx, 0 ; We are done when remainder is zero
je weAreDone
mov eax,ebx ; we have a new left
mov ebx,edx ; our remainder is our new right
jmp again ; repeat
weAreDone:
; ebx has the greatest common divisor
ret
gcd endp
end
First I moved my compare from the end of gcd into the middle,and the reason we do that is because if we leave the compare at the end then once we've got our greatest common divisor then every thing else after that would just be extra work which is why I moved the compare to right after the division problem,if the 2nd number is 0 we jump straight to the return,and if it's not we just continue and switch all the numbers. Also I changed it so that we only read off of the inputs and we don't change them at all,because it's like doit sent a letter to gcd and if we change the inputs it's like we drew a picture all over that letter. And now since we use the inputs a lot less we use the registers a lot more,and a good thing about that is using the registers is way faster. Also I changed my commentary.
WELL TTFN,TA TA FOR NOW!!!!!!!!!!!!!!!!!!!!
No comments:
Post a Comment