In this article, we will learn how the BIOS organizes RAM internally and how to locate memory addresses of variables in our ASM program.
Before we begin, it is recommended that you read these previous articles:
When the BIOS loads the boot sector, the RAM is arranged as follows.
As we can see, the memory address at which the boot sector begins is 0x7c00. If we define a variable in our code and later use it in some operation, we must find out its location by adding the relative address of the variable to the base address, that is, 0x7c00 + N.
This example continues from the previous one , but this time we print the value of the variable.
mov ah, 0x0e; tty mode
mov bx, the_secret; get relative address of the_secret
add bx, 0x7c00; absolute address = the_secret relative address + boot sector base address
mov al, [bx]; set al value to absolute address
int 0x10
jmp $ ; infinite loop
the_secret:
db "X"
; zero padding and magic bios number
times 510-($-$$) db 0
dw 0xaa55
We generate the image:
We load it into qemu:
SeaBIOS (version rel-1.12.1-0-ga5cab58e9a3f-prebuilt.qemu.org)
iPXE (http://ipxe.org) 00:03.0 C980 PCI2.10 PnP PMM+07F91410+07EF1410 C980
Booting from Hard Disk...
X
We can avoid having to calculate the absolute address if we directly indicate the base address to nasm:
[org 0x7c00] ; tell the assembler that our base address is bootsector address
mov ah, 0x0e; tty mode
mov bx, the_secret;
mov al, [bx]
int 0x10
jmp $ ; infinite loop
the_secret:
db "X"
; zero padding and magic bios number
times 510-($-$$) db 0
dw 0xaa55
We generate the image:
We load it into qemu:
SeaBIOS (version rel-1.12.1-0-ga5cab58e9a3f-prebuilt.qemu.org)
iPXE (http://ipxe.org) 00:03.0 C980 PCI2.10 PnP PMM+07F91410+07EF1410 C980
Booting from Hard Disk...
X