Babystep2: Difference between revisions
[unchecked revision] | [unchecked revision] |
m (Tutorial:Babystep2 moved to Babystep2: Moving tutorials to main namespace) |
m (Bot: Replace deprecated source tag with syntaxhighlight) |
||
(14 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ |
|||
{{Infobox Tutorial |
|||
| name=Babystep2: Writing a message using the BIOS |
|||
| prev=[[Babystep1]] |
|||
| next=[[Babystep3]] |
|||
}} |
|||
=== Writing a message using the BIOS === |
=== Writing a message using the BIOS === |
||
Line 7: | Line 15: | ||
# Be aware that the CPU is being interrupted unless you issue the [[CLI]] assembly command |
# Be aware that the CPU is being interrupted unless you issue the [[CLI]] assembly command |
||
Many (but not all) BIOS interrupts expect DS to be filled with a Real Mode segment value. This is why many BIOS interrupts won't work in protected mode. So if you want to use int 10h/ah=0eh to print to the screen, then you need to make sure that |
Many (but not all) BIOS interrupts expect DS to be filled with a Real Mode segment value. This is why many BIOS interrupts won't work in protected mode. So if you want to use int 10h/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 many pairs of segment and offset that point to the same address. For instance, some say that the bootloader is 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. |
|||
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: |
Asm Example: |
||
<syntaxhighlight lang="asm"> |
|||
<pre> |
|||
; boot.asm |
; boot.asm |
||
mov ax, 0x07c0 |
mov ax, 0x07c0 |
||
Line 18: | Line 28: | ||
mov si, msg |
mov si, msg |
||
cld |
|||
ch_loop:lodsb |
ch_loop:lodsb |
||
or al, al ; zero=end |
or al, al ; zero=end of str |
||
jz hang ; get out |
jz hang ; get out |
||
mov ah, |
mov ah, 0x0E |
||
mov bh, 0 |
|||
int 0x10 |
int 0x10 |
||
jmp ch_loop |
jmp ch_loop |
||
Line 28: | Line 40: | ||
jmp hang |
jmp hang |
||
msg db ' |
msg db 'Hello World', 13, 10, 0 |
||
times 510-($-$$) db 0 |
times 510-($-$$) db 0 |
||
db 0x55 |
db 0x55 |
||
db 0xAA |
db 0xAA |
||
</syntaxhighlight> |
|||
</pre> |
|||
Here is the ORG version. Note that you still need to tell DS what to be. |
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 initialize to any value. |
||
<syntaxhighlight lang="asm"> |
|||
<pre> |
|||
[ORG 0x7c00] |
[ORG 0x7c00] |
||
Line 43: | Line 55: | ||
mov si, msg |
mov si, msg |
||
cld |
|||
ch_loop:lodsb |
ch_loop:lodsb |
||
or al, al ; zero=end of string |
or al, al ; zero=end of string |
||
jz hang ; get out |
jz hang ; get out |
||
mov ah, 0x0E |
mov ah, 0x0E |
||
mov bh, 0 |
|||
int 0x10 |
int 0x10 |
||
jmp ch_loop |
jmp ch_loop |
||
Line 53: | Line 67: | ||
jmp hang |
jmp hang |
||
msg db ' |
msg db 'Hello World', 13, 10, 0 |
||
times 510-($-$$) db 0 |
times 510-($-$$) db 0 |
||
db 0x55 |
db 0x55 |
||
db 0xAA |
db 0xAA |
||
</syntaxhighlight> |
|||
</pre> |
|||
=== Procedures === |
|||
To save on writing space, the typical 'procedures' are often separated from the code using CALL/RET like the following: |
|||
<syntaxhighlight lang="asm"> |
|||
<pre> |
|||
[ORG 0x7c00] |
[ORG 0x7c00] |
||
xor ax, ax ;make it zero |
xor ax, ax ;make it zero |
||
mov ds, ax |
mov ds, ax |
||
cld |
|||
mov si, msg |
mov si, msg |
||
Line 72: | Line 89: | ||
jmp hang |
jmp hang |
||
msg db ' |
msg db 'Hello World', 13, 10, 0 |
||
bios_print: |
bios_print: |
||
Line 79: | Line 96: | ||
jz done ;get out |
jz done ;get out |
||
mov ah, 0x0E |
mov ah, 0x0E |
||
mov bh, 0 |
|||
int 0x10 |
int 0x10 |
||
jmp bios_print |
jmp bios_print |
||
Line 87: | Line 105: | ||
db 0x55 |
db 0x55 |
||
db 0xAA |
db 0xAA |
||
</syntaxhighlight> |
|||
</pre> |
|||
For some inexplicable reason, loading SI '''then''' jumping 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). |
For some inexplicable reason, loading SI '''then''' jumping 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). |
||
<syntaxhighlight lang="asm"> |
|||
<pre> |
|||
%macro BiosPrint 1 |
%macro BiosPrint 1 |
||
mov si, word %1 |
mov si, word %1 |
||
Line 106: | Line 124: | ||
xor ax, ax |
xor ax, ax |
||
mov ds, ax |
mov ds, ax |
||
cld |
|||
BiosPrint msg |
BiosPrint msg |
||
Line 112: | Line 131: | ||
jmp hang |
jmp hang |
||
msg db ' |
msg db 'Hello World', 13, 10, 0 |
||
times 510-($-$$) db 0 |
times 510-($-$$) db 0 |
||
db 0x55 |
db 0x55 |
||
db 0xAA |
db 0xAA |
||
</syntaxhighlight> |
|||
</pre> |
|||
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: |
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: |
||
<syntaxhighlight lang="asm"> |
|||
<pre> |
|||
jmp main |
jmp main |
||
Line 127: | Line 146: | ||
main: |
main: |
||
; ... rest of code here |
; ... rest of code here |
||
</syntaxhighlight> |
|||
</pre> |
|||
Don't forget the jmp main at the start - otherwise some random other procedure will get called. |
|||
[[Category:Babystep]] |
[[Category:Babystep]] |
Latest revision as of 04:23, 9 June 2024
Babystep2: Writing a message using the BIOS | |
Tutorial | |
Previous | Next |
Babystep1 | Babystep3 |
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
Many (but not all) BIOS interrupts expect DS to be filled with a Real Mode segment value. This is why many BIOS interrupts won't work in protected mode. So if you want to use int 10h/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 many pairs of segment and offset that point to the same address. For instance, some say that the bootloader is 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, msg
cld
ch_loop:lodsb
or al, al ; zero=end of str
jz hang ; get out
mov ah, 0x0E
mov bh, 0
int 0x10
jmp ch_loop
hang:
jmp hang
msg db 'Hello World', 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 initialize to any value.
[ORG 0x7c00]
xor ax, ax ; make it zero
mov ds, ax
mov si, msg
cld
ch_loop:lodsb
or al, al ; zero=end of string
jz hang ; get out
mov ah, 0x0E
mov bh, 0
int 0x10
jmp ch_loop
hang:
jmp hang
msg db 'Hello World', 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
cld
mov si, msg
call bios_print
hang:
jmp hang
msg db 'Hello World', 13, 10, 0
bios_print:
lodsb
or al, al ;zero=end of str
jz done ;get out
mov ah, 0x0E
mov bh, 0
int 0x10
jmp bios_print
done:
ret
times 510-($-$$) db 0
db 0x55
db 0xAA
For some inexplicable reason, loading SI then jumping 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 %1
ch_loop:lodsb
or al, al
jz done
mov ah, 0x0E
int 0x10
jmp ch_loop
done:
%endmacro
[ORG 0x7c00]
xor ax, ax
mov ds, ax
cld
BiosPrint msg
hang:
jmp hang
msg db 'Hello World', 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.