From: Duje Mihanović <duje.mihanovic@skole.hr>
Date: Sat, 21 Aug 2021 15:06:36 +0000 (+0200)
Subject: Allow controlling (no) newline when printing and listen to keystrokes
X-Git-Tag: 0.1.1~13^2~1
X-Git-Url: http://git.dujemihanovic.xyz/img/%22http:/www.sics.se/static/git-favicon.png?a=commitdiff_plain;h=eec791adfbaddde2f5724e0f5c7319e7d2aaef0e;p=nameless-os.git

Allow controlling (no) newline when printing and listen to keystrokes
---

diff --git a/boot.s b/boot.s
index eb115d0..51be5d9 100644
--- a/boot.s
+++ b/boot.s
@@ -1,21 +1,69 @@
 	bits 16 ; boot sectors run in real mode
 	org 7c00h ; boot sector is loaded at 0x7c00
 
+_start
 	mov di, printing_numbers
 	call print
 
+	or bl, 1
 	mov dx, 0DEADh ; number to print
 	call print_word
+	
+	mov di, space
+	call print
+	
+	xor bl, bl
 	mov dx, 0BEEFh
 	call print_word
 
-	mov di, goodbye
+	mov di, waiting
 	call print
 
-	jmp $
+	mov di, keystroke
+	mov bl, 1
+
+.loop
+	mov ah, 0
+	int 16h
+	cmp ah, 1Ch
+	je .enterpressed
+	jmp .continue
+	cmp ah, 0Eh
+	je .backspace
+	jmp .continue
+.enterpressed
+	pusha
+	mov ah, 0Eh
+	mov al, 0Dh
+	int 10h
+	mov al, 0Ah
+	int 10h
+	popa
+	jmp .loop
+.backspace
+	pusha
+	mov ah, 03h
+	mov bh, 0
+	int 10h
+	dec dl
+	mov ah, 02h
+	int 10h
+	mov ah, 0Eh
+	mov al, ' '
+	int 10h
+	mov ah, 02h
+	int 10h
+	popa
+	jmp .loop
+.continue
+	mov [keystroke], al
+	call print
+	jmp .loop
 
-printing_numbers: db "Printing some random hex numbers", 0
-goodbye: db "Thank you for your attention", 0
+printing_numbers db "Printing some random hex numbers", 0
+waiting db "I await keystrokes", 0
+keystroke db "$", 0
+space db " ", 0
 
 %include "print.s"
 
diff --git a/print.s b/print.s
index e1d9bc6..3a46c72 100644
--- a/print.s
+++ b/print.s
@@ -3,28 +3,33 @@
 ; Prints a null terminated string of your choice.
 ; Arguments:
 ;     CS:DI - pointer to the string you want to print
-print:
+;     BL - 0 prints newline, otherwise no
+print
 	pusha
 	mov ah, 0Eh ; teletype print
-.write2:
+.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
+	je .newline ; 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:
+.newline
+	cmp bl, 1
+	je .done
 	mov al, 0Dh ; carriage return
 	int 10h
 	mov al, 0Ah ; line feed
 	int 10h
+.done
 	popa
 	ret ; return
 
 ; Prints a word (16-bit value) in hex format.
 ; Arguments:
 ;     DX - word to print
-print_word:
+;     BL - 0 will print a newline, 1 won't
+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
@@ -49,83 +54,83 @@ print_word:
 	popa
 	ret
 
-.compare:
+.compare
 	cmp al, 0 ; compare al with 0
 	jne .one ; if not equal, compare with 1
 	mov byte [CURRENT_ADDR], '0' ; set character to ASCII 0
 	ret ; return
-.one:
+.one
 	cmp al, 1
 	jne .two
 	mov byte [CURRENT_ADDR], '1'
 	ret
-.two:
+.two
 	cmp al, 2
 	jne .three
 	mov byte [CURRENT_ADDR], '2'
 	ret
-.three:
+.three
 	cmp al, 3
 	jne .four
 	mov byte [CURRENT_ADDR], '3'
 	ret
-.four:
+.four
 	cmp al, 4
 	jne .five
 	mov byte [CURRENT_ADDR], '4'
 	ret
-.five:
+.five
 	cmp al, 5
 	jne .six
 	mov byte [CURRENT_ADDR], '5'
 	ret
-.six:
+.six
 	cmp al, 6
 	jne .seven
 	mov byte [CURRENT_ADDR], '6'
 	ret
-.seven:
+.seven
 	cmp al, 7
 	jne .eight
 	mov byte [CURRENT_ADDR], '7'
 	ret
-.eight:
+.eight
 	cmp al, 8
 	jne .nine
 	mov byte [CURRENT_ADDR], '8'
 	ret
-.nine:
+.nine
 	cmp al, 9
 	jne .ten
 	mov byte [CURRENT_ADDR], '9'
 	ret
-.ten:
+.ten
 	cmp al, 0Ah
 	jne .eleven
 	mov byte [CURRENT_ADDR], 'A'
 	ret
-.eleven:
+.eleven
 	cmp al, 0Bh
 	jne .twelve
 	mov byte [CURRENT_ADDR], 'B'
 	ret
-.twelve:
+.twelve
 	cmp al, 0Ch
 	jne .thirteen
 	mov byte [CURRENT_ADDR], 'C'
 	ret
-.thirteen:
+.thirteen
 	cmp al, 0Dh
 	jne .fourteen
 	mov byte [CURRENT_ADDR], 'D'
 	ret
-.fourteen:
+.fourteen
 	cmp al, 0Eh
 	jne .fifteen
 	mov byte [CURRENT_ADDR], 'E'
 	ret
-.fifteen:
+.fifteen
 	mov byte [CURRENT_ADDR], 'F'
 	ret
 
-HEX_OUT: db "0x0000", 0
+HEX_OUT db "0x0000", 0