Printing string to BIOS Video Memory not working

983 Views Asked by At

So, im using Bochs to run my bootloader and https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf Chapter 4.1.

Im trying to print to the BIOS console by writing to the video memory directly but when I run Bochs, I see no printed string. The code is have is actually identical to the code on the PDF. Am missing something? Is there a Bochs setting im forgetting or something the PDF didnt tell me?

Here is the assembly file containing the function

 ;
; A simple collection of string routines for 32-bit protected mode.
;
[bits 32]
VIDEO_MEMORY equ 0xB8000
WHITE_ON_BLACK equ 0x0f         ; Color mode for the text to be written

PrintString:    ; Assume ebx holds memory address of string.
    ; edx will hold start of video memory
    ; Recall that each character written will take up 2 bytes of video memory
    ; So any particular row or column on the screen will have mem location = 0xb80000
    ; + 2 * (80r + c)

    ; The way this code is written, its always writing starting from the start of the
    ; video memory at 0xb8000, the top left of the screen, replacing everything there.

    pusha
    mov edx, VIDEO_MEMORY

    PrintLoop:
        mov al, [ebx]            ; Only ebx can be used to index
        mov ah, WHITE_ON_BLACK

        cmp al, 0
        je ExitRoutine

        mov [edx], ax

        inc ebx
        add edx, 2

        jmp PrintLoop

    ExitRoutine:
        popa
        ret

Here is my actual boot logic.

;
; A simple boot sector program that loops forever.
;

[bits 32]
[org 0x7c00]

mov ebx, welcome_msg
call PrintString

jmp $

%include "string_utils.s"

welcome_msg db 'WELCOME TO BASICOS OMFG!', 0
goodbye_msg db 'Goodbye! Thanks for using my BasicOS!', 0

times 510 -( $ - $$ ) db 0

dw 0xaa55
1

There are 1 best solutions below

5
David Hoelzer On

You're currently in real mode since you're in a bootloader, so you can't write to that as a long mode address. Instead, set DS to 0xb800 and then use ebx as an offset:

mov ax, 0xb800
mov ds, ax
mov bx, 0
mov [bx], 0x412e  ; A with a green background, yellow foreground

Otherwise you're writing to an offset from wherever DS is currently.