Skip to content

Commit 7e3cddc

Browse files
committed
print_int: conserve stack memory
1 parent 38171e7 commit 7e3cddc

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

print_int/.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_int_pop_loop
6+
break main

print_int/print_int.s

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
.equ WRITE, 1
55
.equ EXIT, 60
66

7-
.data
8-
97
.text
108
.globl main
119

12-
# char rdi
13-
print_char:
10+
print_char: # debugging
1411
push %rbp
1512
mov %rsp, %rbp
1613
push %rax
@@ -44,7 +41,6 @@ write_string:
4441
syscall
4542
ret
4643

47-
# int rsi
4844
print_int:
4945
push %rbp
5046
mov %rsp, %rbp
@@ -62,41 +58,31 @@ print_int:
6258
print_int_push_loop:
6359
mov %rsi, %rax
6460
and $0xf, %rax
65-
add $0x30, %rax # %rax now contains ascii "0"-"9"
61+
add $0x30, %rax # %rax now contains ascii "0"-"9"
6662
cmp $0x39, %rax
6763
jle print_int_after_adjust
68-
add $39, %rax # adjust for ascii "a"-"f"
64+
add $39, %rax # adjust for ascii "a"-"f"
6965
print_int_after_adjust:
70-
movb %al, -64(%rbp, %rcx) #to do: use this instead of push
71-
push %rax
66+
movb %al, -64(%rbp, %rcx)
7267
inc %rcx
7368
shr $4, %rsi
7469
test %rsi, %rsi
7570
jnz print_int_push_loop
7671

77-
add $2, %rcx
78-
7972
mov $0, %rdx
8073

8174
movb $0x30, -128(%rbp, %rdx)
8275
inc %rdx
83-
8476
movb $0x78, -128(%rbp, %rdx)
8577
inc %rdx
8678

87-
print_int_pop_loop: # copy chars from stack
88-
89-
#mov -64(%rbp, %rdx), %rax
90-
#push %rax
91-
#call print_char
92-
#pop %rax
93-
94-
pop %rax
95-
79+
print_int_pop_loop:
80+
dec %rcx
81+
mov -64(%rbp, %rcx), %rax
9682
movb %al, -128(%rbp, %rdx)
9783
inc %rdx
98-
cmp %rdx, %rcx
99-
jne print_int_pop_loop
84+
test %rcx, %rcx
85+
jnz print_int_pop_loop
10086

10187
movb $0xa, -128(%rbp, %rdx)
10288
inc %rdx
@@ -121,7 +107,6 @@ main:
121107
add $0xa412, %rax
122108
push %rax
123109
call print_int
124-
pop %rsi
125110
jmp exit
126111

127112
exit:

0 commit comments

Comments
 (0)