-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.s
117 lines (98 loc) · 2.03 KB
/
day1.s
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
.global main
.extern getia
.extern printi
.extern part2set
.text
main:
call part2set
jc .Lpart2
.Lpart1:
# Read array
lea array(%rip), %rdi
mov $0x200, %rsi
call getia
mov %rax, %rsi # %rsi = N
# Now, need to do a loop sum
lea array(%rip), %rdi
xor %r8, %r8
jmp .Licheck
.Liloop:
lea 1(%r8), %r9 # j = i + 1
lea (%rdi, %r8, 8), %rax
mov (%rax), %r10 # %r10 = array[i]
jmp .Ljcheck
.Ljloop:
lea (%rdi, %r9, 8), %rax
mov (%rax), %r11 # %r11 = array[j]
lea (%r10, %r11), %r12 # %r12 = array[i] + array[j]
cmp $2020, %r12
jne .Ljincr # Continue looking, != 2020
# Found pair, calculate product and print
imul %r10, %r11
push %r11
lea result_fmt(%rip), %rdi
call printi
add $8, %rsp
ret
.Ljincr:
inc %r9
.Ljcheck:
cmp %rsi, %r9
jl .Ljloop
inc %r8
.Licheck:
cmp %rsi, %r8
jl .Liloop
ret
.Lpart2:
# Read array
lea array(%rip), %rdi
mov $0x200, %rsi
call getia
mov %rax, %rsi # %rsi = N
# Now, need to do a loop sum
lea array(%rip), %rdi
xor %r8, %r8
jmp .Licheck2
.Liloop2:
lea 1(%r8), %r9 # j = i + 1
lea (%rdi, %r8, 8), %rax
mov (%rax), %r11 # %r11 = array[i]
jmp .Ljcheck2
.Ljloop2:
lea 1(%r9), %r10 # k = j + 1
lea (%rdi, %r9, 8), %rax
mov (%rax), %r12 # %r12 = array[j]
jmp .Lkcheck2
.Lkloop2:
lea (%rdi, %r10, 8), %rax
mov (%rax), %r13 # %r13 = array[k]
lea (%r11, %r12), %r14 # %r14 = array[i] + array[j]
add %r13, %r14
cmp $2020, %r14
jne .Lkincr2 # Continue looking, != 2020
# Found pair, calculate product and print
imul %r11, %r13
imul %r12, %r13
push %r13
lea result_fmt(%rip), %rdi
call printi
add $8, %rsp
ret
.Lkincr2:
inc %r10
.Lkcheck2:
cmp %rsi, %r10
jl .Lkloop2
inc %r9
.Ljcheck2:
cmp %rsi, %r9
jl .Ljloop2
inc %r8
.Licheck2:
cmp %rsi, %r8
jl .Liloop2
ret
.data
array: .fill 0x1000
result_fmt: .string "@\n"