From: Duje Mihanović Date: Sat, 21 Aug 2021 10:56:47 +0000 (+0200) Subject: Add print function with some examples X-Git-Tag: 0.1.0~29 X-Git-Url: http://git.dujemihanovic.xyz/%22/img/sics.gif/%22/static/git-favicon.png?a=commitdiff_plain;h=b85f8e94af0b9720083f5cd48092ead24e6004c3;p=nameless-os.git Add print function with some examples --- diff --git a/boot.s b/boot.s index 76880b0..eb115d0 100644 --- a/boot.s +++ b/boot.s @@ -1,17 +1,22 @@ bits 16 ; boot sectors run in real mode org 7c00h ; boot sector is loaded at 0x7c00 + mov di, printing_numbers + call print + mov dx, 0DEADh ; number to print - mov cl, 0 ; column - mov ch, 0 ; row call print_word mov dx, 0BEEFh - mov cl, 0 - mov ch, 1 call print_word + mov di, goodbye + call print + jmp $ +printing_numbers: db "Printing some random hex numbers", 0 +goodbye: db "Thank you for your attention", 0 + %include "print.s" times 510-($-$$) db 0 ; fill with 0 until 0x1fc is reached diff --git a/print.s b/print.s index ab6eef4..e1d9bc6 100644 --- a/print.s +++ b/print.s @@ -1,12 +1,36 @@ %define CURRENT_ADDR cs:di +; Prints a null terminated string of your choice. +; Arguments: +; CS:DI - pointer to the string you want to print +print: + pusha + mov ah, 0Eh ; teletype print +.write2: + mov al, [CURRENT_ADDR] ; get current char and put it in al + cmp al, 0 ; check if al is null (string terminator) + je .done ; if it is, start a new line + int 10h ; otherwise write the char + inc di ; increment pointer to string + jmp .write2 ; jump back to this section +.done: + mov al, 0Dh ; carriage return + int 10h + mov al, 0Ah ; line feed + int 10h + popa + ret ; return + +; Prints a word (16-bit value) in hex format. +; Arguments: +; DX - word to print print_word: pusha ; push all regs to stack mov di, HEX_OUT+5 ; set destination index to last char in HEX_OUT mov ax, dx ; copy argument to accumulator and al, 00001111b ; extract the low nibble of the low half of the accumulator call .compare ; fill in the string with correct value - dec di ; decrement destination index + dec di ; decrement string pointer mov ax, dx ; copy argument to accumulator and al, 11110000b ; extract high nibble of low half of accumulator shr al, 4 ; shift high nibble to low nibble @@ -20,24 +44,10 @@ print_word: and al, 11110000b shr al, 4 call .compare - jmp .write ; write string - -.write: - popa ; get back our arguments - mov dl, cl ; column - mov dh, ch ; row - pusha ; save registers - mov al, 1 ; update cursor - mov bh, 0 ; page number - mov bl, 00000111b ; colors - mov cx, 6 ; length of string - push cs ; push code segment address - pop es ; pop to extra segment address - mov bp, HEX_OUT ; offset to extra segment of string - mov ah, 13h ; write string - int 10h ; BIOS interrupt + mov di, HEX_OUT + call print ; write string popa - ret ; return + ret .compare: cmp al, 0 ; compare al with 0 @@ -118,4 +128,4 @@ print_word: mov byte [CURRENT_ADDR], 'F' ret -HEX_OUT: db "0x0000" +HEX_OUT: db "0x0000", 0