0

I'm working on a program to get the reduce form of a fraction by finding the gcd of the fraction. It works for some while for others it doesn't work. I noticed it when I tried entering in -7, 6, 6, 2 for the numbers. The reduce form of the product of the two fractions should be -7/2, but instead it is -3/1, but if I use -6, 2, 4, 3 the program works fine. Any help would be appreciated.

Sincerely yours;

jdm

```
TITLE MASM Template (main.asm)
INCLUDE Irvine32.inc
.data ;Declare values
myMessage BYTE "Enter first numerator: ", 0 ;Stores a string
myMessage2 BYTE "Enter first denominator: ",0 ;Stores a string
myMessage3 BYTE "Enter second numerator: ",0 ;Stores a string
myMessage4 BYTE "Enter second denominator: ",0 ;Stores a string
myMessage5 BYTE "/", 0 ;Stores a /
myMessage6 BYTE "Your first fraction is: ",0 ;Stores a string
myMessage7 BYTE "Your second fraction is: ",0 ;Stores a string
myMessage8 BYTE "The product of these two are: ",0 ;Stores a string
myMessage9 BYTE "The reduced form is: ",0 ;Stores a string
myMessage10 BYTE "Do you want to do it again: ",0 ;Stores a string
myMessage11 BYTE "Terminating...",0dh,0ah,0 ;Stores an exiting message
myMessage12 BYTE " ",0dh, 0ah, 0 ;Stores a space so I can do a new line
myMessage13 BYTE "You can't store a 0 in the denominator",0dh, 0ah, 0 ;Stores a string
n1 SDWORD 0 ;A variable to store the first numerator
n2 SDWORD 0 ;A variable to store the first denominator
d1 SDWORD 0 ;A variable to store the second numerator
d2 SDWORD 0 ;A variable to store the second denominator
temp SDWORD 0 ;Temp variable to store the product of the numerator
temp2 SDWORD 0 ;Temp variable to store the product of the denominator
temp3 SDWORD 0 ;Temp variable to store
temp4 SDWORD 0 ;Temp variable to store
temp5 SDWORD 0 ;Temp variable to store the product of the numerator that gets manipulated
temp6 SDWORD 0 ;Temp variable to store the product of the denominator that gets manipulated
.code ;Declare main
main PROC ;Declare proc
top: ;jump to the top if the user wants to re-run the program
call Clrscr ;Clears the screen
mov eax, 0 ;Clear eax
mov ebx, 0 ;Clear ebx
mov ecx, 0 ;Clear ecx
mov edx, OFFSET myMessage ;Mov message into edx
call writestring ;Display to screen - Prompting the user for the first numerator
call readint ;Reads the input number and store it into eax
mov n1, eax ;Mov the first numerator into n1
top2: ;Jumps here if the user enters in a 0 or negative number in the denominator
mov edx, OFFSET myMessage2 ;Mov message into edx
call writestring ;Display Message to screen - Prompting the user for the first denominator
call readint ;Read the input number and store it into eax
mov d1, eax ;Store the first denominator into d1
cmp d1, 0 ;compare d1 to 0
je wrong2 ;jump if d1 is equal to 0
jmp sec3 ;If d1 is greater than 0 than it jumps to the next part of the program
wrong2: ;Section Wrong2 - Tells the user they can't store 0 in the denominator
call wrong ;Calls PROC wrong
jmp top2 ;jumps to top2 - prompts the user again for a denominator
sec3: ;Continue with program
mov edx, OFFSET myMessage3 ;Mov string into edx
call writestring ;Display message on screen - Prompts the user for the second numerator
call readint ;Stores the second numerator into eax
mov n2, eax ;Mov the second numerator into n2
top3: ;Jumps here is the second denominator is equal to 0
mov edx, OFFSET myMessage4 ;Mov string into edx
call writestring ;Prompts the user for the second denominator
call readint ;Stores the second denominator into eax
mov d2, eax ;Mov the second denominator into d2
cmp d2, 0 ;Compare d2 to 0
je wrong3 ;Jump is d2 is equal to 0
jmp sec4 ;Jump to continue program is greater than 0
wrong3: ;Section - tells user 0 can't be stored in the denominator
call wrong ;Calls PROC wrong
jmp top3 ;jumps to prompt the user again
sec4: ;continue program
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clearing a line
mov edx, OFFSET myMessage6 ;Mov string into edx
call writestring ;Showing the first fraction
mov eax, n1 ;Mov the first numerator into eax
call writeint ;Display the first numerator
mov edx, OFFSET myMessage5 ;Mov string into edx
call writestring ;Displaying the /
mov eax, d1 ;Mov the first denominator into eax
call writeint ;Display the first denominator
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage7 ;Mov string into edx
call writestring ;Displaying the second fraction
mov eax, n2 ;Mov the second numerator into eax
call writeint ;Displaying the second numerator
mov edx, OFFSET myMessage5 ;Mov string into edx
call writestring ;Display the /
mov eax, d2 ;Mov the second denominator into eax
call writeint ;Displaying the second denominator
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage8 ;Mov string into edx
call writestring ;Displaying the product of the two fractions
mov eax, n1 ;Mov the first numerator into eax
mul n2 ;n1 x n2 store into eax
mov temp, eax ;Stores the product into temp
mov eax, d1 ;Mov the first denominator into eax
mul d2 ;d1 x d2 store into eax
mov temp2, eax ;Stores the product into temp2
mov eax, temp ;Mov the product of the numerator into eax
call writeint ;Display the product of the numerator
mov edx, OFFSET myMessage5 ;Mov string into edx
call writestring ;Display /
mov eax, temp2 ;Mov the product of the denominator into eax
call writeint ;Display the product of the denominator
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage9 ;Mov string into edx
call writestring ;Displaying the reduce form of the product
mov eax, temp ;Mov the product of the numerator into eax
mov temp5,eax ;Mov the product of the numerator into temp5
mov eax, temp2 ;Mov the product of the denominator into eax
mov temp6, eax ;Mov the product of the denominator into temp6
mov edx, 0 ;Mov 0 into edx
cmp temp5, 0 ;Compare the product of the numerator to 0
jl convert ;If the product is negative convert to positive
jmp regular ;If positive continue with program
convert: ;Convert negative to positive
mov eax, temp5 ;Mov the product of the numerator into eax
cdq ;Convert from DWORD to QWORD
xor eax, edx ;Exclusive or eax and edx
sub eax, edx ;eax - edx
mov temp5, eax ;Store the positive number into temp5
jmp regular ;continue with program
regular: ;continue program
call gcd ;Find the gcd of the product of the numerator and denominator
mov edx, 0 ;Set edx to 0
mov eax,temp5 ;Mov the numerator product into eax
div temp3 ;Divide by the gcd value
cmp temp, 0 ;Compare the original product of the numerator with 0
jl correctionnegative ;If the original product of the numerator was negative then it will make the reduce form of the numerator product negative
jmp cresume ;If original was positive then continue
correctionnegative: ;Make negative
neg eax ;Make eax negative
jmp cresume ;resume program
cresume: ;resume
call writeint ;Display the reduce form of the numerator
mov edx, OFFSET myMessage5 ;Mov string into edx
call writestring ;Display /
mov edx, 0 ;Set edx to 0
mov eax,temp2 ;Mov the product of the denominator into eax
div temp3 ;Divide the denominator by the gcd value
call writeint ;Display reduce form of the denominator
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage10 ;Mov string into edx
call writestring ;Prompts the user if they want to run the program again
call readchar ;Fetchs the user response
cmp al, 'y' ;compare the value to y
je top ;if value equals y then jump to the beginning of the pgroam
cmp al, 'Y' ;compare the value to Y
je top ;if value equals Y then jump to the beginning of the program else exit
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage12 ;Mov stirng into edx
call writestring ;Clear line
mov edx, OFFSET myMessage11 ;Mov string into edx
call writestring ;Show terminating message
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
exit ;Exit main
main ENDP ;Exit proc
wrong PROC ;Wrong PROC
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;Clear line
mov edx, OFFSET myMessage13 ;Mov string into edx
call writestring ;Tell user off
mov edx, OFFSET myMessage12 ;Mov string into edx
call writestring ;clear line
ret ;return
wrong ENDP ;End of wrong PROC
gcd PROC ;gcd PROC
mov eax,temp5 ;Mov product of the numerator into eax
cdq ;convert to qword
mov ebx,temp6 ;Mov product of the denominator into ebx
mov eax, ebx ;Mov ebx into eax
mov edx, 0 ;Set edx to 0
div ebx ;Divide eax by ebx
L1: ;loop
mov eax,ebx ;store ebx into eax
mov ebx,edx ;store edx into ebx
cmp ebx,0 ;compare ebx to 0
je endgcd ;if equal to zero exit
mov temp4,edx ;store remainder into temp4
cdq ;convert to qword
mov edx, 0 ;set edx to 0
div ebx ;divide eax by ebx
loop L1 ;loop
endgcd: ;exit
mov temp3, eax ;store eax to temp3
ret ;return
gcd ENDP ;end of gcd PROC
END main ;Tells the program that it is the end
```