hos/kernel/lst/asmfuncs.lst

511 lines
27 KiB
Plaintext

1 ; asmfuncs.asm
2 ; Josh Holtrop
3 ; Created: 10/23/03
4 ; Modified: 02/26/04
5
6 [extern _putc]
7 [extern _console_memory]
8 [extern _cursorPosition]
9 [extern _video_drawConsole]
10 [extern _videoMode]
11
12 %macro jzfar 1
13 jnz %%skip
14 jmp %1
15 %%skip:
16
17 %endmacro
18
19 ;stores the parameter to the CR0 register
20 ;extern dword write_cr0(dword cr0);
21 [global _write_cr0]
22 _write_cr0:
23 00000000 55 push ebp
24 00000001 89E5 mov ebp, esp
25 00000003 8B4508 mov eax, [ebp+8]
26 00000006 0F22C0 mov cr0, eax
27 00000009 5D pop ebp
28 0000000A C3 ret
29
30 ;returns the value in the CR0 register
31 ;extern dword read_cr0();
32 [global _read_cr0]
33 _read_cr0:
34 0000000B 0F20C0 mov eax, cr0;
35 0000000E C3 ret
36
37 ;stores the parameter to the CR3 register
38 ;extern dword write_cr3(dword cr3);
39 [global _write_cr3]
40 _write_cr3:
41 0000000F 55 push ebp
42 00000010 89E5 mov ebp, esp
43 00000012 8B4508 mov eax, [ebp+8]
44 00000015 0F22D8 mov cr3, eax
45 00000018 5D pop ebp
46 00000019 C3 ret
47
48
49 ;returns the value in the CR2 register
50 ;extern dword read_cr2();
51 [global _read_cr2]
52 _read_cr2:
53 0000001A 0F20D0 mov eax, cr2;
54 0000001D C3 ret
55
56
57
58 ;returns the value in the CR3 register
59 ;extern dword read_cr3();
60 [global _read_cr3]
61 _read_cr3:
62 0000001E 0F20D8 mov eax, cr3;
63 00000021 C3 ret
64
65
66 ;compares one string to another
67 ;returns 0 if the strings are different
68 ;extern dword strcmp(char *str1, char *str2);
69 [global _strcmp]
70 _strcmp:
71 00000022 55 push ebp
72 00000023 89E5 mov ebp, esp
73 00000025 56 push esi
74 00000026 57 push edi
75
76 00000027 8B7508 mov esi, [ebp+8]
77 0000002A 8B7D0C mov edi, [ebp+12]
78 strcmp_loop1:
79 0000002D AC lodsb
80 0000002E 8A27 mov ah, [edi]
81 00000030 47 inc edi
82 00000031 38C4 cmp ah, al
83 00000033 750D jnz strcmp_ne
84 00000035 08C0 or al, al
85 00000037 7402 jz strcmp_e
86 00000039 EBF2 jmp strcmp_loop1
87 strcmp_e:
88 0000003B B801000000 mov eax, 1
89 00000040 EB02 jmp short strcmp_done
90 strcmp_ne:
91 00000042 31C0 xor eax, eax
92 strcmp_done:
93
94 00000044 5F pop edi
95 00000045 5E pop esi
96 00000046 5D pop ebp
97 00000047 C3 ret
98
99 ;copies a string from the source to the destination parameter
100 ;extern void strcpy(char *dest, char *src);
101 [global _strcpy]
102 _strcpy:
103 00000048 55 push ebp
104 00000049 89E5 mov ebp, esp
105 0000004B 56 push esi
106 0000004C 57 push edi
107 0000004D 8B7D08 mov edi, [ebp+8]
108 00000050 8B750C mov esi, [ebp+12]
109 strcpyloop:
110 00000053 AC lodsb
111 00000054 AA stosb
112 00000055 08C0 or al, al
113 00000057 75FA jnz strcpyloop
114 00000059 5F pop edi
115 0000005A 5E pop esi
116 0000005B 5D pop ebp
117 0000005C C3 ret
118
119 ;copies memory of n bytes from src to destination
120 ;void memcpy(void *dest, void *src, dword n);
121 [global _memcpy]
122 _memcpy:
123 0000005D 55 push ebp
124 0000005E 89E5 mov ebp, esp
125 00000060 56 push esi
126 00000061 57 push edi
127 00000062 51 push ecx
128 00000063 8B7D08 mov edi, [ebp+8]
129 00000066 8B750C mov esi, [ebp+12]
130 00000069 8B4D10 mov ecx, [ebp+16]
131
132 0000006C FC cld
133 0000006D F3A4 rep movsb
134
135 0000006F 59 pop ecx
136 00000070 5F pop edi
137 00000071 5E pop esi
138 00000072 5D pop ebp
139 00000073 C3 ret
140
141
142 ;copies memory of n dwords (n*4 bytes) from src to destination
143 ;void memcpyd(void *dest, void *src, dword n);
144 [global _memcpyd]
145 _memcpyd:
146 00000074 55 push ebp
147 00000075 89E5 mov ebp, esp
148 00000077 56 push esi
149 00000078 57 push edi
150 00000079 51 push ecx
151 0000007A 8B7D08 mov edi, [ebp+8]
152 0000007D 8B750C mov esi, [ebp+12]
153 00000080 8B4D10 mov ecx, [ebp+16]
154
155 00000083 FC cld
156 00000084 F3A5 rep movsd
157
158 00000086 59 pop ecx
159 00000087 5F pop edi
160 00000088 5E pop esi
161 00000089 5D pop ebp
162 0000008A C3 ret
163
164
165 ;sets num bytes at buffer to the value of c
166 ;void *memset(void *buffer, int c, int num);
167 [global _memset]
168 _memset:
169 0000008B 55 push ebp
170 0000008C 89E5 mov ebp, esp
171 0000008E 57 push edi
172 0000008F 51 push ecx
173 00000090 8B7D08 mov edi, [ebp+8]
174 00000093 57 push edi ;save for return address
175 00000094 8B450C mov eax, [ebp+12]
176 00000097 8B4D10 mov ecx, [ebp+16]
177
178 0000009A F3AA rep stosb
179
180 0000009C 58 pop eax
181 0000009D 59 pop ecx
182 0000009E 5F pop edi
183 0000009F 5D pop ebp
184 000000A0 C3 ret
185
186
187 ;sets num words at buffer to the value of c
188 ;void *memsetw(void *buffer, int c, int num);
189 [global _memsetw]
190 _memsetw:
191 000000A1 55 push ebp
192 000000A2 89E5 mov ebp, esp
193 000000A4 57 push edi
194 000000A5 51 push ecx
195 000000A6 8B7D08 mov edi, [ebp+8]
196 000000A9 57 push edi ;save for return address
197 000000AA 8B450C mov eax, [ebp+12]
198 000000AD 8B4D10 mov ecx, [ebp+16]
199
200 000000B0 F366AB rep stosw
201
202 000000B3 58 pop eax
203 000000B4 59 pop ecx
204 000000B5 5F pop edi
205 000000B6 5D pop ebp
206 000000B7 C3 ret
207
208
209 ;sets num dwords at buffer to the value of c
210 ;void *memsetd(void *buffer, int c, int num);
211 [global _memsetd]
212 _memsetd:
213 000000B8 55 push ebp
214 000000B9 89E5 mov ebp, esp
215 000000BB 57 push edi
216 000000BC 51 push ecx
217 000000BD 8B7D08 mov edi, [ebp+8]
218 000000C0 57 push edi ;save for return address
219 000000C1 8B450C mov eax, [ebp+12]
220 000000C4 8B4D10 mov ecx, [ebp+16]
221
222 000000C7 F3AB rep stosd
223
224 000000C9 58 pop eax
225 000000CA 59 pop ecx
226 000000CB 5F pop edi
227 000000CC 5D pop ebp
228 000000CD C3 ret
229
230
231 ;returns the number of characters in a string
232 ;extern dword strlen(char *str);
233 [global _strlen]
234 _strlen:
235 000000CE 55 push ebp
236 000000CF 89E5 mov ebp, esp
237 000000D1 56 push esi
238 000000D2 53 push ebx
239 000000D3 8B7508 mov esi, [ebp+8]
240 000000D6 31DB xor ebx, ebx
241 strlenloop:
242 000000D8 AC lodsb
243 000000D9 08C0 or al, al
244 000000DB 7403 jz strlendone
245 000000DD 43 inc ebx
246 000000DE EBF8 jmp strlenloop
247 strlendone:
248 000000E0 89D8 mov eax, ebx
249 000000E2 5B pop ebx
250 000000E3 5E pop esi
251 000000E4 5D pop ebp
252 000000E5 C3 ret
253
254 ;this function invalidates the page directory/table entry that
255 ; would be used to access the memory address given in the parameter
256 ;extern void invlpg_(dword addr);
257 [global _invlpg_]
258 _invlpg_:
259 000000E6 8B442404 mov eax, [esp+4]
260 000000EA 0F0138 invlpg [eax]
261 000000ED C3 ret
262
263
264 ;
265 ;void writeCursorPosition(word pos)
266 ;
267 [global _writeCursorPosition]
268 _writeCursorPosition:
269 000000EE 55 push ebp
270 000000EF 89E5 mov ebp, esp
271
272 000000F1 50 push eax
273 000000F2 53 push ebx
274 000000F3 52 push edx
275
276 000000F4 8B4508 mov eax, [ebp+8] ;cursor position in ax
277
278 000000F7 88C3 mov bl, al
279 000000F9 66BAD403 mov dx, 0x03D4
280 000000FD B00E mov al, 0x0E
281 000000FF EE out dx, al
282
283 00000100 6642 inc dx
284 00000102 88E0 mov al, ah
285 00000104 EE out dx, al
286
287 00000105 664A dec dx
288 00000107 B00F mov al, 0x0F
289 00000109 EE out dx, al
290
291 0000010A 6642 inc dx
292 0000010C 88D8 mov al, bl
293 0000010E EE out dx, al
294
295 0000010F 5A pop edx
296 00000110 5B pop ebx
297 00000111 58 pop eax
298 00000112 5D pop ebp
299
300 00000113 C3 ret
301
302
303 ;
304 ;word getCursorPosition()
305 ;
306 [global _getCursorPosition]
307 _getCursorPosition:
308 00000114 53 push ebx
309 00000115 52 push edx
310
311 00000116 31C0 xor eax, eax
312 00000118 66BAD403 mov dx, 0x03D4
313 0000011C B00E mov al, 0x0E
314 0000011E EE out dx, al
315
316 0000011F 6642 inc dx
317 00000121 EC in al, dx
318 00000122 88C3 mov bl, al
319
320 00000124 664A dec dx
321 00000126 B00F mov al, 0x0F
322 00000128 EE out dx, al
323
324 00000129 6642 inc dx
325 0000012B EC in al, dx
326 0000012C 88DC mov ah, bl
327
328 0000012E 5A pop edx
329 0000012F 5B pop ebx
330
331 00000130 C3 ret
332
333
334 ;
335 ;int puts(char *str)
336 ;
337 [global _puts]
338 _puts:
339 00000131 55 push ebp
340 00000132 89E5 mov ebp, esp
341 00000134 56 push esi
342 00000135 50 push eax
343 00000136 8B7508 mov esi, [ebp+8] ;esi = to string
344 puts_loop:
345 00000139 AC lodsb
346 0000013A 3C00 cmp al, 0
347 0000013C 740E jz puts_done
348 0000013E 50 push eax
349 0000013F E8(00000000) call _putc
350 00000144 81C404000000 add esp, 4
351 0000014A EBED jmp puts_loop
352
353 puts_done:
354 0000014C 58 pop eax
355 0000014D 5E pop esi
356 0000014E 5D pop ebp
357 0000014F C3 ret
358
359
360
361
362 [global _putDecu]
363 _putDecu:
364 00000150 55 push ebp
365 00000151 89E5 mov ebp, esp
366 00000153 81EC18000000 sub esp, 24
367 00000159 C745FC01000000 mov DWORD [ebp-4], 1
368 00000160 C645FB00 mov BYTE [ebp-5], 0
369 L2:
370 00000164 8B5508 mov edx, DWORD [ebp+8]
371 00000167 B8CDCCCCCC mov eax, -858993459
372 0000016C F7E2 mul edx
373 0000016E 89D0 mov eax, edx
374 00000170 C1E803 shr eax, 3
375 00000173 3B45FC cmp eax, DWORD [ebp-4]
376 00000176 7305 jae L4
377 00000178 E912000000 jmp L3
378 L4:
379 0000017D 8B45FC mov eax, DWORD [ebp-4]
380 00000180 89C2 mov edx, eax
381 00000182 C1E202 sal edx, 2
382 00000185 01C2 add edx, eax
383 00000187 8D0412 lea eax, [edx+edx]
384 0000018A 8945FC mov DWORD [ebp-4], eax
385 0000018D EBD5 jmp L2
386 L3:
387 0000018F 90 nop
388 L5:
389 00000190 817DFC01000000 cmp DWORD [ebp-4], 1
390 00000197 7705 ja L7
391 00000199 E959000000 jmp L6
392 L7:
393 0000019E 8B5508 mov edx, DWORD [ebp+8]
394 000001A1 89D0 mov eax, edx
395 000001A3 BA00000000 mov edx, 0
396 000001A8 F775FC div DWORD [ebp-4]
397 000001AB 8945F4 mov DWORD [ebp-12], eax
398 000001AE 8A45F4 mov al, BYTE [ebp-12]
399 000001B1 8845FB mov BYTE [ebp-5], al
400 000001B4 B800000000 mov eax, 0
401 000001B9 8A45FB mov al, BYTE [ebp-5]
402 000001BC 0FAF45FC imul eax, DWORD [ebp-4]
403 000001C0 294508 sub DWORD [ebp+8], eax
404 000001C3 8B55FC mov edx, DWORD [ebp-4]
405 000001C6 B8CDCCCCCC mov eax, -858993459
406 000001CB F7E2 mul edx
407 000001CD 89D0 mov eax, edx
408 000001CF C1E803 shr eax, 3
409 000001D2 8945FC mov DWORD [ebp-4], eax
410 000001D5 8D45FB lea eax, [ebp-5]
411 000001D8 800030 add BYTE [eax], 48
412 000001DB 81EC0C000000 sub esp, 12
413 000001E1 B800000000 mov eax, 0
414 000001E6 8A45FB mov al, BYTE [ebp-5]
415 000001E9 50 push eax
416 000001EA E8(00000000) call _putc
417 000001EF 81C410000000 add esp, 16
418 000001F5 EB99 jmp L5
419 L6:
420 000001F7 81EC0C000000 sub esp, 12
421 000001FD 8A4508 mov al, BYTE [ebp+8]
422 00000200 0530000000 add eax, 48
423 00000205 25FF000000 and eax, 255
424 0000020A 50 push eax
425 0000020B E8(00000000) call _putc
426 00000210 81C410000000 add esp, 16
427 00000216 C9 leave
428 00000217 C3 ret
429
430
431
432
433 [global _putDec]
434 _putDec:
435 00000218 55 push ebp
436 00000219 89E5 mov ebp, esp
437 0000021B 81EC18000000 sub esp, 24
438 00000221 817D0800000000 cmp DWORD [ebp+8], 0
439 00000228 7919 jns L9
440 0000022A 81EC0C000000 sub esp, 12
441 00000230 682D000000 push 45
442 00000235 E8(00000000) call _putc
443 0000023A 81C410000000 add esp, 16
444 00000240 F75D08 neg DWORD [ebp+8]
445 L9:
446 00000243 C745FC01000000 mov DWORD [ebp-4], 1
447 0000024A C645FB00 mov BYTE [ebp-5], 0
448 L10:
449 0000024E 8B4508 mov eax, DWORD [ebp+8]
450 00000251 3B45FC cmp eax, DWORD [ebp-4]
451 00000254 7305 jae L12
452 00000256 E912000000 jmp L11
453 L12:
454 0000025B 8B45FC mov eax, DWORD [ebp-4]
455 0000025E 89C2 mov edx, eax
456 00000260 C1E202 sal edx, 2
457 00000263 01C2 add edx, eax
458 00000265 8D0412 lea eax, [edx+edx]
459 00000268 8945FC mov DWORD [ebp-4], eax
460 0000026B EBE1 jmp L10
461 L11:
462 0000026D 8B55FC mov edx, DWORD [ebp-4]
463 00000270 B8CDCCCCCC mov eax, -858993459
464 00000275 F7E2 mul edx
465 00000277 89D0 mov eax, edx
466 00000279 C1E803 shr eax, 3
467 0000027C 8945FC mov DWORD [ebp-4], eax
468 L13:
469 0000027F 817DFC01000000 cmp DWORD [ebp-4], 1
470 00000286 7705 ja L15
471 00000288 E959000000 jmp L14
472 L15:
473 0000028D 8B5508 mov edx, DWORD [ebp+8]
474 00000290 89D0 mov eax, edx
475 00000292 BA00000000 mov edx, 0
476 00000297 F775FC div DWORD [ebp-4]
477 0000029A 8945F4 mov DWORD [ebp-12], eax
478 0000029D 8A45F4 mov al, BYTE [ebp-12]
479 000002A0 8845FB mov BYTE [ebp-5], al
480 000002A3 B800000000 mov eax, 0
481 000002A8 8A45FB mov al, BYTE [ebp-5]
482 000002AB 0FAF45FC imul eax, DWORD [ebp-4]
483 000002AF 294508 sub DWORD [ebp+8], eax
484 000002B2 8B55FC mov edx, DWORD [ebp-4]
485 000002B5 B8CDCCCCCC mov eax, -858993459
486 000002BA F7E2 mul edx
487 000002BC 89D0 mov eax, edx
488 000002BE C1E803 shr eax, 3
489 000002C1 8945FC mov DWORD [ebp-4], eax
490 000002C4 8D45FB lea eax, [ebp-5]
491 000002C7 800030 add BYTE [eax], 48
492 000002CA 81EC0C000000 sub esp, 12
493 000002D0 B800000000 mov eax, 0
494 000002D5 8A45FB mov al, BYTE [ebp-5]
495 000002D8 50 push eax
496 000002D9 E8(00000000) call _putc
497 000002DE 81C410000000 add esp, 16
498 000002E4 EB99 jmp L13
499 L14:
500 000002E6 81EC0C000000 sub esp, 12
501 000002EC 8A4508 mov al, BYTE [ebp+8]
502 000002EF 0530000000 add eax, 48
503 000002F4 25FF000000 and eax, 255
504 000002F9 50 push eax
505 000002FA E8(00000000) call _putc
506 000002FF 81C410000000 add esp, 16
507 00000305 C9 leave
508 00000306 C3 ret
509
510