-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path031.asm
42 lines (34 loc) · 1.35 KB
/
031.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
coins dd 1, 2, 5, 10, 20, 50, 100, 200 ;coin values
sums times 201 dd 0 ;array of sums, result will be in sums[200]
section .text
extern printf
global main
main:
mov dword [sums], 1 ;set sums[0] to 1
xor eax, eax ;coins index
sum_outer:
mov ebx, [coins + 4 * eax] ;coins[eax] in ebx
sum_inner:
mov edx, ebx ;copy ebx to edx
sub edx, [coins + 4 * eax] ;subtract coins[eax]
mov edi, [sums + 4 * edx] ;move result in edi
add [sums + 4 * ebx], edi ;add to sums[ebx]
inc ebx ;inc inner loop index
cmp ebx, 200 ;end of sums array?
jle sum_inner ;if lower, continue inner loop
inc eax ;else increase outer loop index
cmp eax, 8 ;end of coins array?
jl sum_outer ;if lower, start over with outer loop
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
mov dword esi, [sums + 4 * 200]
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc