Skip to content

Commit 1b6a0e3

Browse files
committed
group3: poc
1 parent a02cc9b commit 1b6a0e3

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

group3/.gdbinit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ lay asm
33
define hook-quit
44
set confirm off
55
end
6-
break print_lines_loop
6+
break string_compare

group3/group3.s

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
fh:
1212
.quad 0
13-
in:
13+
in: # address of in buffer
1414
.quad 0
15-
out:
15+
out: # address of out buffer
1616
.quad 0
1717
linebuf:
1818
.quad 0
@@ -31,6 +31,12 @@ file:
3131

3232
.include "../print_int/print_int.s"
3333

34+
.macro readln off
35+
push \off
36+
call read_line
37+
add $8, %rsp
38+
.endm
39+
3440
get_memory:
3541
enter
3642
push_all
@@ -108,11 +114,13 @@ copy_done:
108114
pop_all
109115
return
110116

117+
# read line from in into linebuf
118+
# return length in %rax
111119
read_line:
112120
enter
113121
sub $128, %rsp
114122
push_all
115-
mov 16(%rbp), %rax
123+
mov 16(%rbp), %rax # int offset
116124
mov $0, %rcx
117125
cmp %rcx, file_size(%rip)
118126
je read_line_done
@@ -156,35 +164,47 @@ munmap:
156164
pop_all
157165
return
158166

167+
168+
159169
print_lines:
160170
enter
161171
sub $128, %rsp
162172
push_all
163-
mov $0, %rbx
173+
mov $0, %rbx # input offset
174+
mov linebuf(%rip), %rsi
175+
mov %rbp, %rdi
176+
sub $64, %rdi # tmp
164177

165178
print_lines_loop:
166179

167-
push %rbx
168-
call read_line
180+
readln %rbx
181+
182+
# compare linebuf and tmp, store result in %r9
183+
push %rax
184+
strcmp %rsi, %rdi, $3
185+
mov %rax, %r9
186+
pop %rax
187+
188+
memcpy %rsi, %rdi, $3
169189

170190
add %rax, %rbx
171191

192+
test %r9, %r9
193+
jz print_lines_write_line
172194
write linebuf(%rip), $3
173195
writeln
196+
print_lines_write_line:
174197
write linebuf(%rip), %rax
175198
writeln
176-
plop
177-
178-
179199
inc %rbx
180-
181200
cmp file_size(%rip), %rbx
182-
183201
jl print_lines_loop
184202
print_lines_done:
185203
pop_all
186204
return
187205

206+
207+
188208
main:
189209
enter
190210
push_all
@@ -198,11 +218,6 @@ main:
198218
mov %rax, linebuf(%rip)
199219
call init_file
200220
call print_lines
201-
#push $0xc
202-
#call read_line
203-
#push linebuf(%rip)
204-
#push %rax
205-
#call write_string
206221

207222
call close
208223
call munmap

0 commit comments

Comments
 (0)