Babystep2: Difference between revisions

From OSDev Wiki
Jump to navigation Jump to search
Line 40: Line 40:
msg db 'Welcome to Macintosh', 13, 10, 0
msg db 'Welcome to Macintosh', 13, 10, 0
times 510-($-$$) db 0
times 510-($-$$) db 0
(追記) db 0xAA (追記ここまで)
db 0x55
db 0x55
(削除) db 0xAA (削除ここまで)
</source>
</source>


Line 66: Line 66:


times 510-($-$$) db 0
times 510-($-$$) db 0
(追記) db 0xAA (追記ここまで)
db 0x55
db 0x55
(削除) db 0xAA (削除ここまで)
</source>
</source>


Line 99: Line 99:


times 510-($-$$) db 0
times 510-($-$$) db 0
(追記) db 0xAA (追記ここまで)
db 0x55
db 0x55
(削除) db 0xAA (削除ここまで)
</source>
</source>


Line 129: Line 129:


times 510-($-$$) db 0
times 510-($-$$) db 0
(追記) db 0xAA (追記ここまで)
db 0x55
db 0x55
(削除) db 0xAA (削除ここまで)
</source>
</source>



Revision as of 21:22, 23 January 2013


Babystep2: Writing a message using the BIOS

Previous Next

Writing a message using the BIOS

Quick review:

  1. Boot sector loaded by BIOS is 512 bytes
  2. The code in the boot sector of the disk is loaded by the BIOS at 0000:7c00
  3. Machine starts in Real Mode
  4. 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
movax,0x07c0
movds,ax
movsi,msg
ch_loop:lodsb
oral,al; zero=end or str
jzhang; get out
movah,0x0E
int0x10
jmpch_loop
hang:
jmphang
msgdb'WelcometoMacintosh',13,10,0
times510-($-$$)db0
db0xAA
db0x55

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.

[ORG0x7c00]
xorax,ax; make it zero
movds,ax
movsi,msg
ch_loop:lodsb
oral,al; zero=end of string
jzhang; get out
movah,0x0E
int0x10
jmpch_loop
hang:
jmphang
msgdb'WelcometoMacintosh',13,10,0
times510-($-$$)db0
db0xAA
db0x55


Procedures

To save on writing space, the typical 'procedures' are often separated from the code using CALL/RET like the following:

[ORG0x7c00]
xorax,ax;make it zero
movds,ax
movsi,msg
callbios_print
hang:
jmphang
msgdb'WelcometoMacintosh',13,10,0
bios_print:
lodsb
oral,al;zero=end of str
jzdone;get out
movah,0x0E
int0x10
jmpbios_print
done:
ret
times510-($-$$)db0
db0xAA
db0x55

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

%macroBiosPrint1
movsi,word%1
ch_loop:lodsb
oral,al
jzdone
movah,0x0E
int0x10
jmpch_loop
done:
%endmacro
[ORG0x7c00]
xorax,ax
movds,ax
BiosPrintmsg
hang:
jmphang
msgdb'WelcometoMacintosh',13,10,0
times510-($-$$)db0
db0xAA
db0x55

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:

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

Retrieved from "https://wiki.osdev.org/index.php?title=Babystep2&oldid=14382"