Real mode assembly II: Difference between revisions

Jump to navigation Jump to search
m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (Bot: Replace deprecated source tag with syntaxhighlight)
Line 9:
This is where things get fun. We're going to convert a value in al to two characters and print them out. The following code snippet from http://www.df.lth.se/~john_e/gems/gem003a.html shows how to convert the low nybble of al to an ASCII character.
 
<sourcesyntaxhighlight lang="asm">
cmp al,10
sbb al,69h
das
</syntaxhighlight>
</source>
 
Simple, but doesn't do that much. After all, it only converts the low nybble and can do weird things occasionally. So we need to shift (and rotate for the low nybble). Here's how I do it:
 
<sourcesyntaxhighlight lang="asm">
shr al,4 ; Shift right four places to put upper nybble in lower spot
cmp al,10
Line 25:
mov ah,0Eh ; Print out upper nybble
int 10h
</syntaxhighlight>
</source>
 
Sure, it works, but it's only printing out the upper nybble and the low nybble disappears! Oh noes! So we have to store a copy of al in a variable first:
 
<sourcesyntaxhighlight lang="asm">
mov [.temp],al
</syntaxhighlight>
</source>
 
And restore it after to do the second:
 
<sourcesyntaxhighlight lang="asm">
mov al,[.temp]
</syntaxhighlight>
</source>
 
Now we rotate and shift to do the conversion for the low nybble:
 
<sourcesyntaxhighlight lang="asm">
ror al,4
shr al,4
Line 50:
mov ah,0Eh
int 10h
</syntaxhighlight>
</source>
 
===The code===
Line 56:
Now put that all together:
 
<sourcesyntaxhighlight lang="asm">
print_hex_byte:
mov [.temp],al
Line 80:
.temp db 0
</syntaxhighlight>
</source>
 
And that function is now callable! Using what you've learned, you can also write '''your own''' call to print all of ax and not just al. Try it, it's not that hard!
Line 88:
[[NASM]] has some great macro abilities, including automating tasks such as writing your db statements for you. Simple, multi-line macros go something like this:
 
<sourcesyntaxhighlight lang="asm">
%macro name operands
; code code code
%endmacro
</syntaxhighlight>
</source>
 
You can use %1, %2, etc. to represent the contents of arguments passed to your macro. You can even emulate BASIC statements with macros, like discussed [[printing to the screen without a db|here]].
Line 98:
Even simpler macros are the single-line macros, or %define statements. They create assemble-time statements to be inserted, but they are more like constants than functions.
 
<sourcesyntaxhighlight lang="asm">
%define D_SIGNATURE 0xCAFEF00D
%define D_INFO 0x29A3B83D
Line 105:
push D_INFO
jmp 0x2000:0
</syntaxhighlight>
</source>
 
Now, that's just example code, it could mean anything, but it depends on what's at 0x2000:0, of course. But the principle is the same, %define is used to create constants and the like.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu