1010
1111fh:
1212 .quad 0
13- in : # address of in buffer
13+ inbuf : # address of in buffer
1414 .quad 0
1515out: # address of out buffer
1616 .quad 0
@@ -89,7 +89,7 @@ init_file:
8989 push_all
9090 mov $READ, %rax
9191 mov fh(%rip ), %rdi
92- mov in (%rip ), %rsi
92+ mov inbuf (%rip ), %rsi
9393 mov file_size(%rip ), %rdx
9494 syscall
9595 pop_all
@@ -101,7 +101,7 @@ copy:
101101 mov $0 , %rcx
102102 cmp %rcx , file_size(%rip )
103103 je copy_done
104- mov in (%rip ), %rbx
104+ mov inbuf (%rip ), %rbx
105105copy_loop:
106106 mov %rcx , %rdi
107107 add out (%rip ), %rdi
@@ -114,8 +114,8 @@ copy_done:
114114 pop_all
115115 return
116116
117- # read line from in into linebuf
118- # return length in %rax
117+ # read line (excluding newline) from inbuf into linebuf, starting at given offset
118+ # return number of characters read in %rax
119119read_line:
120120 enter
121121 sub $128 , %rsp
@@ -124,7 +124,7 @@ read_line:
124124 mov $0 , %rcx
125125 cmp %rcx , file_size(%rip )
126126 je read_line_done
127- mov in (%rip ), %rbx
127+ mov inbuf (%rip ), %rbx
128128 add %rax , %rbx
129129read_line_loop:
130130 mov %rcx , %rdi
@@ -154,7 +154,7 @@ munmap:
154154 enter
155155 push_all
156156 mov $MUNMAP, %rax
157- lea in , %rdi
157+ lea inbuf , %rdi
158158 mov file_size(%rip ), %rsi
159159 syscall
160160 mov $MUNMAP, %rax
@@ -165,36 +165,40 @@ munmap:
165165 return
166166
167167
168-
168+ # tmp = '___'
169+ # with open('data.txt') as file:
170+ # while line := file.readline():
171+ # if not line.startswith(tmp):
172+ # print(line[:3])
173+ # tmp = line[:3]
174+ # print('\t' + line.rstrip()[3:])
169175print_lines:
170176 enter
171177 sub $128 , %rsp
172178 push_all
173- mov $0 , %rbx # input offset
179+ mov $0 , %rbx # int offset (offset in inbuf)
174180 mov linebuf(%rip ), %rsi
175181 mov %rbp , %rdi
176- sub $64 , %rdi # tmp
177-
182+ sub $64 , %rdi # char tmp[3] (start of previous line)
178183print_lines_loop:
179-
180- readln %rbx
181-
182- # compare linebuf and tmp, store result in %r9
184+ readln %rbx # read line into linebuf
185+ # %rax now contains the length
183186 push %rax
184- strcmp %rsi , %rdi , $3
187+ strcmp %rsi , %rdi , $3 # check linebuf.startswith(tmp), move result to %r9
185188 mov %rax , %r9
186189 pop %rax
187-
188- memcpy %rsi , %rdi , $3
189-
190- add %rax , %rbx
191-
190+ memcpy %rsi , %rdi , $3 # update tmp
191+ add %rax , %rbx # add length to offset (this skips the newline)
192192 test %r9 , %r9
193193 jz print_lines_write_line
194194 write linebuf(%rip ), $3
195195 writeln
196196print_lines_write_line:
197- write linebuf(%rip ), %rax
197+ mov linebuf(%rip ), %r10
198+ add $2 , %r10 # skip two chars from linebuf
199+ movb $0x9 , (%r10 ) # replace third char with a tab
200+ sub $2 , %rax
201+ write %r10 , %rax
198202 writeln
199203 inc %rbx
200204 cmp file_size(%rip ), %rbx
@@ -211,7 +215,7 @@ main:
211215 call open_file
212216 call stat_file
213217 call get_memory
214- mov %rax , in (%rip )
218+ mov %rax , inbuf (%rip )
215219 call get_memory
216220 mov %rax , out (%rip )
217221 call get_memory
0 commit comments