-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.asm
329 lines (276 loc) · 5.98 KB
/
functions.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
; functions.asm
sys_exit equ 1
sys_read equ 3
sys_write equ 4
stdin equ 0
stdout equ 1
stderr equ 3
sys_open equ 5
sys_close equ 6
O_RDONLY equ 0
O_RDWR equ 1
sys_creat equ 8
sys_sync equ 36
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; function string length (strlen) ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
strlen:
push ebx ;save the value of ebx in the stack
mov ebx, eax
nextchar:
cmp byte[eax], 0 ; compare eax with 0
jz finish ; if 0 jump to finish
inc EAX ; increment in 1 eax
jmp nextchar ; goes back
finish:
sub eax, ebx ; substract initial value
pop ebx ; brings back ebx
ret ; return to where the function was called
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; function string print (sprint) ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sprint:
push edx ; save values to stack
push ecx
push ebx
push eax
call strlen ; get string length (stored in eax)
mov edx, eax ; move length to edx
pop eax ; restore eax
mov ecx, eax ; move eax (message) to ecx
mov ebx, stdout
mov eax, sys_write ; sys_write
int 80h ; execute
pop ebx ; restore other values
pop ecx
pop edx
ret ; return to where the function was called
;; uses sprint and adds a new line at the end
sprintLF:
call sprint
push EAX
mov EAX, 0xA
push EAX
mov EAX, ESP
call sprint
pop EAX
pop EAX
ret
;; uses iprint and adds a new line at the end
iprintLF:
call iprint
push EAX
mov EAX, 0xA
push EAX
mov EAX, ESP
call sprint
pop EAX
pop EAX
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; function to print ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
iprint:
push eax ; save register values to the stack
push ecx
push edx
push esi
mov ecx, 0 ; initialize the counter
divloop:
inc ecx; ; increment (will be used to print later)
mov edx, 0
mov esi, 10
idiv esi
add edx, 48
push edx
cmp eax, 0
jnz divloop
printloop:
dec ecx
mov eax, esp
call sprint ; call print function
pop eax ; restore eax after print
cmp ecx, 0 ; keep printing until the end
jnz printloop ; jump back if not zero
pop esi ; restore values
pop edx
pop ecx
pop eax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;function to convert ascii to int;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
atoi:
push ebx ; save registers to the stack
push ecx
push edx
push esi
mov esi, eax ; number to convert in eax
mov eax, 0 ; initialize with 0
mov ecx, 0
multcycle: ; multiplication cycle
xor ebx, ebx
mov bl, [esi+ecx]
cmp bl, 48
jl finished
cmp bl, 57
jg finished
cmp bl, 10
je finished
cmp bl, 0
jz finished
sub bl, 48
add eax, ebx
mov ebx, 10
mul ebx
inc ecx
jmp multcycle
finished:
mov ebx, 10 ; decimal value 10 to ebx
div ebx ; eax/10
pop esi ; restore the values
pop edx
pop ecx
pop ebx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; rceives integer converts it to text;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
itoa:
push ebx ; save registers to the stack
push ecx
push edx
push esi
mov ebx, 10
mov ecx, 0
push ecx
inc ecx
.dividir:
inc ecx
mov edx, 0
idiv ebx
add edx,0x30
push edx
cmp eax, 0
je .fuera
jmp .dividir
.fuera:
mov ebx, 0
.guardar:
pop eax
mov byte[esi+ebx], al
inc ebx
cmp ebx, ecx
jne .guardar
pop esi
pop edx
pop ecx
pop ebx
ret
;; receives value in EDI and calculates it's square root
isqrt32:
mov ebx, edi
xor eax, eax
.while:
cmp eax, ebx
jnb .endwhile
add ebx, eax
shr ebx, 1
mov eax, edi
xor edx, edx
div ebx
jmp .while
.endwhile:
mov eax, ebx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; farenheit to celsius function ;;;
;;; receives farenheit in eax ;;;
;;; C = ((F-32)*5)/9 ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ftoc:
sub eax, 32
imul eax, 5
push edx ; save to stack
mov edx, 0
push ebx ; save to stack
mov ebx, 9
div ebx
pop ebx ; restore values
pop edx ; restore values
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; celsius to farenheit function ;;;
;;; receives farenheit in eax ;;;
;;; F = (C*9/5) + 32 ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ctof:
imul eax, 9
push ebx ; save to stack
push edx
mov edx, 0
mov ebx, 5
div ebx
add eax, 32
pop ebx ; restore values
pop edx ; restore values
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Function to print string from array ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
copystring:
push ecx ; save and clear registers
push ebx
mov ebx, 0
mov ecx, 0
mov ebx, eax
.sigcar:
mov bl, byte[eax]
cmp bl, 0xA
je .salto
mov byte[esi+ecx], bl ; moves a char to esi
cmp byte[eax],0 ; checks if it's done
jz .finalizar
.salto:
inc eax ; next letter
inc ecx ; so it doesn't rewrite a char
jmp .sigcar
.finalizar: ; restore values
pop ebx
pop ecx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Function to print string from array ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
stringcopy:
push ecx ; save and clear registers
push ebx
mov ebx, 0
mov ecx, 0
mov ebx, eax
.sigcar:
mov bl, byte[eax]
mov byte[esi+ecx], bl ; moves a char to esi
cmp byte[eax],0 ; checks if it's done
jz .finalizar
inc eax ; next letter
inc ecx ; so it doesn't rewrite a char
jmp .sigcar
.finalizar: ; restore values
pop ebx
pop ecx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; function to read from user ;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ReadText:
mov eax, sys_read
mov ebx, stdin
int 0x80
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; sequence to exit the program ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
quit:
mov eax, sys_exit ; exit program
int 0x80