News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Using MessageBox to display DWORD values

Started by 00100b, January 07, 2005, 04:50:49 PM

Previous topic - Next topic

00100b

What I've been trying to figure out, as part of my studies of MASM, is how to display a message box that simply contains the results of an operation.

Using the API to display a message box for strings I've accomplished.  What I can't seem to figure out is how to do something like:

MOV EDX, 01h
SHL EDX, 10
INVOKE MessageBox, 0, [EDX], 0, 0

Now I know that your looking at this and laughing histarically at me  :red and I know that this doesn't work.

I'm trying to use this as a "spot-check/step-through" type tool for me while I'm learning.

Can you provide me with any guideance?

This particular piece is my attempt to translate the MAKELANGID macro which in the WinNT.H file states:
#define MAKELANGID(p, s)       ((((WORD  )(s)) << 10) | (WORD  )(p))

I don't want the answer to that in particular.  I'd like to work it out myself.  Just thought that you might be asking yourself; "Self?!, what in the heck is that Forby trying to do?"


Thank you

Vortex

Hi Forby,

There is a very usefull function provided by masm32.lib, dwtoa converts a DWORD value to an ascii string.
From masmlib.hlp:
Quote
dwtoa

dwtoa proc public uses esi edi dwValue:DWORD, lpBuffer:PTR BYTE

Note that the parameter lpBuffer is an address of DWORD size.

Description
dwtoa convert a DWORD value to an ascii string.

Parameters
   1. dwValue The DWORD value to convert.

   2. lpBuffer The address of the buffer to put the converted DWORD into.

Return Value
There is no return value.

Comments
The buffer for the converted value should be large enough to hold the string including the terminating zero

An example:

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
include \masm32\macros\macros.asm

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib


.data?
buffer db 100 dup(?)

.code
start:

    mov     edx,01h
    shl     edx,10
    invoke  dwtoa,edx,ADDR buffer   ; convert dword to ascii
    fn      MessageBox,0,ADDR buffer,"dwtoa",MB_OK             
    invoke  ExitProcess,0

END start



00100b

Thank you very much Vortex.  Not only for the response, but for your patience :U

Bieb

Quote from: 00100b on January 07, 2005, 04:50:49 PM
01h

Now, I know it's of no importance, but you are aware of the fact that 01h, 01, and 1 are the same values, right?
:P

00100b

Quote from: Bieb on January 07, 2005, 07:43:46 PM
Now, I know it's of no importance, but you are aware of the fact that 01h, 01, and 1 are the same values, right?
:P
Yeap, I'm just a stickler for explicit conventions (which makes me wonder why I didn't use the same convention on the SHL instruction :red).

Since using something like Ah would cause an error (A2070: invalid instruction operands), I adopted the convention that I will always use at least multiples of 2 when using hexidecimal values and pad out with zeros if the left-most digit is an alpha.  Regardless of whether or not the left-most digit is a numeral.

Thanks for looking out for me.  :U

00100b

Here's an even bigger Thank You Vortex.

Thank you for not pointing me to the DW2A procedure (which used the wsprintf API call).  I just spent the last couple of hours getting the DWTOA procedure down in my head and I'm finally starting to catch on. :bg

Here is the path that you helped to place me on (apologies for being long winded).  It did open my eyes ALOT.  Didn't you hear the thunderous POP echo throughout the universe?  Well, that was me pulling my head out of my a.....

Translation, not executable code.

; Step through simulation using 1033 as the DWORD value that is to be
; conveted into a string.

dwValue = 0409h           ; 1033d
lpBuffer = 0200h          ; arbitrary number picked for this
                          ;simulation to represent memory address.

MOV EAX, dwValue          ; EAX = 1033d
MOV EDI, [lpBuffer]       ; EDI = 0200h

OR EAX, EAX               ; EFlags[ZF]=0b; EFlags[SF]=0b
JNZ sign                  ; If EFlags[ZF]=0b, jump to sign

sign:
  JNS pos                 ; If EFlags[SF]=0b, jump to pos

pos:
  MOV ECX, 1999999Ah      ; ECX = 429496730d
  MOV ESI, EDI            ; ESI = 0200h

  .WHILE (EAX > 00h)      ; (EAX = 1033d) > 0d = True
    MOV EBX, EAX          ; EBX = 1033d
    MUL ECX               ; EDX:EAX = 67h:4CCCCE6Ah
                          ; EDX:EAX = 103d:1288490602d
    MOV EAX, EDX          ; EAX = 103d
    LEA EDX, [EDX*4+EDX]  ; EDX = (103d * 4d + 103d) = 515d
    ADD EDX, EDX          ; EDX = (515d + 515d) = 1030d
    SUB EBX, EDX          ; EBX = (1033d - 1030d) = 3d
    ADD BL, '0'           ; BL = (3d + 48d) = 51d
    MOV [EDI], BL         ; EDI[0200h] = 51d
    INC EDI               ; EDI = (0200h + 01h) = 0201h

  .WHILE (EAX > 00h)      ; (EAX = 103d) > 0d = True
    MOV EBX, EAX          ; EBX = 103d
    MUL ECX               ; EDX:EAX = 0Ah:4CCCCCF6h
                          ; EDX:EAX = 10d:1288490230d
    MOV EAX, EDX          ; EAX = 10d
    LEA EDX, [EDX*4+EDX]  ; EDX = (10d * 4d + 10d) = 50d
    ADD EDX, EDX          ; EDX = (50d + 50d) = 100d
    SUB EBX, EDX          ; EBX = (103d - 100d) = 3d
    ADD BL, '0'           ; BL = (3d + 48d) = 51d
    MOV [EDI], BL         ; EDI[0201h] = 51d
    INC EDI               ; EDI = (0201h + 01h) = 0202h

  .WHILE (EAX > 00h)      ; (EAX = 10d) > 0d = True
    MOV EBX, EAX          ; EBX = 10d
    MUL ECX               ; EDX:EAX = 01h:00000004h
                          ; EDX:EAX = 1d:4d
    MOV EAX, EDX          ; EAX = 1d
    LEA EDX, [EDX*4+EDX]  ; EDX = (1d * 4d + 1d) = 5d
    ADD EDX, EDX          ; EDX = (5d + 5d) = 10d
    SUB EBX, EDX          ; EBX = (10d - 10d) = 0d
    ADD BL, '0'           ; BL = (0d + 48d) = 48d
    MOV [EDI], BL         ; EDI[0202h] = 48d
    INC EDI               ; EDI = (0202h + 01h) = 0203h

  .WHILE (EAX > 00h)      ; (EAX = 1d) > 0d = True
    MOV EBX, EAX          ; EBX = 13d
    MUL ECX               ; EDX:EAX = 00h:19999999Ah
                          ; EDX:EAX = 0d:429496730d
    MOV EAX, EDX          ; EAX = 0d
    LEA EDX, [EDX*4+EDX]  ; EDX = (0d * 4d + 0d) = 0d
    ADD EDX, EDX          ; EDX = (0d + 0d) = 0d
    SUB EBX, EDX          ; EBX = (1d - 0d) = 1d
    ADD BL, '0'           ; BL = (1d + 48d) = 49d
    MOV [EDI], BL         ; EDI[0203h] = 49d
    INC EDI               ; EDI = (0203h + 01h) = 0204h

  .WHILE (EAX > 00h)      ; (EAX = 0d) > 0d = False
  .ENDW

  MOV BYTE PTR [EDI], 0   ; EDI[0204h] = 0d

  .WHILE (ESI < EDI)      ; (ESI = 0200h) < (EDI = 0204h) = True
    DEC EDI               ; EDI = 0203h
    MOV AL, [ESI]         ; AL = (ESI[0200h]) = 51d
    MOV AH, [EDI]         ; AH = (EDI[0203h]) = 49d
    MOV [EDI], AL         ; EDI[0203h] = 51d
    MOV [ESI], AH         ; ESI[0200h] = 49d
    INC ESI               ; ESI = (0200h + 01h) = 0201h

  .WHILE (ESI < EDI)      ; (ESI = 0201h) < (EDI = 0203h) = True
    DEC EDI               ; EDI = 0202h
    MOV AL, [ESI]         ; AL = (ESI[0201h]) = 51d
    MOV AH, [EDI]         ; AH = (EDI[0202h]) = 48d
    MOV [EDI], AL         ; EDI[0202h] = 51d
    MOV [ESI], AH         ; ESI[0201h] = 48d
    INC ESI               ; ESI = (0201h + 01h) = 0202h

  .WHILE (ESI < EDI)      ; (ESI = 0202h) < (EDI = 0202h) = False
  .ENW

   RET                    ; lpBuffer = '1033', 0


If you couldn't tell, Forby is one happy camper.