-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgcd.asm
More file actions
53 lines (43 loc) · 810 Bytes
/
Copy pathpgcd.asm
File metadata and controls
53 lines (43 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%include "asm_io.asm"
global main
segment .data
msg_a db "Enter a: ", 0
msg_b db "Enter b: ", 0
msg_res db "GCD = ", 0
segment .bss
a resd 1
b resd 1
segment .text
main:
mov eax, msg_a
call print_string
call read_int
mov [a], eax
mov eax, msg_b
call print_string
call read_int
mov [b], eax
call gcd
mov ecx, eax
mov eax, msg_res
call print_string
mov eax, ecx
call print_int
call print_nl
mov eax, 1
mov ebx, 0
int 0x80
; gcd — reads directly from [a] and [b], returns result in eax
gcd:
mov eax, [a]
mov ebx, [b]
.loop:
cmp ebx, 0
je .done
mov edx, 0
div ebx ; eax = eax / ebx, edx = eax % ebx
mov eax, ebx ; a = b
mov ebx, edx ; b = remainder
jmp .loop
.done:
ret