Skip to content

Commit 4eca3f7

Browse files
committed
group3: add comments
1 parent 1b6a0e3 commit 4eca3f7

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
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 string_compare
6+
break print_lines_loop

group3/group3.s

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
fh:
1212
.quad 0
13-
in: # address of in buffer
13+
inbuf: # address of in buffer
1414
.quad 0
1515
out: # 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
105105
copy_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
119119
read_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
129129
read_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:])
169175
print_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)
178183
print_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
196196
print_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

print_int/print_int.s

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,11 @@ write_string:
144144

145145
print_int:
146146
enter
147-
148147
sub $48, %rsp
149148
push_all
150-
mov 16(%rbp), %rsi # param: number to print
151149
push %rax
152-
153-
mov $0, %rcx
150+
mov 16(%rbp), %rsi # int n
151+
mov $0, %rcx # int count
154152

155153
print_int_push_loop:
156154
mov %rsi, %rax
@@ -185,11 +183,7 @@ print_int_pop_loop:
185183
inc %rdx
186184

187185
lea -48(%rbp), %rsi
188-
push %rsi
189-
push %rdx
190-
call write_string
191-
plop
192-
plop
186+
write %rsi, %rdx
193187

194188
pop %rax
195189
pop_all

0 commit comments

Comments
 (0)