0

Hello!

I'm working on a project for class (Assembly Intel 8086) and am having trouble figuring out my problem with this program.

The project is to set-up to find GCD of two numbers. However, I can't figure out why the division loop I have isn't working properly. I have the full code and everything is ready to go and the output looks fine, but it just outputs the 2nd variable instead of the GCD.

Anyone with more experience know where I'm messing up?

```
;CPSC 2382: Introduction to Computer Systems and Assembly Language
;Spring 2010
;Project 3
;Programmer: NJW
;Descriptions: Compute GCD of two integers and display the results on the screen.
.LIST
TITLE Project 3 - Greatest Common Divisor (GCD.asm)
INCLUDE Irvine32.inc
.data
PrintAnd BYTE " and ",0
PrintMessage BYTE " share the following Greatest Common Divisor (GCD): ",0
PrintEndline BYTE " ",0dh, 0ah, 0
value1 DWORD 0 ; val1 and val2 are the integers used when finding GCD
value2 DWORD 0
.code
main PROC
mov value1, 84 ; first, we'll find the GCD of 84 and 18
mov value2, 18
call DisplayGCD ; call the DisplayGCD subroutine to print messages to screen
call GCD ; call the GCD subroutine to determine GCD of 84 and 18
mov value1, 111 ; next, we'll find the GCD of 111 and 13
mov value2, 13
call DisplayGCD ; call the DisplayGCD subroutine to print messages to screen
call GCD ; call the GCD subroutine to determine GCD of 111 and 13
mov value1, 180 ; next, we'll find the GCD of 180 and 48
mov value2, 48
call DisplayGCD ; call the DisplayGCD subroutine to print messages to screen
call GCD ; call the GCD subroutine to determine GCD of 180 and 48
exit ; halts the program
main ENDP ; marks the ends of the main procedure
;----------------------------------------------------------
DisplayGCD PROC
;
; When this subroutine is called, it will print out 2
; values and will prepare for when the GCD subroutine is
; called which will print the GCD. This display subroutine
; will show this each time: "[value1] and [value2] share
; the following Greatest Common Divisor (GCD): ".
;----------------------------------------------------------
mov eax, 0
mov edx, 0
mov eax, value1 ; prepare to print integer
call WriteDec ; print value1 to the screen
mov edx, OFFSET PrintAnd ; mov the message to edx so it can print to the screen
call WriteString ; print " and " to the screen
mov eax, value2 ; prepare to print integer
call WriteDec ; print value2 to the screen
mov edx, OFFSET PrintMessage ; mov the message to edx so it can print to the screen
call WriteString ; print "share the following Greatest Common Divisor (GCD): "
ret
DisplayGCD ENDP ; marks the ends of the main procedure
;----------------------------------------------------------
GCD PROC
;
; Takes 2 values from main and loops a set of division
; instructions in order to determine GCD. It will then
; return to main and do the same with other given integers.
;----------------------------------------------------------
mov edx, value1 ; mov value1 (dividend) into dx
cdq
mov eax, value2 ; mov value2 (divisor) into ax
div eax ; for 84 and 18 example, ax=4 and remainder in dx=12
L1: ; loop these instructions until GCD is determined
cmp edx, 0 ; if remainder is 0, then GCD is found and can end procedure
je ENDGCD
mov eax, edx ; GCD algorithm needs the remainder to be in divisor slot
mov edx, value2 ; mov value2 to dividend to prepare for division
mov value2, eax ; for the rest of the GCD algorithm to work, must save previous remainder
div eax ; for 84 and 18 example, ax=1 and remainder in dx=6
loop L1 ; loop until remainder is zero and GCD has been determined
ENDGCD:
mov eax, value2 ; mov value2, which is the remainder that resulted in the GCD to eax
call WriteDec ; print the GCD to the screen
mov edx, OFFSET PrintEndline
call WriteString
ret ; return to find the GCD of other integers
GCD ENDP ; marks the ends of the main procedure
END main ; END directive marks the last line of the program
```