-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.s
More file actions
106 lines (82 loc) · 2.38 KB
/
Copy pathprint.s
File metadata and controls
106 lines (82 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# =============================================================================
.global print_newline
.global print
.global print_u64
.global print_s64
# =============================================================================
.text
print_newline:
# ()
pushq $2561 # 0x0a01 = byte 1, 10, 0, 0, .....
movq %rsp, %rdi
callq print
addq $8, %rsp
retq
# =============================================================================
print:
# (* data)
# write(1, &data+1, data)
movzbq (%rdi), %rdx
leaq 1(%rdi), %rsi
movq $1, %rdi
movq $1, %rax
syscall
retq
# =============================================================================
print_u64:
# (number)
movq %rsp, %rsi
subq $24, %rsp
callq conv_u64
movq %rax, %rdi
callq print
addq $24, %rsp
retq
# =============================================================================
print_s64:
# (number)
movq %rsp, %rsi
subq $24, %rsp
callq conv_s64
movq %rax, %rdi
callq print
addq $24, %rsp
retq
# =============================================================================
conv_u64:
# (number, end of buffer[24]) -> ptr suitable for print()
# 24 bytes is safe for 64bit integer
# return: data ready to call print
movq %rdi, %rax # prepare to divide
movq $10, %rcx # for each iteration, divide by 10
xorq %r8, %r8
func_conv_u64_loop_start:
decq %rsi
xorq %rdx, %rdx
divq %rcx
addb $48, %dl # remainder + 48
movb %dl, (%rsi)
incb %r8b
testq %rax, %rax
jnz func_conv_u64_loop_start
decq %rsi
movb %r8b, (%rsi) # store length info
movq %rsi, %rax
retq
# =============================================================================
conv_s64:
# (number, end of buffer[24]) -> ptr suitable for print()
# 24 bytes is safe for 64bit integer
# return: data ready to call print
testq %rdi, %rdi
jns conv_u64
subq $8, %rsp
neg %rdi
callq conv_u64
movb (%rax), %dl # get counter
movb $45, (%rax) # add neg sign
incb %dl # inc counter
decq %rax # mov ptr
movb %dl, (%rax) # put counter back
addq $8, %rsp
retq