1 2 %macro jzfar 1 3 jnz %%skip 4 jmp %1 5 %%skip: 6 7 %endmacro 8 9 [global _writeCursorPosition] 10 [global _getCursorPosition] 11 [global _putc] 12 [global _puts] 13 [global _printf] 14 [global _console_scroll] 15 [global _console_cls] 16 [global _putHex] 17 [global _putDec] 18 [global _putDecu] 19 20 21 ; 22 ;void writeCursorPosition(word pos) 23 ; 24 _writeCursorPosition: 25 00000000 55 push ebp 26 00000001 89E5 mov ebp, esp 27 28 00000003 50 push eax 29 00000004 53 push ebx 30 00000005 52 push edx 31 32 00000006 8B4508 mov eax, [ebp+8] ;cursor position in ax 33 34 00000009 88C3 mov bl, al 35 0000000B 66BAD403 mov dx, 0x03D4 36 0000000F B00E mov al, 0x0E 37 00000011 EE out dx, al 38 39 00000012 6642 inc dx 40 00000014 88E0 mov al, ah 41 00000016 EE out dx, al 42 43 00000017 664A dec dx 44 00000019 B00F mov al, 0x0F 45 0000001B EE out dx, al 46 47 0000001C 6642 inc dx 48 0000001E 88D8 mov al, bl 49 00000020 EE out dx, al 50 51 00000021 5A pop edx 52 00000022 5B pop ebx 53 00000023 58 pop eax 54 00000024 5D pop ebp 55 56 00000025 C3 ret 57 58 59 ; 60 ;word getCursorPosition() 61 ; 62 _getCursorPosition: 63 00000026 53 push ebx 64 00000027 52 push edx 65 66 00000028 31C0 xor eax, eax 67 0000002A 66BAD403 mov dx, 0x03D4 68 0000002E B00E mov al, 0x0E 69 00000030 EE out dx, al 70 71 00000031 6642 inc dx 72 00000033 EC in al, dx 73 00000034 88C3 mov bl, al 74 75 00000036 664A dec dx 76 00000038 B00F mov al, 0x0F 77 0000003A EE out dx, al 78 79 0000003B 6642 inc dx 80 0000003D EC in al, dx 81 0000003E 88DC mov ah, bl 82 83 00000040 5A pop edx 84 00000041 5B pop ebx 85 86 00000042 C3 ret 87 88 89 ; 90 ;int putc(int chr) 91 ; 92 _putc: 93 00000043 55 push ebp 94 00000044 89E5 mov ebp, esp 95 00000046 53 push ebx 96 00000047 51 push ecx 97 00000048 52 push edx 98 99 00000049 E8D8FFFFFF call _getCursorPosition 100 0000004E 89C3 mov ebx, eax 101 00000050 89D9 mov ecx, ebx 102 103 00000052 8B4508 mov eax, [ebp+8] ;al=character 104 00000055 3C0A cmp al, 10 ;newline 105 00000057 7436 jz putc_newline 106 00000059 3C09 cmp al, 9 ;tab 107 0000005B 746B jz putc_tab 108 109 0000005D D1E3 shl ebx, 1 110 0000005F 81C300800B00 add ebx, 0xb8000 111 00000065 B407 mov ah, 0x07 112 00000067 668903 mov [ebx], ax 113 0000006A 89C8 mov eax, ecx 114 0000006C 40 inc eax 115 0000006D 3DD0070000 cmp eax, 2000 116 00000072 750A jnz putc_writeit2 117 00000074 E86C010000 call _console_scroll 118 00000079 B880070000 mov eax, 2000-80 119 putc_writeit2: 120 0000007E 50 push eax 121 0000007F E87CFFFFFF call _writeCursorPosition 122 00000084 81C404000000 add esp, 4 123 0000008A E96E000000 jmp putc_done 124 125 putc_newline: 126 0000008F 89D8 mov eax, ebx ;eax = cursor position 127 00000091 BB50000000 mov ebx, 80 128 00000096 31D2 xor edx, edx 129 00000098 66F7F3 div bx ;ax=dx:ax/bx, dx=remainder 130 0000009B 66BB5000 mov bx, 80 131 0000009F 6629D3 sub bx, dx 132 000000A2 89C8 mov eax, ecx 133 000000A4 01D8 add eax, ebx ;eax = new cursor position 134 000000A6 3DD0070000 cmp eax, 2000 135 000000AB 750A jnz putc_newline_writeit2 136 000000AD E833010000 call _console_scroll 137 000000B2 B880070000 mov eax, 2000-80 ;beginning of last row 138 putc_newline_writeit2: 139 000000B7 50 push eax 140 000000B8 E843FFFFFF call _writeCursorPosition 141 000000BD 81C404000000 add esp, 4 142 000000C3 E935000000 jmp putc_done 143 144 putc_tab: 145 000000C8 89D8 mov eax, ebx ;eax = cursor position 146 000000CA BB08000000 mov ebx, 8 147 000000CF F6F3 div bl ;al=ax/bl, ah=remainder 148 000000D1 31D2 xor edx, edx 149 000000D3 88E2 mov dl, ah 150 000000D5 66BB0800 mov bx, 8 151 000000D9 6629D3 sub bx, dx 152 000000DC 89C8 mov eax, ecx 153 000000DE 01D8 add eax, ebx ;eax = new cursor position 154 000000E0 3DD0070000 cmp eax, 2000 155 000000E5 750A jnz putc_tab_writeit2 156 000000E7 E8F9000000 call _console_scroll 157 000000EC B880070000 mov eax, 2000-80 ;beginning of last row 158 putc_tab_writeit2: 159 000000F1 50 push eax 160 000000F2 E809FFFFFF call _writeCursorPosition 161 000000F7 81C404000000 add esp, 4 162 163 putc_done: 164 000000FD 5A pop edx 165 000000FE 59 pop ecx 166 000000FF 5B pop ebx 167 00000100 5D pop ebp 168 169 00000101 C3 ret 170 171 172 173 ; 174 ;void printf(char *fmt, ... ) 175 ; 176 _printf: 177 00000102 55 push ebp 178 00000103 89E5 mov ebp, esp 179 00000105 60 pusha 180 00000106 8B5D08 mov ebx, [ebp+8] ;ebx = position in format string 181 00000109 89EE mov esi, ebp 182 0000010B 81C60C000000 add esi, 12 ;esi = to next variable arg 183 00000111 31C9 xor ecx, ecx ;ecx used if we encounter a '%' 184 printf_loop: 185 00000113 8A03 mov al, [ebx] 186 00000115 43 inc ebx 187 00000116 3C00 cmp al, 0 188 jzfar printf_done 189 00000118 7505 <1> jnz %%skip 190 0000011A E9C3000000 <1> jmp %1 191 <1> %%skip: 192 <1> 193 0000011F 81F901000000 cmp ecx, 1 194 00000125 7417 jz printf_special 195 00000127 3C25 cmp al, '%' 196 jzfar printf_percent 197 00000129 7505 <1> jnz %%skip 198 0000012B E9A8000000 <1> jmp %1 199 <1> %%skip: 200 <1> 201 202 00000130 50 push eax 203 00000131 E80DFFFFFF call _putc 204 00000136 81C404000000 add esp, 4 205 0000013C EBD5 jmp printf_loop 206 207 printf_special: 208 0000013E 31C9 xor ecx, ecx 209 00000140 3C64 cmp al, 'd' 210 00000142 7419 jz printf_decimal 211 00000144 3C75 cmp al, 'u' 212 00000146 7428 jz printf_decimalu 213 00000148 3C78 cmp al, 'x' 214 0000014A 7437 jz printf_hex 215 0000014C 3C25 cmp al, '%' 216 0000014E 7446 jz printf_ppercent 217 00000150 3C73 cmp al, 's' 218 00000152 7453 jz printf_string 219 00000154 3C63 cmp al, 'c' 220 00000156 7462 jz printf_char 221 00000158 E970000000 jmp printf_special_done 222 223 printf_decimal: 224 0000015D 8B06 mov eax, [esi] 225 0000015F 50 push eax 226 00000160 E8FE010000 call _putDec 227 00000165 81C404000000 add esp, 4 228 0000016B E95D000000 jmp printf_special_done 229 230 printf_decimalu: 231 00000170 8B06 mov eax, [esi] 232 00000172 50 push eax 233 00000173 E823010000 call _putDecu 234 00000178 81C404000000 add esp, 4 235 0000017E E94A000000 jmp printf_special_done 236 237 printf_hex: 238 00000183 8B06 mov eax, [esi] 239 00000185 50 push eax 240 00000186 E8A2000000 call _putHex 241 0000018B 81C404000000 add esp, 4 242 00000191 E937000000 jmp printf_special_done 243 244 printf_ppercent: 245 00000196 50 push eax 246 00000197 E8A7FEFFFF call _putc 247 0000019C 81C404000000 add esp, 4 248 000001A2 E926000000 jmp printf_special_done 249 250 printf_string: 251 000001A7 8B06 mov eax, [esi] 252 000001A9 50 push eax 253 000001AA E8CD000000 call _puts 254 000001AF 81C404000000 add esp, 4 255 000001B5 E913000000 jmp printf_special_done 256 257 printf_char: 258 000001BA 8B06 mov eax, [esi] 259 000001BC 50 push eax 260 000001BD E881FEFFFF call _putc 261 000001C2 81C404000000 add esp, 4 262 000001C8 E900000000 jmp printf_special_done 263 264 printf_special_done 265 000001CD 81C604000000 add esi, 4 ;point to next extra argument 266 000001D3 E93BFFFFFF jmp printf_loop 267 268 printf_percent: 269 000001D8 B901000000 mov ecx, 1 270 000001DD E931FFFFFF jmp printf_loop 271 272 273 printf_done: 274 000001E2 61 popa 275 000001E3 5D pop ebp 276 000001E4 C3 ret 277 278 279 280 ; 281 ;void console_scroll() 282 ; 283 _console_scroll: 284 000001E5 60 pusha 285 000001E6 BEA0800B00 mov esi, 0xb8000+160 286 000001EB BF00800B00 mov edi, 0xb8000 287 000001F0 B9C0030000 mov ecx, 960 ;(2000-80)/2 288 console_scroll_loop: 289 000001F5 AD lodsd 290 000001F6 AB stosd 291 000001F7 E2FC loop console_scroll_loop 292 000001F9 66B82007 mov ax, 0x0720 293 000001FD B950000000 mov ecx, 80 294 console_scroll_loop2: 295 00000202 66AB stosw 296 00000204 E2FC loop console_scroll_loop2 297 00000206 61 popa 298 00000207 C3 ret 299 300 ; 301 ;void console_cls() 302 ; 303 _console_cls: 304 00000208 60 pusha 305 00000209 BF00800B00 mov edi, 0xb8000 306 0000020E 66B82007 mov ax, 0x0720 307 00000212 B9D0070000 mov ecx, 2000 308 console_cls_loop: 309 00000217 66AB stosw 310 00000219 E2FC loop console_cls_loop 311 0000021B 6800000000 push dword 0 312 00000220 E8DBFDFFFF call _writeCursorPosition 313 00000225 81C404000000 add esp, 4 314 0000022B 61 popa 315 0000022C C3 ret 316 317 ; 318 ;int putHex(dword number) 319 ; 320 _putHex: 321 0000022D 55 push ebp 322 0000022E 89E5 mov ebp, esp 323 00000230 60 pusha 324 00000231 8B4508 mov eax, [ebp+8] ;eax = number to print 325 00000234 31DB xor ebx, ebx ;we have not printed a character yet 326 00000236 B908000000 mov ecx, 8 ;counter for number of characters 327 328 putHex_loop: 329 0000023B 50 push eax 330 0000023C 51 push ecx 331 332 0000023D 49 dec ecx 333 0000023E C1E102 shl ecx, 2 ;edx=counter*4 (amount to shift by) 334 00000241 D3E8 shr eax, cl 335 00000243 250F000000 and eax, 0x0F 336 00000248 80F900 cmp cl, 0 337 0000024B 7409 jz putHex_notzero ;if number is 0 338 0000024D 3C00 cmp al, 0 339 0000024F 7505 jnz putHex_notzero 340 00000251 80FB00 cmp bl, 0 341 00000254 741F jz putHex_loop_end 342 putHex_notzero: 343 00000256 B301 mov bl, 1 344 00000258 0530000000 add eax, '0' 345 0000025D 3D39000000 cmp eax, '9' 346 00000262 7605 jbe putHex_dontadjust 347 00000264 0507000000 add eax, 'A'-'9'-1 348 putHex_dontadjust: 349 00000269 50 push eax 350 0000026A E8D4FDFFFF call _putc 351 0000026F 81C404000000 add esp, 4 352 353 putHex_loop_end: 354 00000275 59 pop ecx 355 00000276 58 pop eax 356 00000277 E2C2 loop putHex_loop 357 358 00000279 61 popa 359 0000027A 5D pop ebp 360 0000027B C3 ret 361 362 363 ; 364 ;int puts(char *str) 365 ; 366 _puts: 367 0000027C 55 push ebp 368 0000027D 89E5 mov ebp, esp 369 0000027F 56 push esi 370 00000280 50 push eax 371 00000281 8B7508 mov esi, [ebp+8] ;esi = to string 372 puts_loop: 373 00000284 AC lodsb 374 00000285 3C00 cmp al, 0 375 00000287 740E jz puts_done 376 00000289 50 push eax 377 0000028A E8B4FDFFFF call _putc 378 0000028F 81C404000000 add esp, 4 379 00000295 EBED jmp puts_loop 380 381 puts_done: 382 00000297 58 pop eax 383 00000298 5E pop esi 384 00000299 5D pop ebp 385 0000029A C3 ret 386 387 388 389 390 391 392 _putDecu: 393 0000029B 55 push ebp 394 0000029C 89E5 mov ebp, esp 395 0000029E 81EC18000000 sub esp, 24 396 000002A4 C745FC01000000 mov DWORD [ebp-4], 1 397 000002AB C645FB00 mov BYTE [ebp-5], 0 398 L2: 399 000002AF 8B5508 mov edx, DWORD [ebp+8] 400 000002B2 B8CDCCCCCC mov eax, -858993459 401 000002B7 F7E2 mul edx 402 000002B9 89D0 mov eax, edx 403 000002BB C1E803 shr eax, 3 404 000002BE 3B45FC cmp eax, DWORD [ebp-4] 405 000002C1 7305 jae L4 406 000002C3 E912000000 jmp L3 407 L4: 408 000002C8 8B45FC mov eax, DWORD [ebp-4] 409 000002CB 89C2 mov edx, eax 410 000002CD C1E202 sal edx, 2 411 000002D0 01C2 add edx, eax 412 000002D2 8D0412 lea eax, [edx+edx] 413 000002D5 8945FC mov DWORD [ebp-4], eax 414 000002D8 EBD5 jmp L2 415 L3: 416 000002DA 90 nop 417 L5: 418 000002DB 817DFC01000000 cmp DWORD [ebp-4], 1 419 000002E2 7705 ja L7 420 000002E4 E959000000 jmp L6 421 L7: 422 000002E9 8B5508 mov edx, DWORD [ebp+8] 423 000002EC 89D0 mov eax, edx 424 000002EE BA00000000 mov edx, 0 425 000002F3 F775FC div DWORD [ebp-4] 426 000002F6 8945F4 mov DWORD [ebp-12], eax 427 000002F9 8A45F4 mov al, BYTE [ebp-12] 428 000002FC 8845FB mov BYTE [ebp-5], al 429 000002FF B800000000 mov eax, 0 430 00000304 8A45FB mov al, BYTE [ebp-5] 431 00000307 0FAF45FC imul eax, DWORD [ebp-4] 432 0000030B 294508 sub DWORD [ebp+8], eax 433 0000030E 8B55FC mov edx, DWORD [ebp-4] 434 00000311 B8CDCCCCCC mov eax, -858993459 435 00000316 F7E2 mul edx 436 00000318 89D0 mov eax, edx 437 0000031A C1E803 shr eax, 3 438 0000031D 8945FC mov DWORD [ebp-4], eax 439 00000320 8D45FB lea eax, [ebp-5] 440 00000323 800030 add BYTE [eax], 48 441 00000326 81EC0C000000 sub esp, 12 442 0000032C B800000000 mov eax, 0 443 00000331 8A45FB mov al, BYTE [ebp-5] 444 00000334 50 push eax 445 00000335 E809FDFFFF call _putc 446 0000033A 81C410000000 add esp, 16 447 00000340 EB99 jmp L5 448 L6: 449 00000342 81EC0C000000 sub esp, 12 450 00000348 8A4508 mov al, BYTE [ebp+8] 451 0000034B 0530000000 add eax, 48 452 00000350 25FF000000 and eax, 255 453 00000355 50 push eax 454 00000356 E8E8FCFFFF call _putc 455 0000035B 81C410000000 add esp, 16 456 00000361 C9 leave 457 00000362 C3 ret 458 459 460 461 462 _putDec: 463 00000363 55 push ebp 464 00000364 89E5 mov ebp, esp 465 00000366 81EC18000000 sub esp, 24 466 0000036C 817D0800000000 cmp DWORD [ebp+8], 0 467 00000373 7919 jns L9 468 00000375 81EC0C000000 sub esp, 12 469 0000037B 682D000000 push 45 470 00000380 E8BEFCFFFF call _putc 471 00000385 81C410000000 add esp, 16 472 0000038B F75D08 neg DWORD [ebp+8] 473 L9: 474 0000038E C745FC01000000 mov DWORD [ebp-4], 1 475 00000395 C645FB00 mov BYTE [ebp-5], 0 476 L10: 477 00000399 8B4508 mov eax, DWORD [ebp+8] 478 0000039C 3B45FC cmp eax, DWORD [ebp-4] 479 0000039F 7305 jae L12 480 000003A1 E912000000 jmp L11 481 L12: 482 000003A6 8B45FC mov eax, DWORD [ebp-4] 483 000003A9 89C2 mov edx, eax 484 000003AB C1E202 sal edx, 2 485 000003AE 01C2 add edx, eax 486 000003B0 8D0412 lea eax, [edx+edx] 487 000003B3 8945FC mov DWORD [ebp-4], eax 488 000003B6 EBE1 jmp L10 489 L11: 490 000003B8 8B55FC mov edx, DWORD [ebp-4] 491 000003BB B8CDCCCCCC mov eax, -858993459 492 000003C0 F7E2 mul edx 493 000003C2 89D0 mov eax, edx 494 000003C4 C1E803 shr eax, 3 495 000003C7 8945FC mov DWORD [ebp-4], eax 496 L13: 497 000003CA 817DFC01000000 cmp DWORD [ebp-4], 1 498 000003D1 7705 ja L15 499 000003D3 E959000000 jmp L14 500 L15: 501 000003D8 8B5508 mov edx, DWORD [ebp+8] 502 000003DB 89D0 mov eax, edx 503 000003DD BA00000000 mov edx, 0 504 000003E2 F775FC div DWORD [ebp-4] 505 000003E5 8945F4 mov DWORD [ebp-12], eax 506 000003E8 8A45F4 mov al, BYTE [ebp-12] 507 000003EB 8845FB mov BYTE [ebp-5], al 508 000003EE B800000000 mov eax, 0 509 000003F3 8A45FB mov al, BYTE [ebp-5] 510 000003F6 0FAF45FC imul eax, DWORD [ebp-4] 511 000003FA 294508 sub DWORD [ebp+8], eax 512 000003FD 8B55FC mov edx, DWORD [ebp-4] 513 00000400 B8CDCCCCCC mov eax, -858993459 514 00000405 F7E2 mul edx 515 00000407 89D0 mov eax, edx 516 00000409 C1E803 shr eax, 3 517 0000040C 8945FC mov DWORD [ebp-4], eax 518 0000040F 8D45FB lea eax, [ebp-5] 519 00000412 800030 add BYTE [eax], 48 520 00000415 81EC0C000000 sub esp, 12 521 0000041B B800000000 mov eax, 0 522 00000420 8A45FB mov al, BYTE [ebp-5] 523 00000423 50 push eax 524 00000424 E81AFCFFFF call _putc 525 00000429 81C410000000 add esp, 16 526 0000042F EB99 jmp L13 527 L14: 528 00000431 81EC0C000000 sub esp, 12 529 00000437 8A4508 mov al, BYTE [ebp+8] 530 0000043A 0530000000 add eax, 48 531 0000043F 25FF000000 and eax, 255 532 00000444 50 push eax 533 00000445 E8F9FBFFFF call _putc 534 0000044A 81C410000000 add esp, 16 535 00000450 C9 leave 536 00000451 C3 ret 537 538 539 540