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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GCD Help

Status
Not open for further replies.

HotCode

Programmer
Sep 29, 2011
4
Ok well I am working on a program that does the GCD thing but needs to be recursive. My problem is I dont know how to call it with the values that I have in the data section and two I dont know if it works right or not.
well here is what I have so far
Thanks for any help :)
Code:
TITLE MASM GCD						(GCD.asm)


INCLUDE Irvine32.inc
.data
myMessage BYTE "GCD Recursive",0dh,0ah,0

;first set of nums
val1 DWORD  5
val2 DWORD  20

;second set of nums
val3 DWORD  24
val4 DWORD  18

;3rd set
val5 DWORD  11
val6 DWORD  7

;4th set
val7 DWORD  432
val8 DWORD  226

;5th set
val9 DWORD  26
val10 DWORD  13

.code
main PROC
	call Clrscr  

	mov	 edx,offset myMessage
	call WriteString
	call Crlf
	call GCD
	call WriteInt
	
	exit
main ENDP


;------------------------------------------------
GCD PROC,
	denom:DWORD,
	divisor:DWORD
; This finds GCD
; Gets values from stored values
;returns NA

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

	mov ebx,divisor			;this is the divider make sure its smaller number!    
	mov edx,denom			;this is value 1
		div ebx					 ;divide int1 by int2
		cmp  edx,0				 ;does remainder = 0 ?
		je   L1		             ;yes: quit
		call GCD		     ;no: call GCD agian
      L1:
        mov eax,ebx		     ;EAX = GCD

	pop edx
	pop ebx
	ret
GCD ENDP

END main
 
Ok so I got it to work a little bit it finds the GCD for 5 and 20 but when I test it with 18 and 24 the program stops working. I am missing something here with my logic or some thing with the registers. I dont know what is wrong with this seeing that it worked with the first test group of numbers. Here is what I have so far:
Code:
TITLE MASM GCD						(GCD.asm)




INCLUDE Irvine32.inc
.data
myMessage BYTE "GCD",0dh,0ah,0
myMess2   BYTE "The GCD is = " ,0dh,0ah,0

;first set of nums
val1 DWORD  5
val2 DWORD  20

;second set of nums
val3 DWORD  24
val4 DWORD  18


.code
main PROC
	call Clrscr  

	mov	 edx,offset myMessage
	call WriteString		;write message
	call Crlf				;new line
	push val1				;push 1st set on the stack
	push val2
	call GCD
	pop val1
	pop val2
	mov	 edx,offset myMess2
	call WriteString
	call WriteInt           ;Display GCD WriteInt uses EAX = qutent
	call Crlf				;new line
	
	;next set of values
	push val4				;push 1st set on the stack
	push val3
	call GCD
	pop val4
	pop val3
	mov	 edx,offset myMess2
	call WriteString
	call WriteInt           ;Display GCD WriteInt uses EAX = qutent

	exit
main ENDP

;------------------------------------------------
GCD PROC
; This finds GCD
; Gets values from stored values
;returns NA

;------------------------------------------------
		
	
		mov  edx,0					;zero out edx for remainder
		mov  eax,dword ptr[esp+12]  ;dividend
        mov  ebx,dword ptr[esp+8]   ;divisor
		div  ebx                    ;eax/ebx
		cmp  edx,0                  ;remainder in edx
		je   L1		                ;yes: quit
		call GCD		            ;no: call GCD agian
	L1:
		mov eax,ebx			        ;move the divisor into eax for printing i.e GCD	
	    call DumpRegs				;show what is in the registry
	  
		
    	ret 8                 ;clean up the stack
GCD ENDP

END main
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top