Real mode assembly II: Difference between revisions

Jump to navigation Jump to search
m
added source tags
[unchecked revision][unchecked revision]
mNo edit summary
m (added source tags)
Line 7:
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.
 
<source lang="asm">
cmp al,10
sbb al,69h
das
</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:
 
<source lang="asm">
shr al,4 ; Shift right four places to put upper nybble in lower spot
cmp al,10
Line 20 ⟶ 23:
mov ah,0Eh ; Print out upper nybble
int 10h
</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:
 
<source lang="asm">
mov [.temp],al
</source>
 
And restore it after to do the second:
 
<source lang="asm">
mov al,[.temp]
</source>
 
Now we rotate and shift to do the conversion for the low nybble:
 
<source lang="asm">
ror al,4
shr al,4
Line 39 ⟶ 48:
mov ah,0Eh
int 10h
</source>
 
===The code===
Line 44 ⟶ 54:
Now put that all together:
 
<source lang="asm">
print_hex_byte:
mov [.temp],al
Line 67 ⟶ 78:
.temp db 0
</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 74 ⟶ 86:
[[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:
 
<source lang="asm">
%macro name operands
; code code code
%endmacro
</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 82 ⟶ 96:
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.
 
<source lang="asm">
%define D_SIGNATURE 0xCAFEF00D
%define D_INFO 0x29A3B83D
Line 88 ⟶ 103:
push D_INFO
jmp 0x2000:0
</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.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu