Skip to content

Commit 38171e7

Browse files
committed
reserve stack space in print_int
1 parent fd3e09a commit 38171e7

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

print_int/print_int.s

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ print_char:
1818
push %rdx
1919
push %rsi
2020
push %rdi
21-
mov $WRITE, %rax
22-
mov %rdi, -128(%rbp)
21+
mov 16(%rbp), %rax
22+
movb $0x3e, -128(%rbp)
23+
mov %rax, -127(%rbp)
24+
movb $0x3c, -126(%rbp)
25+
movb $0xa, -125(%rbp)
2326
lea -128(%rbp), %rsi
27+
mov $WRITE, %rax
2428
mov $STDOUT, %rdi
25-
mov $1, %rdx
29+
mov $4, %rdx
2630
syscall
2731
pop %rdi
2832
pop %rsi
@@ -42,14 +46,17 @@ write_string:
4246

4347
# int rsi
4448
print_int:
45-
push %rax
4649
push %rbp
50+
mov %rsp, %rbp
51+
sub $128, %rsp
52+
53+
push %rax
4754
push %rsi
4855
push %rdx
4956
push %rcx
5057
push %rdi
51-
mov %rsp, %rbp # save stack pointer
5258

59+
mov 16(%rbp), %rsi
5360
mov $0, %rcx
5461

5562
print_int_push_loop:
@@ -60,7 +67,7 @@ print_int_push_loop:
6067
jle print_int_after_adjust
6168
add $39, %rax # adjust for ascii "a"-"f"
6269
print_int_after_adjust:
63-
#movb %al, -64(%rbp, %rcx) #to do: use this instead of push
70+
movb %al, -64(%rbp, %rcx) #to do: use this instead of push
6471
push %rax
6572
inc %rcx
6673
shr $4, %rsi
@@ -78,10 +85,13 @@ print_int_after_adjust:
7885
inc %rdx
7986

8087
print_int_pop_loop: # copy chars from stack
81-
pop %rax
8288

83-
mov %rax, %rdi
89+
#mov -64(%rbp, %rdx), %rax
90+
#push %rax
8491
#call print_char
92+
#pop %rax
93+
94+
pop %rax
8595

8696
movb %al, -128(%rbp, %rdx)
8797
inc %rdx
@@ -93,20 +103,25 @@ print_int_pop_loop: # copy chars from stack
93103

94104
lea -128(%rbp), %rsi
95105
call write_string
96-
mov %rbp, %rsp # restore stack pointer
106+
97107
pop %rdi
98108
pop %rcx
99109
pop %rdx
100110
pop %rsi
101-
pop %rbp
102111
pop %rax
112+
113+
mov %rbp, %rsp
114+
pop %rbp
115+
103116
ret
104117

105118
main:
106-
mov $0x9084, %rsi
107-
shl $16, %rsi
108-
add $0xa412, %rsi
119+
mov $0x9084, %rax
120+
shl $16, %rax
121+
add $0xa412, %rax
122+
push %rax
109123
call print_int
124+
pop %rsi
110125
jmp exit
111126

112127
exit:

0 commit comments

Comments
 (0)