Hey, I'm new to assembly, and I have to do this for a project.
say eax = 13
I do the code:
print xdword$(eax),13,10
and it prints out
"D"
(with out the 0 in front)
I want to know how I would make it print out as:
0D
(with the 0 in front)
Any ideas or suggestions?
Try using wsprintf (http://msdn2.microsoft.com/en-us/library/ms647550.aspx) instead :)
Ehtyar.
One possibility is:
push eax
print "0"
pop eax
print xdword$(eax),13,10
But using wsprintf would be somewhat cleaner.
Here is another option if you are allowed to use a library module.
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
comment * -----------------------------------------------------
Build this template with
"CONSOLE ASSEMBLE AND LINK"
----------------------------------------------------- *
.code
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
call main
inkey
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL pbuf :DWORD ; allocate a DWORD pointer
LOCAL buffer[16]:BYTE ; allocate a 16 byte buffer for display
mov pbuf, ptr$(buffer) ; load buffer address into pointer
mov ecx, "A" ; write the character to a 32 bit register
invoke dw2hex_ex,ecx,pbuf ; convert it to 32 bit hex string.
print right$(pbuf,2),13,10 ; display the right two characters.
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
LATER : :bg
A low level example directly using a table to store the hex values from 0 to 255.
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
comment * -----------------------------------------------------
Build this template with
"CONSOLE ASSEMBLE AND LINK"
----------------------------------------------------- *
.data
align 16
hex_table \
db "000102030405060708090A0B0C0D0E0F"
db "101112131415161718191A1B1C1D1E1F"
db "202122232425262728292A2B2C2D2E2F"
db "303132333435363738393A3B3C3D3E3F"
db "404142434445464748494A4B4C4D4E4F"
db "505152535455565758595A5B5C5D5E5F"
db "606162636465666768696A6B6C6D6E6F"
db "707172737475767778797A7B7C7D7E7F"
db "808182838485868788898A8B8C8D8E8F"
db "909192939495969798999A9B9C9D9E9F"
db "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
db "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
db "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
db "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
db "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
db "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"
.code
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
call main
inkey
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL pbuf :DWORD
LOCAL buffer[16]:BYTE
mov pbuf, ptr$(buffer) ; write buffer address to pointer
mov eax, "Z" ; write a character to EAX
movzx ecx, WORD PTR [hex_table+eax*2] ; get the 2 chars at table address + eax*2 displacement
mov edx, pbuf ; write output buffer address to EDX
mov [edx], ecx ; write address of 2 bytes in table to buffer address
print pbuf,13,10
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
.DATA
szBuffer BYTE 16 DUP(0)
pszBuffer PBYTE szBuffer
.CODE
...
print cat$(pszBuffer,chr$(48),xdword$(eax),chr$(13,10))
...
You could also copy the xdword$ macro into your source file and modify the format string. If you would rather use wsprintf instead of sprintf (msvcrt.dll would not be required) just replace 'invoke crt_sprintf, ADDR buffer, ADDR xdfmt, xdwordvalue' with 'invoke wsprintf, ADDR buffer, ADDR xdfmt, xdwordvalue' in the macro.
Oops, fixed the error. It worked but it was wrong.
Samuel,
If you really want to learn assembly language, at some point you need to learn how to to things without the macros. They are a good way to get started though. And then later after you learn the details, they are really a convenience.
Perhaps Hutch, just for completeness, could you show a test piece which uses XLAT/XLATB? Some may find that useful.