-
Notifications
You must be signed in to change notification settings - Fork 0
/
005.asm
46 lines (38 loc) · 1.19 KB
/
005.asm
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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
section .text
extern printf
global main
main:
mov eax, 1 ;put 1 in eax
mov ebx, 1 ;put 1 in ebx, will be increased in the nest step
next:
inc ebx ;increase eax to compute GCD/LCM with next number
push rbx ;push both numbers on the stack for LCM later
push rax
gcd:
xchg eax, ebx ;get gcd (a, b), result in ebx
xor edx, edx
div ebx
mov eax, edx
test eax, eax
jnz gcd
lcm:
pop rax ;get original number 1 from the stack
xor edx, edx
div ebx ;divide by gcd
pop rbx ;get original number 2 from the stack
mul ebx ;multiply
cmp ebx, 20 ;check if ebx is still <= 20
jle next ;if it is, continue with next number
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
mov esi, eax
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc