-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.asm
More file actions
225 lines (169 loc) · 4.67 KB
/
functions.asm
File metadata and controls
225 lines (169 loc) · 4.67 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
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
;---------------------------------------------------------------
; void print -- Prints message to stdout
;
; IN:
; EAX: Message to print
; EBX: Buffer size
; OUT:
; None
;---------------------------------------------------------------
print:
;save registers
push eax
push ebx
push ecx
push edx
mov ecx, eax ; Move message in ECX
mov edx, ebx ; Specify size
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
int 0x80 ; syscall
;recover registers
pop edx
pop ecx
pop ebx
pop eax
ret
;---------------------------------------------------------------
; void print_error -- Prints error and exits
;
; IN:
; EBX: String
; OUT:
; EAX: String length
;---------------------------------------------------------------
print_error:
; Print error
mov eax, 4 ; sys_write
mov ebx, 2 ; stderr
mov ecx, errorMsg
mov edx, lenErrorMsg
int 0x80 ; syscall
; Exit
mov eax, 1
int 0x80
;---------------------------------------------------------------
; int slen -- Finds length of a string
;
; IN:
; EBX: String
; OUT:
; EAX: String length
;---------------------------------------------------------------
slen:
; Save registers
push ebx
push ecx
xor ecx, ecx ; Clear ECX
do_slen:
cmp byte [ebx], 0 ; Is current char empty?
jz end_slen ; If yes, jump to end
inc ecx ; Increase counter
inc ebx ; Move onto next character
jmp do_slen ; Continue loop
end_slen:
mov eax, ecx ; Place return value in EAX
; Recover registers
pop ecx
pop ebx
ret
;---------------------------------------------------------------
; int to_int -- Converts string into integer
;
; IN:
; EBX: String
; OUT:
; EAX: Converted integer
;
; Notes:
; It is presumed, that string ends with \n
; TODO: change where result is stored (?)
;---------------------------------------------------------------
to_int:
; Save registers
push ebx
push ecx
push edx
call slen ; Get string length
mov ecx, eax ; Put length in ecx
dec ecx ; For \n
xor eax, eax ; EAX = 0
do_digit:
; Check if char is between 0 and 9
cmp byte [ebx], '0'
jb print_error
cmp byte [ebx], '9'
ja print_error
push eax ; Save EAX
push ebx ; Save EBX
; Put 10^ECX in eax
dec ecx ; Temporarily decrease for next function
mov ebx, 10
call power ; Call function
inc ecx ; Increase it back
pop ebx ; Recover
; EDX = int(ebx[n])
xor edx, edx
mov dl, [ebx] ; EDX = EBX[0]
sub edx, '0' ; EDX = EDX - '0'
; EAX *= EDX -> EAX = EBX[n] * 10^ECX
mul edx ; EAX = EDX * i32
mov edx, eax
pop eax
add eax, edx ; Result += EBX[0] * 10^ECX
inc ebx ; Move on next char
dec ecx ; Update counter
jnz do_digit
; Recover registers
pop edx
pop ecx
pop ebx
ret
;---------------------------------------------------------------
; int power -- Calculates power of a number
;
; IN:
; EBX: Base number
; ECX: Exponent
; OUT:
; EAX: Returns EBX^ECX
;
; Notes:
; Only works for exponent >= 0
;---------------------------------------------------------------
power:
; Save registers
push ecx ; Save ECX
push ebx ; Save EBX
xor eax, eax ; Empty result | EAX = 0
inc eax ; EAX = 1
cmp ecx, 0 ; Is ECX == 0?
je end_power ; If so, end function
do_power:
mul ebx ; EAX *= EBX
dec ecx ; Update counter
jnz do_power ; While counter != 0
end_power:
; Recover registers
pop ebx ; Recover EBX
pop ecx ; Recover ECX
ret
;---------------------------------------------------------------
; int remainder -- Returns remainder of divison
;
; IN:
; EAX: Dividend
; EBX: Divisor
; OUT:
; EAX: Remainder
;---------------------------------------------------------------
remainder:
push edx ; Save EDX
xor edx, edx ; Empty EDX
div ebx ; EDX:EAX / EBX | quotient -> EAX; remainder -> EDX
mov eax, edx ; Store result in eax
pop edx ; Restore EDX
ret ; Return
SECTION .data
errorMsg db "Undefined error occured. Exiting..."
lenErrorMsg equ $ - errorMsg