Skip to content

Commit d696d35

Browse files
committed
add print_int
1 parent 9df2967 commit d696d35

7 files changed

Lines changed: 113 additions & 9 deletions

File tree

cat/cat.s

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,3 @@ _start:
137137
call _close
138138
call _munmap
139139
call _exit
140-
141-
call _print_ouch
142-
143-
leave

hello/hello.s

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77
.text
88

9+
# void print_chars(int {rsi}, int {rdx});
10+
print_chars:
11+
movq $1, %rax
12+
movq $1, %rdi
13+
syscall
14+
ret
15+
916
_start:
1017
#https://stackoverflow.com/questions/29790175/assembly-x86-leave-instruction
1118
pushq %rbp
1219
movq %rsp, %rbp
1320

14-
movq $1, %rax
15-
movq $1, %rdi
1621
leaq .hello.str, %rsi
1722
movq $10, %rdx
18-
syscall
23+
call print_chars
1924

2025
movq $60, %rax
2126
movq $0, %rdi
2227
syscall
23-
24-
leave

print_int/.gdbinit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set debuginfod enabled off
2+
lay asm
3+
define hook-quit
4+
set confirm off
5+
end

print_int/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

print_int/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build:
2+
$(CC) print_int.s -c -g
3+
$(LD) -o print_int print_int.o
4+
5+
run: build
6+
./print_int

print_int/print_int

6.19 KB
Binary file not shown.

print_int/print_int.s

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#https://www.youtube.com/watch?v=_hbZN4khAyU
2+
#https://github.com/xmdi/SCHIZONE
3+
.equ READ, 0
4+
.equ STDOUT, 1
5+
.equ CLOSE, 3
6+
.equ WRITE, 1
7+
.equ OPEN, 2
8+
.equ FSTAT, 5
9+
.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
19+
20+
21+
.data
22+
23+
my_number:
24+
.quad 0x41
25+
26+
.text
27+
.globl _start
28+
29+
# void print_chars(char *rsi, int rdx);
30+
print_chars:
31+
movq $WRITE, %rax
32+
movq $STDOUT, %rdi
33+
syscall
34+
ret
35+
36+
# void print_int_h(int {rsi});
37+
# Prints hexadecimal value in {rsi} to stdout.
38+
_print_int_h:
39+
push %rax
40+
push %rbp
41+
push %rsi
42+
push %rdx
43+
44+
mov %rsp, %rbp # save base stack pointer
45+
46+
# value is kept in {rsi} and low bits are shifted off, four by four
47+
# do all arithmetic in {rax}
48+
49+
# move trailing '\n' onto stack
50+
push $0xa
51+
52+
_loop:
53+
54+
mov %sil, %al #
55+
and $15, %al # %al contains low nibble of %rsi
56+
add $48, %al # %al now correctly contains ascii "0"-"9"
57+
cmp $57, %al #
58+
jle _insert_byte
59+
add $39, %al # adjust %al for ascii "a"-"f"
60+
_insert_byte:
61+
dec %rsp
62+
mov %al, (%rsp) # move this ascii value into next slot on stack
63+
shr $4, %rsi # go on to next lowest bit
64+
test %rsi, %rsi # loop until nothing nonzero left
65+
jnz _loop
66+
67+
# move leading '0x' onto stack
68+
push $0x78 #x
69+
push $0x30 #0
70+
71+
# get ready to print
72+
mov %rbp, %rdx ##
73+
sub %rsp, %rdx ## %rdx will be length of number in bytes
74+
mov %rsp, %rsi ## address of top of stack
75+
call print_chars ## print out bytes
76+
mov %rbp, %rsp ## restore stack pointer
77+
78+
pop %rdx
79+
pop %rsi
80+
pop %rbp
81+
pop %rax
82+
83+
ret
84+
85+
_start:
86+
mov my_number, %rsi
87+
call _print_int_h
88+
jmp _exit
89+
90+
_exit:
91+
movq $EXIT, %rax
92+
movq $0, %rdi
93+
syscall

0 commit comments

Comments
 (0)