Babystep2: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (Remove the text Macintosh)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(3 intermediate revisions by 3 users not shown)
Line 22:
 
Asm Example:
<sourcesyntaxhighlight lang="asm">
; boot.asm
mov ax, 0x07c0
Line 28:
 
mov si, msg
cld
ch_loop:lodsb
or al, al ; zero=end orof str
jz hang ; get out
mov ah, 0x0E
mov bh, 0
int 0x10
jmp ch_loop
Line 42 ⟶ 44:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
 
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 holdinitialize to any value.
 
<sourcesyntaxhighlight lang="asm">
[ORG 0x7c00]
 
Line 53 ⟶ 55:
 
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
Line 68 ⟶ 72:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
 
=== Procedures ===
 
To save on writing space, the typical 'procedures' are often separated from the code using CALL/RET like the following:
<sourcesyntaxhighlight lang="asm">
[ORG 0x7c00]
xor ax, ax ;make it zero
mov ds, ax
cld
 
mov si, msg
Line 91 ⟶ 96:
jz done ;get out
mov ah, 0x0E
mov bh, 0
int 0x10
jmp bios_print
Line 99 ⟶ 105:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
 
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).
 
<sourcesyntaxhighlight lang="asm">
%macro BiosPrint 1
mov si, word %1
Line 118 ⟶ 124:
xor ax, ax
mov ds, ax
cld
 
BiosPrint msg
Line 129 ⟶ 136:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
 
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:
<sourcesyntaxhighlight lang="asm">
jmp main
 
Line 139 ⟶ 146:
main:
; ... rest of code here
</syntaxhighlight>
</source>
Don't forget the jmp main at the start - otherwise some random other procedure will get called.