flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to convert from dec to hex or vice versa?

Author
Thread Post new topic Reply to topic
KIRK



Joined: 05 Dec 2007
Posts: 20
Location: Russia
KIRK 09 Oct 2011, 08:15
I want to do something like this:
Code:
rept 256 n {
   if defined hex(n)
      db 1
   else
      db 0
   end if
}    

or:
Code:
dc 0FFh
macro dc Code
{ int(Code): db Code
}    
Post 09 Oct 2011, 08:15
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 10 Oct 2011, 17:13
Strange that I couldn't find something this simple on the board.
So here goes:
Code:
; Takes eax and turns it into a hex-string in [edi] (16 or 32-bit)
    eax2hex: ;di to di+cx
        push    eax ebx edx
        mov     ebx,16
      .l:
        xor     edx,edx
        div     ebx
        add     edx,"0"
        cmp     edx,"9"
        jbe     @f
        add     edx,"A"-"9"-1
      @@:
        mov     [edi+ecx-1],dl
        sub     ecx,1
        jne     .l
        pop     edx ebx eax
        ret
    

Code:
;Translates rax into hex-string in strbuf
rax2hex:
        push    rax rbx rcx rdx
        mov     ebx,16
        mov     ecx,15
        mov     byte[strbuf+16],0
      .l:
        xor     edx,edx
        div     rbx
        add     edx,"0"
        cmp     edx,"9"
        jbe     @f
        add     edx,"A"-"9"-1
      @@:
        mov     [strbuf+rcx],dl
        sub     ecx,1
        jnc     .l
        pop     rdx rcx rbx rax
        ret
    

Code:
;16/32/64-bit eax into dec-string in strbuf
eax2dec:
        mov     ebx,10
        mov     ecx,8
        mov     byte[strbuf+9],0
      .l:
        xor     edx,edx
        div     ebx
        add     edx,"0"
        mov     [strbuf+ecx],dl
        sub     ecx,1
        jnc     .l
        ret
    


Sorry I don't understand your macros. The first one only generates ones and zeroes.
Post 10 Oct 2011, 17:13
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1657
Location: Toronto, Canada
AsmGuru62 10 Oct 2011, 17:21
Post 10 Oct 2011, 17:21
View user's profile Send private message Send e-mail Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 10 Oct 2011, 17:43
Since this is under macros I'll assume you want similiar to this, which displays strings from value:
Code:
@DISPLAY_SIGNED                 equ 0
@DISPLAY_UNSIGNED               equ 1

@DISPLAY_GROUP4                 equ  4
@DISPLAY_GROUP8                 equ  8
@DISPLAY_GROUP16                equ 16
@DISPLAY_GROUP32                equ 32
@DISPLAY_GROUP64                equ 64
@DISPLAY_GROUPNONE              equ ((1 shl 63) - 1)

@DISPLAY_SCALE10                equ 1000
@DISPLAY_SCALE2                 equ 1024


macro @display_newline
 {
        display CR,LF
 }

macro @display_bin8 val,group
 {
        display 'bin:'

        repeat 8
                display '0'+((val shr (8 - %)) and 0x01)

                if ((% mod group) = 0)
                        display ' '
                end if
        end repeat
 }

macro @display_bin16 val,group
 {
        display 'bin:'

        repeat 16
                display '0'+((val shr (16 - %)) and 0x01)

                if ((% mod group) = 0)
                        display ' '
                end if
        end repeat
 }

macro @display_bin32 val,group
 {
        display 'bin:'

        repeat 32
                display '0'+((val shr (32 - %)) and 0x01)

                if ((% mod group) = 0)
                        display ' '
                end if
        end repeat
}

macro @display_dec8 val,type
 {
        local number


        number = (val and 0xFF)

        if type
                @display_dec64 number,type
        else
                if (number and 0x80)
                        number = (number or 0xFFFFFFFFFFFFFF00)
                end if

                @display_dec64 number,type
        end if
 }

macro @display_dec16 val,type
 {
        local number


        number = (val and 0xFFFF)

        if type
                @display_dec64 number,type
        else
                if (number and 0x8000)
                        number = (number or 0xFFFFFFFFFFFF0000)
                end if

                @display_dec64 number,type
        end if
 }

macro @display_dec32 val,type
 {
        local number


        number = (val and 0xFFFFFFFF)

        if type
                @display_dec64 number,type
        else
                if (number and 0x80000000)
                        number = (number or 0xFFFFFFFF00000000)
                end if

                @display_dec64 number,type
        end if
 }

;
; @display_dec64 adapted from macro by revolution 2011
;
macro @display_dec64 val,type
 {
        local number
        local leading_zero
        local digit
        local divisor


        display 'dec:'

        number = (val and 0xFFFFFFFFFFFFFFFF)

        if type
                if number < 0
                        number = number - 10000000000000000000

                        if number < 0
                                number = number + 10000000000000000000
                                display '9'
                        else
                                display '1'
                        end if
                end if
        end if

        if number = (1 shl 63)
                display '-9223372036854775808'
        else
                if number < 0
                        number = -number
                        display '-'
                end if

                leading_zero = 0
                divisor = 1000000000000000000

                while divisor > 0
                        digit = number / divisor
                        leading_zero = leading_zero + digit

                        if leading_zero | (divisor = 1)
                                display digit+'0'
                                number = number - (digit * divisor)
                        end if

                        divisor = divisor / 10
                end while
        end if
 }

