Saturday, March 14, 2020

ASM: Convert int to string in Assembly

This post shows how to convert int (DWORD) to string using MASM library dwtoa. There seems to be a bug in dwtoa as it can only handle 2147483647 instead of 4294967295 which is the maximum size of DWORD (32-bit). The code for dwtoa checks for a negative number which is not needed for DWORD. Note that DWORD is defined as 32-bit unsigned.

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
buffer DB 11 DUP(0)
;value DWORD 4294967295
value DWORD 2147483647
.code
start:
invoke dwtoa, value, addr buffer
invoke StdOut, addr buffer
invoke ExitProcess, 0
end start
view raw int2string.asm hosted with ❤ by GitHub


Pre-requisite:



To assemble:
\masm32\bin\ml /c /Zd /coff int2string.asm

To link:
\masm32\bin\Link /SUBSYSTEM:CONSOLE int2string.obj

2 comments:

DevManiac said...

This code does not even compile in VS2019!

Techno Scavenger said...

I haven't tried this using Visual Studio at all. This compiles if using MASM, see this link http://www.masm32.com/

Configuring TUN/TAP virtual network interface for use with QEMU on Xubuntu 24.04

Configuring TUN/TAP virtual network interface for use with QEMU on Xubuntu 24.04 I am planning to run qemu-system-ppc to play around QEMU ...