Can't print characters to screen on a real computer

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
stskyblade
Posts: 12
Joined: Sun Jul 28, 2024 9:56 pm

Can't print characters to screen on a real computer

Post by stskyblade »

I want to print a letter "A" in red color on a real computer by writing to video memory at address 0xb80000, but couldn't make it work.

What I observed:
My program works well with Qemu, and I can see a red "A" on the top of Qemu window. But on real computer, I can only see a blinking white underline symbol.

Steps to produce:

mbr.s

Code: Select all

// This program should be written to MBR
// BIOS will load it at read address mode

.section .text
.global mbr_start
.code16
mbr_start:
  mov $0xb800, %ax
  mov %ax, %ds
  mov $0, %di
  movw $0x0441, (%di) // red "A"

_halt:
  jmp _halt


// add MBR boot sign
// https://stackoverflow.com/questions/47859273/calculating-padding-length-with-gas-att-directives-for-a-boot-sector
.org 510
.word 0xaa55


mbr.ld

Code: Select all

ENTRY(mbr_start)


SECTIONS
{
  .text 0x7c00:
  {
    *(.text)
}
}

Code: Select all

mbr.bin:
	i686-elf-g++ -T mbr.ld -o mbr.elf -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -nostdlib -g -save-temps mbr.S
        i686-elf-objcopy -O binary mbr.elf mbr.bin
# generate a file of 200MB
        dd if=/dev/zero of=./mbr.img bs=512 count=409600
# copy bin to disk img
	dd if=./mbr.bin of=./mbr.img conv=notrunc
	

# it works with qemu
	qemu-system-i386 mbr.img


# not work on real computer
# write disk image to a SATA disk
	sudo dd if=mbr.img of=/dev/sdb bs=512 count=4
# boot that SATA disk on a laptop, ASUS x450vc

stskyblade
Posts: 12
Joined: Sun Jul 28, 2024 9:56 pm

Re: Can't print characters to screen on a real computer

Post by stskyblade »

Sorry, guys. The bug doesn't exist.

This is how it happens:
Connection between my SATA disk and computer is not good. When I use `dd` to write the image to disk, I didn't actually write to block device /dev/sdb, but wrote to a regular file /dev/sdb instead.

So what I observed is not the result of the code posted. It's another bug of old version code. I fixed it by setting DS segment register to 0. I thought my laptop didn't do it, but Qemu do it.
Post Reply