macro @display_dec_scaled val,scale
 {
        __display_scale equ      0 ,\
                                'k',\
                                'M',\
                                'G',\
                                'T',\
                                'P',\
                                'E',\
                                'Z',\
                                'Y'

        local number
        local index
        local suffix


        number = val
        index = 0

        if (scale = @DISPLAY_SCALE10)
                if number < 0
                        number = number - 10000000000000000000

                        if number < 0
                                number = number + 10000000000000000000
                        end if

                        number = (number / 1000) + 10000000000000000
                        index = index + 1
                end if
        else
        end if

        while (number > scale)
                number = number / scale
                index = index + 1
        end while

        @display_dec64 number,@DISPLAY_SIGNED

        virtual at 0
                db __display_scale

                load suffix byte from index
        end virtual

        display suffix
 }

macro @display_hex8 val,group
 {
        local .x


        display 'hex:'

        repeat 2
                .x = (val and (0x0F shl ((2 - %) shl 2))) shr ((2 - %) shl 2) or '0'

                if .x > '9'
                        .x = .x + 'A' - ':'
                end if

                display .x

                if (((% * 4) mod group) = 0)
                        display ' '
                end if
        end repeat
 }


macro @display_hex16 val,group
 {
        local .x


        display 'hex:'

        repeat 4
                .x = (val and (0x0F shl ((4 - %) shl 2))) shr ((4 - %) shl 2) or '0'

                if .x > '9'
                        .x = .x + 'A' - ':'
                end if

                display .x

                if (((% * 4) mod group) = 0)
                        display ' '
                end if
        end repeat
 }

macro @display_hex32 val,group
 {
        local .x


        display 'hex:'

        repeat 8
                .x = (val and (0x0F shl ((8 - %) shl 2))) shr ((8 - %) shl 2) or '0'

                if .x > '9'
                        .x = .x + 'A' - ':'
                end if

                display .x

                if (((% * 4) mod group) = 0)
                        display ' '
                end if
        end repeat
 }

macro @display_hex64 val,group
 {
        local .x


        display 'hex:'

        repeat 16
                .x = (val and (0x0F shl ((16 - %) shl 2))) shr ((16 - %) shl 2) or '0'

                if .x > '9'
                        .x = .x + 'A' - ':'
                end if

                display .x

                if (((% * 4) mod group) = 0)
                        display ' '
                end if
        end repeat
 }

macro @display_timestamp
 {
        local .t
        local .t_s
        local .t_m
        local .t_h


        .t = %t

        .t_s =   .t                               mod 60
        .t_m = ((.t               - .t_s) /   60) mod 60
        .t_h = ((.t - (.t_m * 60) - .t_s) / 3600) mod 24

        display '0'+(.t_h / 10),'0'+(.t_h mod 10)
        display ':'
        display '0'+(.t_m / 10),'0'+(.t_m mod 10)
        display ':'
        display '0'+(.t_s / 10),'0'+(.t_s mod 10)
 }
    
but you can also make FASM write a value back out e.g changing display_hex8:
Code:
macro @convert_hex8 dst,val
 {
        local .x

        repeat 2
                .x = (val and (0x0F shl ((2 - %) shl 2))) shr ((2 - %) shl 2) or '0'

                if .x > '9'
                        .x = .x + 'A' - ':'
                end if

                store byte .x at dst+(%-1)
        end repeat
 }

my_str dq 0

@convert_hex8 my_str,0xFF      
will put the string "FF" into my_str
Post 10 Oct 2011, 17:43
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Nov 2022, 09:29
Madis731 wrote:
Strange that I couldn't find something this simple on the board.
So here goes:
Code:
;Translates rax into hex-string in strbuf
rax2hex:
        push    rax rbx rcx rdx
        mov     ebx,16
        mov     ecx,15
        mov     byte[strbuf+16],0
      .l:
        xor     edx,edx
        div     rbx
        add     edx,"0"
        cmp     edx,"9"
        jbe     @f
        add     edx,"A"-"9"-1
      @@:
        mov     [strbuf+rcx],dl
        sub     ecx,1
        jnc     .l
        pop     rdx rcx rbx rax
        ret
    



And there's one for Unicode, too?
Post 12 Nov 2022, 09:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 12 Nov 2022, 09:49
AE wrote:
And there's one for Unicode, too?
What encoding do you mean by "Unicode"?

Do you want UTF-8 encoding. Or UTF-16 (WTF-16)? UCS2? UTF-32?
Post 12 Nov 2022, 09:49
View user's profile Send private message Visit poster's website Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Nov 2022, 10:03
I mean that with include 'win64w.inc' it will not work Smile
Post 12 Nov 2022, 10:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 12 Nov 2022, 10:07
That would be widechar in MS parlance.

So you need to store two bytes per character.
Code:
;...
        mov     ecx,15*2
        mov     word[strbuf+32],0
;...
        mov     [strbuf+rcx],dx
        sub     ecx,2
;...    
Post 12 Nov 2022, 10:07
View user's profile Send private message Visit poster's website Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Nov 2022, 11:15
revolution wrote:
So you need to store two bytes per character.

Thanks!
Post 12 Nov 2022, 11:15
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.