0

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
Skyb
  • 11
  • 5
  • That *should* be writing one byte, the low byte of the binary integer you loaded from `total`. (Not converted to ASCII or anything, but the file size should be 1, not 0). But anyway, [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) explains how to convert an integer to an ASCII decimal string. – Peter Cordes Mar 28 '21 at 14:13

1 Answers1

0

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.

vitsoft
  • 5,515
  • 1
  • 18
  • 31