Writing a message using the BIOS
Quick review:
- Boot Sector loaded by BIOS is 512 bytes
- The code in the boot sector of the disk is loaded by the BIOS at 0000: 7c00
- Machine starts in real mode
- Be aware that the CPU is being interrupted unless you issue the CLI Assembly command
Except (but not all) BIOS interrupts Except CT ds to be filled with a real mode segment value. this is why when BIOS interrupts won't work in protected mode. so if you want to use int 10 h/AH = 0eh to print to the screen, then you need to make sure that your seg: Offset
For the characters to print is correct.
In real mode, addresses are calculated as segment * 16 + offset. since offset can be much larger than 16, there are using pairs of segment and offset that point to the same address. for instance, some say that the bootloader is loaded at 0000: 7c00, while
Others say 07c0: 0000. This is in fact the same address: 16*0x0000 + 0x7c00 = 16*0x07c0 + 0x0000 = 0x7c00.
It doesn't matter if you use 0000: 7c00 or 07c0: 0000, but if you use org you need to be aware of what's happening. by default, the start of a raw binary is at offset 0, but if you need it you can change the offset to something different and make it work. for
Instance the following snippet accesses the variable MSG with segment 0x7c0.
ASM example:
; boot.asm mov ax, 0x07c0 mov ds, ax mov si, msgch_loop:lodsb or al, al ; zero=end or str jz hang ; get out mov ah, 0x0E int 0x10 jmp ch_loop hang: jmp hang msg db 'Welcome to Macintosh', 13, 10, 0 times 510-($-$$) db 0 db 0x55 db 0xAA
Here is the org version. This time, MSG is accessed with segment 0. Note that you still need to tell DS what to be as it can hold any value.
[ORG 0x7c00] xor ax, ax ; make it zero mov ds, ax mov si, msgch_loop:lodsb or al, al ; zero=end of string jz hang ; get out mov ah, 0x0E int 0x10 jmp ch_loop hang: jmp hang msg db 'Welcome to Macintosh', 13, 10, 0 times 510-($-$$) db 0 db 0x55 db 0xAA
Procedures
To save on writing space, the typical 'Procedures 'are often separated from the code using call/RET like the following:
[ORG 0x7c00] xor ax, ax ;make it zero mov ds, ax mov si, msg call bios_print hang: jmp hang msg db 'Welcome to Macintosh', 13, 10, 0 bios_print: lodsb or al, al ;zero=end of str jz done ;get out mov ah, 0x0E int 0x10 jmp bios_printdone: ret times 510-($-$$) db 0 db 0x55 db 0xAA
For some inexplicable reason, loading SiThenJumping to the procedure always bugged me. Fortunately for psychos like me NASM's macros let you pretend that you are passing a parameter (macro definitions has to go before it's being called ).
%macro BiosPrint 1 mov si, word %1ch_loop:lodsb or al, al jz done mov ah, 0x0E int 0x10 jmp ch_loopdone:%endmacro [ORG 0x7c00] xor ax, ax mov ds, ax BiosPrint msg hang: jmp hang msg db 'Welcome to Macintosh', 13, 10, 0 times 510-($-$$) db 0 db 0x55 db 0xAA
And in case your code is becoming long and unreadable, you can break it up into different files, then include the files at the beginning of you main code. Like so:
jmp main %include "othercode.inc" main: ; ... rest of code here
Don't forget the JMP main at the start-otherwise some random other procedure will get called.