-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.asm
222 lines (184 loc) · 2.77 KB
/
io.asm
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
;rbx , rbp , rsp , or r12-r15 - callee saved -change back if changed
global _start
section .data
section .text
exit:
mov rax, 60
syscall
ret
string_length:
xor rax, rax
.L1:
cmp byte[rdi + rax], 0 ;comparing string character with null character
je .ex
inc rax
jmp .L1
.ex:
ret
print_string:
call string_length
mov rdx, rax ;move to number of characters to be printed
mov rax, 1 ;syscall number is 1
mov rsi, rdi ;move string to be printed to proper reg
mov rdi, 1 ;file descriptor
syscall
ret
print_char:
push rdi
mov rsi, rsp ;pointer to where character was pushed to
mov rax, 1 ;syscall number for printing on stdout
mov rdi, 1 ;file descriptor for stdout
mov rdx, 1 ;number of characters to be printed
syscall
pop rdi
ret
print_newline:
mov rdi, 10
call print_char
ret
print_uint:
mov rax, rdi
mov rdi, rsp
push 0
sub rsp, 16
dec rdi
mov r8, 10
.loop:
xor rdx, rdx
div r8
or dl, 0x30
dec rdi
mov [rdi], dl
test rax, rax
jnz .loop
call print_string
add rsp, 24
ret
print_int:
cmp rdi, 0
jge .positive
push rdi
mov rdi, '-'
call print_char
pop rdi
neg rdi
.positive:
call print_uint
ret
read_char:
sub rsp, 8
xor rax, rax
xor rdi, rdi
mov rsi, rsp
mov rdx, 1
syscall
pop rax
ret
read_word:
push r13
xor r13, r13
.L1:
cmp r13, rsi
je .err
push rdi
push rsi
call read_char
pop rsi
pop rdi
cmp rax, ' '
je .done
cmp rax, 13
je .done
cmp rax, 10
je .done
cmp rax, 9
je .done
mov byte[rdi + r13], al
inc r13
jmp .L1
.err:
xor rax, rax
jmp exit
.done:
mov byte[rdi + r13], 0
mov rax, rdi
.exit:
pop r13
ret
;rdi - null terminated string
parse_uint:
mov rcx, rdi
xor rax, rax
mov r8, 1
.L1:
cmp byte[rcx], 0
je .L2
inc rcx
jmp .L1
.L2:
dec rcx
mov rdx, [rcx]
and rdx, 0xf
imul rdx, r8
add rax, rdx
imul r8, 10
cmp rcx, rdi
jg .L2
.done:
ret
parse_int:
cmp byte[rdi], '-'
jne .uint
inc rdi
call parse_uint
neg rax
jmp .done
.uint:
call parse_uint
.done:
ret
string_equals:
xor rcx, rcx
.L1:
mov r8b, [rdi + rcx]
cmp r8b, byte[rsi + rcx]
jne .false
cmp r8b, 0 ;if equal check if end of string (null terminator)
je .true
inc rcx
jmp .L1
.true:
mov rax, 1
jmp .done
.false:
xor rax, rax
.done:
ret
;rdi - src, rsi - dest, rdx - count
string_copy:
xor rcx, rcx
.L1:
cmp rdx, rcx
jb .err
mov r8b, byte[rdi + rcx]
mov byte[rsi + rcx], r8b
inc rcx
test r8b, r8b
jnz .L1
mov rax, rsi
jmp .done
.err:
xor rax, rax
.done:
ret
_start:
sub rsp, 24
mov rdi, rsp
mov rsi, 24
call read_word
mov rdi, rax
call print_string
call print_newline
add rsp, 24
xor rdi, rdi
call exit