Skip to content

Commit f57cdc0

Browse files
committed
print_int: avoid stack push for 0, x and newline
1 parent 62df166 commit f57cdc0

2 files changed

Lines changed: 15 additions & 28 deletions

File tree

print_int/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
build:
2-
$(CC) print_int.s -c -g
3-
$(LD) -o print_int print_int.o
2+
$(CC) print_int.s -c
3+
$(LD) -e main -o print_int print_int.o
44

55
run: build
66
./print_int

print_int/print_int.s

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
#https://www.youtube.com/watch?v=_hbZN4khAyU
22
#https://github.com/xmdi/SCHIZONE
3-
.equ READ, 0
43
.equ STDOUT, 1
5-
.equ CLOSE, 3
64
.equ WRITE, 1
7-
.equ OPEN, 2
8-
.equ FSTAT, 5
95
.equ EXIT, 60
10-
.equ MMAP, 9
11-
.equ MUNMAP, 11
12-
.equ OFFSET_SIZE, 48
13-
.equ CHAR_O, 0x4f
14-
.equ CHAR_U, 0x55
15-
.equ CHAR_C, 0x43
16-
.equ CHAR_H, 0x48
17-
.equ BANG, 0x21
18-
.equ NEWLINE, 0xa
196

207
.data
218

229
.text
23-
.globl _start
10+
.globl main
2411

2512
# char *rsi, int rdx
2613
write_string:
@@ -38,8 +25,6 @@ print_int:
3825
push %rcx
3926
mov %rsp, %rbp # save stack pointer
4027
mov $0, %rcx
41-
push $0xa # "\n"
42-
inc %rcx
4328
print_int_push_loop:
4429
mov %rsi, %rax
4530
and $15, %rax
@@ -53,21 +38,21 @@ print_int_after_adjust:
5338
shr $4, %rsi
5439
test %rsi, %rsi
5540
jnz print_int_push_loop
56-
push $0x78 # "x"
57-
inc %rcx
58-
push $0x30 # "0"
59-
inc %rcx
6041
mov $0, %rdx
42+
movb $0x30, -128(%rbp, %rdx)
43+
inc %rdx
44+
movb $0x78, -128(%rbp, %rdx)
45+
inc %rdx
6146
print_int_pop_loop: # copy chars from stack
6247
pop %rax
6348
movb %al, -128(%rbp, %rdx)
6449
inc %rdx
6550
cmp %rsp, %rbp
6651
jne print_int_pop_loop
52+
movb $0xa, -128(%rbp, %rdx)
53+
inc %rdx
6754
lea -128(%rbp), %rsi
68-
mov %rcx, %rdx
6955
call write_string
70-
7156
mov %rbp, %rsp # restore stack pointer
7257
pop %rcx
7358
pop %rdx
@@ -76,12 +61,14 @@ print_int_pop_loop: # copy chars from stack
7661
pop %rax
7762
ret
7863

79-
_start:
80-
mov $0xdeadbeef, %rsi
64+
main:
65+
mov $0xdead, %rsi
66+
shl $16, %rsi
67+
add $0xbeef, %rsi
8168
call print_int
82-
jmp _exit
69+
jmp exit
8370

84-
_exit:
71+
exit:
8572
movq $EXIT, %rax
8673
movq $0, %rdi
8774
syscall

0 commit comments

Comments
 (0)