Skip to content

Commit 732fcbd

Browse files
committed
print_int: fix a bug where print_int clobbers registers
1 parent 4bd3043 commit 732fcbd

File tree

4 files changed

+115
-105
lines changed

4 files changed

+115
-105
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 copy
6+
break print_lines_loop

group3/group3.s

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ in:
1717
.quad 0
1818
out:
1919
.quad 0
20-
line:
20+
linebuf:
2121
.quad 0
2222
file_size:
2323
.quad 0
@@ -36,6 +36,7 @@ file:
3636

3737
get_memory:
3838
enter
39+
push_all
3940
mov $MMAP, %rax
4041
mov $0, %rdi #addr
4142
mov $3, %rdx #prot r=1 w=2
@@ -44,46 +45,56 @@ get_memory:
4445
mov $0, %r9 #offset
4546
mov $34, %r10 #flags map_private=0x02, map_anonymous=0x20
4647
syscall
48+
pop_all
4749
return
4850

4951
exit:
5052
enter
53+
push_all
5154
mov $EXIT, %rax
5255
mov $0, %rdi
5356
syscall
57+
pop_all
5458
return
5559

5660
open_file:
5761
enter
62+
push_all
5863
mov $OPEN, %rax
5964
mov $file, %rdi
6065
mov $0, %rsi
6166
mov $0644, %rdx
6267
syscall
6368
mov %rax, fh(%rip)
69+
pop_all
6470
return
6571

6672
stat_file:
6773
enter
74+
push_all
6875
mov $FSTAT, %rax
6976
mov fh(%rip), %rdi
7077
lea st, %rsi
7178
syscall
7279
mov 48(%rsi), %rbx
7380
mov %rbx, file_size(%rip)
81+
pop_all
7482
return
7583

7684
init_file:
7785
enter
86+
push_all
7887
mov $READ, %rax
7988
mov fh(%rip), %rdi
8089
mov in(%rip), %rsi
8190
mov file_size(%rip), %rdx
8291
syscall
92+
pop_all
8393
return
8494

8595
copy:
8696
enter
97+
push_all
8798
mov $0, %rcx
8899
cmp %rcx, file_size(%rip)
89100
je copy_done
@@ -97,18 +108,22 @@ copy_loop:
97108
cmp %rcx, file_size(%rip)
98109
jne copy_loop
99110
copy_done:
111+
pop_all
100112
return
101113

102114
read_line:
103115
enter
116+
push_all
117+
mov 16(%rbp), %rax
104118
sub $128, %rsp
105119
mov $0, %rcx
106120
cmp %rcx, file_size(%rip)
107121
je read_line_done
108122
mov in(%rip), %rbx
123+
add %rax, %rbx
109124
read_line_loop:
110125
mov %rcx, %rdi
111-
add line(%rip), %rdi
126+
add linebuf(%rip), %rdi
112127
mov (%rbx, %rcx), %rsi
113128
movb %sil, (%rdi)
114129
cmp $0xa, %sil
@@ -118,35 +133,21 @@ read_line_loop:
118133
jne read_line_loop
119134
read_line_done:
120135
mov %rcx, %rax
121-
return
122-
123-
copy_lines:
124-
enter
125-
sub $128, %rsp
126-
mov $0, %rcx
127-
cmp %rcx, file_size(%rip)
128-
je copy_lines_done
129-
mov in(%rip), %rbx
130-
copy_lines_loop:
131-
mov %rcx, %rdi
132-
add out(%rip), %rdi
133-
mov (%rbx, %rcx), %rsi
134-
movb %sil, (%rdi)
135-
inc %rcx
136-
cmp %rcx, file_size(%rip)
137-
jne copy_lines_loop
138-
copy_lines_done:
136+
pop_all
139137
return
140138

141139
close:
142140
enter
141+
push_all
143142
mov $CLOSE, %rax
144143
lea fh, %rdi
145144
syscall
145+
pop_all
146146
return
147147

148148
munmap:
149149
enter
150+
push_all
150151
mov $MUNMAP, %rax
151152
lea in, %rdi
152153
mov file_size(%rip), %rsi
@@ -155,28 +156,65 @@ munmap:
155156
lea out, %rdi
156157
mov file_size(%rip), %rsi
157158
syscall
159+
pop_all
160+
return
161+
162+
print_lines:
163+
enter
164+
push_all
165+
sub $128, %rsp
166+
mov $0, %rbx
167+
168+
print_lines_loop:
169+
170+
push %rbx
171+
call print_int
172+
pop %rbx
173+
174+
push %rbx
175+
call read_line
176+
177+
add %rax, %rbx
178+
179+
180+
push linebuf(%rip)
181+
push %rax
182+
call write_string
183+
pop %rax
184+
pop %rax
185+
pop %rax
186+
187+
call write_newline
188+
189+
inc %rbx
190+
191+
cmp $30, %rbx
192+
193+
jl print_lines_loop
194+
print_lines_done:
195+
push $0x31
196+
call write_char_debug
197+
pop_all
158198
return
159199

160200
main:
161201
enter
202+
push_all
162203
call open_file
163204
call stat_file
164205
call get_memory
165206
mov %rax, in(%rip)
166207
call get_memory
167208
mov %rax, out(%rip)
168209
call get_memory
169-
mov %rax, line(%rip)
210+
mov %rax, linebuf(%rip)
170211
call init_file
171-
call read_line
172-
#call copy
173-
174-
push line(%rip)
175-
push %rax
176-
call write_string
177-
call write_newline
178-
pop %rax
179-
pop %rax
212+
call print_lines
213+
#push $0xc
214+
#call read_line
215+
#push linebuf(%rip)
216+
#push %rax
217+
#call write_string
180218

181219
call close
182220
call munmap

print_int/main.s

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
.text
22
.globl main
33

4-
.include "../print_int/print_int.s"
4+
.include "print_int.s"
5+
6+
basic_method:
7+
enter
8+
sub $144, %rsp
9+
push_all
10+
mov 16(%rbp), %rsi
11+
mov $0, %rcx
12+
pop_all
13+
return
514

615
main:
716
mov $0x9084, %rax
817
shl $16, %rax
918
add $0xa412, %rax
1019
push %rax
1120
call print_int
12-
jmp exit
1321

1422
exit:
1523
movq $EXIT, %rax

0 commit comments

Comments
 (0)