News:

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

Number formatting in console mode?

Started by JRevor, March 24, 2010, 06:23:38 PM

Previous topic - Next topic

JRevor

Hello there.  I'd like to know if there's any number formatting macro/function. I'm interested in printing every ""digit"" in a DWORD (hexadecimal notation), whether they're zero or not. I'm actually working on a circuit simulation program in console mode, and I want to create result tables like this :



=========================
INPUT | STATE  | NEXT-STATE
=========================
0     |0001AB91|00000001
1     |0001AB91|00020000
0     |0001AB92|0000000A
1     |0001AB92|0002000C
.
.


Thanks a lot for your help.

qWord

FPU in a trice: SmplMath
It's that simple!

JRevor

Thanks a lot!

EDIT : What about writing the string in binary?

dedndave

this code will comvert the dword to a binary string
        INVOKE  dw2bin_ex,dwValue,addr Buffer
you can use print to display it if the string is zero terminated

MichaelW

#4
Edit: Corrected an error and added an additional macro that uses one of the CRT Conversion Functions to handle any base from 2 to 36.

;====================================================================

bin$ MACRO i32
  IFNDEF __bin$_buffer__
    .data
      __bin$_buffer__ db 36 dup(0)
    .code
  ENDIF
  invoke dw2bin_ex, i32, ADDR __bin$_buffer__
  EXITM <OFFSET __bin$_buffer__>
ENDM

;====================================================================

i32to$ MACRO i32, base
  IFNDEF __i32to$_buffer__
    .data
      __i32to$_buffer__ db 36 dup(0)
    .code
  ENDIF
  invoke crt__itoa, i32, ADDR __bin$_buffer__, base
  EXITM <OFFSET __bin$_buffer__>
ENDM

;====================================================================
    include \masm32\include\masm32rt.inc
;====================================================================
    .data
        buffer db 36 dup(0)
    .code
;====================================================================
start:
;====================================================================

    mov eax, 10011111b
    invoke dw2bin_ex, eax, ADDR buffer
    print ADDR buffer,"b",13,10
    mov eax, 10011111b
    print bin$(eax),"b",13,10
    mov eax, 10011111b
    print i32to$(eax, 2),"b",13,10,13,10
    print i32to$(12345678h, 16),"h",13,10
    print i32to$(12345678h, 10),"d",13,10
    print i32to$(12345678h, 8),"o",13,10,13,10

    inkey "Press any key to exit..."
    exit

;====================================================================
end start

eschew obfuscation

GregL

JRevor,

You can also use printf from the C Run-Time Library, if you are familiar with that. It offers a ton of formatting options.

With MASM32 use: INVOKE crt_printf, ...

It requires: INCLUDE msvcrt.inc
               INCLUDELIB msvcrt.lib