The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: MichaelW on October 14, 2005, 09:02:35 AM

Title: Hex tables
Post by: MichaelW on October 14, 2005, 09:02:35 AM
Here are two examples of macros that can define hex value lookup tables with far fewer source code characters than would be required to define the tables manually (~200 versus ~600 for hex_table). The hex_string_table defines each byte value as a null-terminated string with a trailing space. It is intended for generating hex dumps, and the macro bypasses the considerable amount of tedium that would be required to create the table manually.

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

    build_hex_table MACRO
      LOCAL def, d1, d2
      hex_table LABEL BYTE
      def equ <db ">
      FORC d1, <0123456789ABCDEF>
        FORC d2, <0123456789ABCDEF>
          def CATSTR def, <d1>, <d2>
        ENDM
        def CATSTR def, <">
        def
        def equ <db ">
      ENDM
    ENDM

    build_hex_string_table MACRO
      LOCAL def, d1, d2
      hex_string_table LABEL BYTE
      def equ <db >
      FORC d1,<0123456789ABCDEF>
        FORC d2,<0123456789ABCDEF>
          IFIDN <d1>,<d2>
            IFIDN <d1>,<F>
              def CATSTR def,<">,<d1>,<d2>,< ",0>
            ELSE
              def CATSTR def,<">,<d1>,<d2>,< ",0,>
            ENDIF
          ELSE
            def CATSTR def,<">,<d1>,<d2>,< ",0,>
          ENDIF
        ENDM
        def
        def equ <db >
      ENDM
    ENDM
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      build_hex_table
      db 0              ; to allow print as string

      build_hex_string_table
      testbuffer db 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    print ADDR hex_table

    print chr$(13,10,13,10)
    mov   esi, OFFSET testbuffer
    mov   ebx, 16
  @@:
    xor   eax, eax
    mov   al, [esi]
    shl   eax, 2
    add   eax, OFFSET hex_string_table
    print eax
    add   esi, 1
    sub   ebx, 1
    jnz   @B

    mov   eax, input(13,10,13,10,"Press enter to exit...")
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


Title: Re: Hex tables
Post by: Ratch on October 14, 2005, 02:35:01 PM
MichaelW,

Quote
Here are two examples of macros that can define hex value lookup tables....
    And yet another one here. Ratch


HEXTABLE MACRO
   LOCAL I,J
   I = '0'
   REPEAT 10
     J = '0'
     REPEAT 10
       BYTE I,J
       J = J+1
     ENDM
     J = 'A'
     REPEAT 6
      BYTE I,J
      J = J+1
     ENDM
     I = I+1
   ENDM
   I = 'A'
   REPEAT 6
     J = '0'
     REPEAT 10
       BYTE I,J
       J = J+1
     ENDM
     J = 'A'
     REPEAT 6
      BYTE I,J
      J = J+1
     ENDM
     I = I+1
   ENDM
ENDM
Title: Re: Hex tables
Post by: Tedd on October 14, 2005, 04:59:28 PM
..and the no-added-sugar version... :bg

    ;al = hex value to 'lookup'
    mov edx,eax
    and eax,0fh     ;al = _X
    cmp al,0Ah
    sbb al,69h
    das             ;al => hexchar
    xchg eax,edx
    shr eax,4
    and eax,0fh     ;al = X_
    cmp al,0Ah
    sbb al,69h
    das             ;al => hexchar

    mov ah,dl       ;ax = hexchars

    or eax,00200000h    ;add space+null