Printing to the screen without a db: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
m (Added Text UI category)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(One intermediate revision by one other user not shown)
Line 3: Line 3:
Note: this macro is designed to be used in [[real mode]] only, since it uses [[BIOS]] functions.
Note: this macro is designed to be used in [[real mode]] only, since it uses [[BIOS]] functions.


<source lang="asm">
<syntaxhighlight lang="asm">
print_string: ; Null terminated string in SI.
print_string: ; Null terminated string in SI.
pusha ; Push all registers
pusha ; Push all registers
Line 30: Line 30:
call print_string ; Print it out using the print_string function.
call print_string ; Print it out using the print_string function.
%endmacro
%endmacro
</syntaxhighlight>
</source>


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):
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):


<source lang="asm">
<syntaxhighlight lang="asm">
print 'Printing without a db in NASM!',0Dh,0Ah ; Print out a little message!
print 'Printing without a db in NASM!',0Dh,0Ah ; Print out a little message!
</syntaxhighlight>
</source>


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.
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.

==See Also==
*[[Printing To Screen]]

[[Category:Tutorials]]
[[Category:Tutorials]]
[[Category:Video]]
[[Category:Video]]

Latest revision as of 04:41, 9 June 2024

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.

See Also