File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -137,7 +137,3 @@ _start:
137137 call _close
138138 call _munmap
139139 call _exit
140-
141- call _print_ouch
142-
143- leave
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ set debuginfod enabled off
2+ lay asm
3+ define hook-quit
4+ set confirm off
5+ end
Original file line number Diff line number Diff line change 1+ hello
Original file line number Diff line number Diff line change 1+ build :
2+ $(CC ) print_int.s -c -g
3+ $(LD ) -o print_int print_int.o
4+
5+ run : build
6+ ./print_int
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments