commit bd6235251321e8ba8f91121f7497dd778125d962 Author: Robbe Van Herck Date: Fri Dec 27 16:17:28 2019 +0100 Reading and printing strings from hard drives diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97d14fb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +boot.bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a675331 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +compile_and_run: compile run + +compile: + rm -rf boot.bin + nasm -f bin -o boot.bin main.asm + +run: + qemu-system-x86_64 -drive format=raw,file=boot.bin -drive format=raw,file=drive.bin -monitor stdio diff --git a/drive.bin b/drive.bin new file mode 100644 index 0000000..ade7f77 Binary files /dev/null and b/drive.bin differ diff --git a/main.asm b/main.asm new file mode 100644 index 0000000..4986a18 --- /dev/null +++ b/main.asm @@ -0,0 +1,113 @@ +org 0x7C00 +bits 16 + +jmp .start + +;;; Print string at bx +.puts: +.puts_loop: + +mov al, [bx] ; load the character +cmp al, 0 ; check for null byte +je .end_puts_loop + +mov ah, 0x0e +int 0x10 ; print charachter +inc bx +jmp .puts_loop +.end_puts_loop: +ret + + + +;;; Print bl as hex +.puthex: + +; Print high bits +mov dl, bl +shr dl, 4 + +cmp dl, 9 +jg .puthex_high_alpha + +; It's < 10, print as digit +mov al, dl +add al, 0x30 +mov ah, 0x0e +int 0x10 +jmp .puthex_low + +.puthex_high_alpha: +; It's >= 10, print as letter +mov al, dl +add al, 0x57 ; 0x57 = (0x61 - 10) +mov ah, 0x0e +int 0x10 + +.puthex_low: +; Print low bits +mov dl, bl +and dl, 0b1111 + +cmp dl, 9 +jg .puthex_low_alpha + +; It's < 10, print as digit +mov al, dl +add al, 0x30 +mov ah, 0x0e +int 0x10 +jmp .puthex_end + +.puthex_low_alpha: +; It's >= 10, print as letter +mov al, dl +add al, 0x57 ; 0x57 = (0x61 - 10) +mov ah, 0x0e +int 0x10 + +.puthex_end: +ret + + + +.start: + +; reset disk system +mov ah, 0x00 +mov dl, 0x81 +int 0x13 + +; print result on error +jnc .reset_disk_no_error +mov bl, ah +call .puthex +.reset_disk_no_error: + +; Read sector 1 into memory +mov ah, 0x02 +mov al, 0x01 ; number of sectors +mov ch, 0x00 ; cylinder number, low 8 bits +mov cl, 0x01 ; cylinder number, high 2 bits + sector number (6 bits) +mov dh, 0x00 ; head number +mov dl, 0x81 ; drive number +mov bx, .data ; data buffer +int 0x13 + +; print result on error +jnc .read_disk_no_error +mov bl, ah +call .puthex +.read_disk_no_error: + +; print string +mov bx, .data +call .puts + +.end: +hlt + +.data: + +times 510 - ($ - $$) db 0 +dw 0xAA55 \ No newline at end of file