assemblymasmirvine32masm32

Down Convert DWORD to BYTE, Assembly 32 bits


I would like to down convert Dword to Byte so that I can store it to a textfile in readable text by call WriteToFile. How can I do it?

 .386     
 .model flat, stdcall  
 INCLUDE Irvine32.inc
 .stack 4096  

 .Data
 total         DWORD  ?      ;total is a variable which holding a math calc output
 totalTXT       BYTE  ?      ;totalTXT would be the output in String value. 
    

Here is where i try to downcast the conversion

 xor eax, eax
 mov eax,  total
 mov totalTXT, al

I need totalTXT in byte so I can store into file as text. But After debugging, my textfile become empty.

WRITE_TO_FILE:                 ;assume file is successfully opened

 mov eax,fileHandle 
 mov edx, OFFSET   totalTXT          
 mov ecx, LENGTHOF totalTXT   
 call WriteToFile
 call CloseFile

Solution

  • DWORD memory variable may contain four ASCII characters, a pointer, a number encoded as signed or unsigned binary number, floating-point number, binary-coded decimal number etc. If you want to store its contents in the form of a human-readable text, you'll need to convert it first.

    Also note that one byte reserved by totalTXT is not enough for numbers above 9. It should be declared as totalTXT BYTE 12 DUP (?) or whatever size is you total converted to.

    As Peter Cordes says in his comment, Irvine32 has WriteDec and WriteInt functions to print a binary integer from EAX, like printf %u or %d.