Assembly language I have this code but there is an error because when i insert a
ID: 3828102 • Letter: A
Question
Assembly language
I have this code but there is an error because when i insert a numberit always gives me the same GDC
Here is the code:
TITLE Greatest Common Divisor (gdc.asm)
INCLUDE Irvine32.inc
.data
str1 BYTE "Enter two integers: ",0
str2 BYTE "The GDC is: ",0
firstNo SDWORD ?
secondNo SDWORD ?
rem DWORD ?
.code
main PROC
call Clrscr
mov ecx,5
Label1:
call InputIntegers
call GDC
call Display
loop Label1
exit
main ENDP
InputIntegers PROC USES eax edx
mov edx, OFFSET str1
call WriteString
call ReadInt
mov firstNo,eax
call ReadInt
mov secondNo,eax
call Crlf
ret
InputIntegers ENDP
GDC PROC USES eax ebx edx
mov eax,firstNo
mov ebx,eax
sar ebx,31
add eax,ebx
xor eax,ebx
mov firstNo,eax
mov eax,secondNo
mov ebx,eax
sar ebx,31
add eax,ebx
xor eax,ebx
mov secondNo,eax
Label1:
mov eax,firstNo
mov edx,0
mov ebx,secondNo
div ebx
mov rem,edx
mov eax,ebx
mov ebx,rem
cmp secondNo,0
jb Label2
loop Label1
Label2:
mov eax,firstNo
ret
GDC ENDP
Display PROC USES edx
mov edx, OFFSET str2
call WriteString
call WriteDec
call Crlf
call Crlf
ret
Display ENDP
END main
Here is the output with the problem circle in red
CAWINDOWSAsystem321cmd.exe ab88, exe Enter two integer s 33 24 The GDC is 259225307 Enter two integers 12 20 he GDO is 259225307 Enter two integers: 71 19 he GDC is 5922530Explanation / Answer
To calculate the gcd try my code. This will be the simplest code you would have :
.LC0:
.string "Enter two positive integers: "
.LC1:
.string "%d %d"
.LC2:
.string "GCD = %d"
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rdx, [rbp-8]
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call scanf
.L5:
mov edx, DWORD PTR [rbp-4]
mov eax, DWORD PTR [rbp-8]
cmp edx, eax
je .L2
mov edx, DWORD PTR [rbp-4]
mov eax, DWORD PTR [rbp-8]
cmp edx, eax
jle .L3
mov edx, DWORD PTR [rbp-4]
mov eax, DWORD PTR [rbp-8]
sub edx, eax
mov eax, edx
mov DWORD PTR [rbp-4], eax
jmp .L5
.L3:
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-4]
sub edx, eax
mov eax, edx
mov DWORD PTR [rbp-8], eax
jmp .L5
.L2:
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, 0
leave
ret