Skip to content

Commit e8c5a06

Browse files
feat(day1): complete day 1 task
1 parent 0bc42a9 commit e8c5a06

File tree

5 files changed

+585
-2
lines changed

5 files changed

+585
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is a repository contiaining the solution for advent of code problems of 202
66

77
Here is the list of the problems solved and language used to solve them
88

9-
- [ ] Day 1 - Assembly
9+
- [x] Day 1 - Assembly
1010
- [ ] Day 2
1111
- [ ] Day 3
1212
- [ ] Day 4

day-1/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
*.in
12
*.o
2-
*.out
3+
solution
4+
solution2

day-1/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
solution.o: solution.asm
4+
nasm -f elf64 solution.asm -o solution.o
5+
6+
solution: solution.o
7+
ld solution.o -o solution
8+
9+
build: solution
10+
11+
build2: solution2
12+
13+
solution2: solution2.o
14+
ld solution2.o -o solution2
15+
16+
solution2.o: solution2.asm
17+
nasm -f elf64 solution2.asm -o solution2.o
18+
19+
run: build
20+
./solution
21+
22+
clean:
23+
rm solution.o
24+
rm solution

day-1/solution.asm

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
SYS_WRITE equ 1 ; write text to stdout
2+
SYS_READ equ 0 ; read text from stdin
3+
SYS_EXIT equ 60 ; terminate the program
4+
STDOUT equ 1 ; stdout
5+
STDIN equ 0 ; stdin
6+
LENGTH equ 1 ; Length to read
7+
NEWLINE equ 10; ASCII newline
8+
9+
;--------------------------------------------------------------------------
10+
11+
section .bss
12+
current resb 1 ; To store the current
13+
first resb 1 ; To store the first digit
14+
second resb 1 ; To store the second digit
15+
first_done resb 1 ; To store the second digit
16+
total resq 1 ; To store the total
17+
buffer resb 22
18+
19+
20+
;--------------------------------------------------------------------------
21+
22+
section .text
23+
global _start
24+
25+
26+
input:
27+
mov rax, SYS_READ
28+
mov rdi, STDIN
29+
mov rdx, LENGTH
30+
mov rsi, current
31+
syscall
32+
33+
34+
cmp rax, 0
35+
je reset_exit
36+
37+
ret
38+
39+
exit:
40+
xor edi, edi
41+
mov rax, SYS_EXIT
42+
syscall
43+
44+
output:
45+
mov rax, rdi
46+
mov rdi, buffer + 20
47+
mov rcx, 10
48+
mov rbx, 0
49+
50+
.repeat:
51+
dec rdi
52+
xor rdx, rdx
53+
div rcx
54+
add dl, '0'
55+
mov [rdi], dl
56+
inc rbx
57+
58+
test rax, rax
59+
jnz .repeat
60+
61+
; rbx now contains the number of digits in the string
62+
63+
; Move the string to the beginning of the buffer
64+
65+
mov rsi, rdi
66+
add rsi, 20
67+
mov byte [rsi], NEWLINE
68+
add rsi, 1
69+
mov byte [rsi], 0
70+
mov rsi, rdi
71+
mov rdi, STDOUT ; Use STDOUT as file descriptor
72+
sub rdx, rbx ; Calculate string length
73+
mov rax, SYS_WRITE ; syscall number for sys_write
74+
syscall
75+
76+
ret
77+
78+
main:
79+
call input
80+
81+
movzx rax, byte [current]
82+
83+
cmp rax, NEWLINE
84+
je reset
85+
86+
87+
cmp rax, '0'
88+
jl main
89+
cmp rax, '9'
90+
jg main
91+
92+
sub byte [current], '0'
93+
94+
cmp byte [first_done], 0
95+
je do_first
96+
97+
jmp do_second
98+
99+
100+
call exit
101+
102+
do_first:
103+
mov al, byte [current]
104+
mov byte [first], al
105+
mov byte [second], al
106+
mov byte [first_done], 1
107+
jmp main
108+
109+
do_second:
110+
mov al, byte [current]
111+
mov byte [second], al
112+
jmp main
113+
114+
do_stuff:
115+
movzx rax, byte [first]
116+
imul rax, 10
117+
add qword [total], rax
118+
movzx rax, byte [second]
119+
add qword [total], rax
120+
ret
121+
122+
reset:
123+
call do_stuff
124+
125+
mov byte [first], 0
126+
mov byte [second], 0
127+
mov byte [first_done], 0
128+
129+
mov rdi, qword [total]
130+
call output
131+
132+
jmp main
133+
134+
reset_exit:
135+
call do_stuff
136+
137+
mov rdi, qword [total]
138+
call output
139+
140+
call exit
141+
142+
143+
144+
_start:
145+
mov qword [total], 0
146+
mov byte [first], 0
147+
mov byte [second], 0
148+
mov byte [first_done], 0
149+
150+
call main
151+
152+
153+

0 commit comments

Comments
 (0)