The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Samuel on October 25, 2007, 03:31:08 AM

Title: Printing hex?
Post by: Samuel on October 25, 2007, 03:31:08 AM
Hey, I'm new to assembly, and I have to do this for a project.

say eax = 13
I do the code:
print xdword$(eax),13,10
and it prints out
"D"(with out the 0 in front)

I want to know how I would make it print out as:
0D (with the 0 in front)


Any ideas or suggestions?
Title: Re: Printing hex?
Post by: Ehtyar on October 25, 2007, 12:08:13 PM
Try using wsprintf (http://msdn2.microsoft.com/en-us/library/ms647550.aspx) instead :)

Ehtyar.
Title: Re: Printing hex?
Post by: MichaelW on October 25, 2007, 12:25:54 PM
One possibility is:

push eax
print "0"
pop eax
print xdword$(eax),13,10


But using wsprintf would be somewhat cleaner.
Title: Re: Printing hex?
Post by: hutch-- on October 25, 2007, 10:00:31 PM
Here is another option if you are allowed to use a library module.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL pbuf  :DWORD              ; allocate a DWORD pointer
    LOCAL buffer[16]:BYTE           ; allocate a 16 byte buffer for display

    mov pbuf, ptr$(buffer)          ; load buffer address into pointer

    mov ecx, "A"                    ; write the character to a 32 bit register

    invoke dw2hex_ex,ecx,pbuf       ; convert it to 32 bit hex string.

    print right$(pbuf,2),13,10      ; display the right two characters.

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start


LATER :  :bg

A low level example directly using a table to store the hex values from 0 to 255.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .data
      align 16
        hex_table \
          db "000102030405060708090A0B0C0D0E0F"
          db "101112131415161718191A1B1C1D1E1F"
          db "202122232425262728292A2B2C2D2E2F"
          db "303132333435363738393A3B3C3D3E3F"
          db "404142434445464748494A4B4C4D4E4F"
          db "505152535455565758595A5B5C5D5E5F"
          db "606162636465666768696A6B6C6D6E6F"
          db "707172737475767778797A7B7C7D7E7F"
          db "808182838485868788898A8B8C8D8E8F"
          db "909192939495969798999A9B9C9D9E9F"
          db "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
          db "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
          db "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
          db "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
          db "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
          db "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL pbuf  :DWORD
    LOCAL buffer[16]:BYTE

    mov pbuf, ptr$(buffer)                  ; write buffer address to pointer

    mov eax, "Z"                            ; write a character to EAX

    movzx ecx, WORD PTR [hex_table+eax*2]   ; get the 2 chars at table address + eax*2 displacement
    mov edx, pbuf                           ; write output buffer address to EDX
    mov [edx], ecx                          ; write address of 2 bytes in table to buffer address

    print pbuf,13,10

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Title: Re: Printing hex?
Post by: GregL on October 26, 2007, 12:26:45 AM

.DATA
  szBuffer  BYTE 16 DUP(0)
  pszBuffer PBYTE szBuffer
.CODE
  ...
  print cat$(pszBuffer,chr$(48),xdword$(eax),chr$(13,10))
  ...


You could also copy the xdword$ macro into your source file and modify the format string. If you would rather use wsprintf instead of sprintf (msvcrt.dll would not be required) just replace 'invoke crt_sprintf, ADDR buffer, ADDR xdfmt, xdwordvalue' with 'invoke wsprintf, ADDR buffer, ADDR xdfmt, xdwordvalue' in the macro.

Oops, fixed the error. It worked but it was wrong.
Title: Re: Printing hex?
Post by: GregL on October 26, 2007, 04:25:41 AM
Samuel,

If you really want to learn assembly language, at some point you need to learn how to to things without the macros. They are a good way to get started though. And then later after you learn the details, they are really a convenience.

Title: Re: Printing hex?
Post by: Mark Jones on October 26, 2007, 03:21:47 PM
Perhaps Hutch, just for completeness, could you show a test piece which uses XLAT/XLATB? Some may find that useful.