Printing to the screen without a db
Printing to the screen without a db - OSDev Wiki
NASM gives you the ability to create useful macros that have a calling convention similar to BASIC statements. Macros can be used for many purposes, and here one is used to emulate BASIC's PRINT statement.
Note: this macro is designed to be used in real mode only, since it uses BIOS functions.
print_string: ; Null terminated string in SI.
pusha ; Push all registers
mov ah, 0Eh ; INT 10h teletype.
.loop:
lodsb ; Get byte from string.
cmp al, 0 ; Null terminator reached?
je .done ; Yes, end printing.
int 10h ; No, print the character.
jmp .loop ; Loop!
.done:
popa ; Pop all registers.
ret ; Return.
%macro print 1+
section .data ; At the end of the binary.
%%string:
db %1,0
section .text ; Back to where we were.
mov si,%%string
call print_string ; Print it out using the print_string function.
%endmacro
Now you can use print as if it were a function or instruction in your OS like so (note, you don't want to use this in a bootloader, it messes with the data section and I don't know how to place the boot signature at the end of the data section and still pad out to 512 bytes):
print 'Printing without a db in NASM!',0Dh,0Ah ; Print out a little message!
By the way, a little feature of NASM is the usage of the ` (back quote) character to contain a string that can use C-style escape codes like \n. It's nice and helpful.