flat assembler
Message board for the users of flat assembler.

Index > Windows > Rol and to convert num to text.

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1821
Roman 02 Jul 2022, 09:33
I try convert integer number to text. Without div or mul.
But somthing not good.
Code:
                mov eax,4444
                rol eax,4
                mov ebx,eax
                bswap ebx
                and ebx,0xffff;0000
                ;xor ebx,0xffff;0000
                add  bh,'0'
                mov [txt],bh  ;txt db 80 dup (0)
    

I get text "1" but this is incorrect. I expected '4'
Post 02 Jul 2022, 09:33
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 02 Jul 2022, 12:07
You have to add 30h for each number of 1-9 to get ASCII code. But we have hexadecimal system so you definitely need to divide value by 10 to easily get lower number. And do that for each next one.
If you really want to do that without div or mul you have to compare each part and extract it from source. It is more complicated way to do that simple things.
Post 02 Jul 2022, 12:07
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 02 Jul 2022, 12:57
Code:
                mov eax,4444 ; {00000000`00000000`00010001`01011100} = 4444
                rol eax,4 ; {00000000`00000001`00010101`11000000} = 71104
                mov ebx,eax
                bswap ebx ; {11000000`00010101`00000001`00000000} = 3222601984
                and ebx,0xffff;0000  {00000000`00000000`00000001`00000000} = 256
                ;xor ebx,0xffff;0000
                add  bh,'0' ; {00000000`00000000`00110001`00000000} = 12544
                mov [txt],bh  ;txt db 80 dup (0)    
It is unclear what you wanted to achieve with this code


Last edited by macomics on 02 Jul 2022, 12:58; edited 1 time in total
Post 02 Jul 2022, 12:57
View user's profile Send private message Reply with quote
FlierMate1



Joined: 31 May 2022
Posts: 118
FlierMate1 02 Jul 2022, 12:58
I came across 'Binary number to decimal ASCII string conversion' ( https://board.flatassembler.net/topic.php?t=3924 ) the other day when doing utf8count.asm. Too advanced for average coders like me.
Post 02 Jul 2022, 12:58
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1821
Roman 02 Jul 2022, 13:41
I found int to hex text.
But i want int to decimal text numbers
Code:
;convert int to hex text
        mov     rsi,txt     ;in data txt db 16 dup (0)
        mov     eax,100  ;100=64h
        mov     cl,8
@@rnd1:
        rol     eax,4
        mov     ebx,eax
        and     al,00001111b
        add     al,'0'
        cmp     al,'9'
        jbe     @@rnd2
        add     al,07h
@@rnd2: mov     [rsi],al
        inc     rsi
        mov     eax,ebx
        dec     cl
        jnz     @@rnd1 
    
Post 02 Jul 2022, 13:41
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 02 Jul 2022, 13:53
First then you will have to translate int into packed BCD
Post 02 Jul 2022, 13:53
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1821
Roman 02 Jul 2022, 14:19
First i found this Smile
formula: A/10 = a*((1<<67)/10) >> 67
A/10 = a*0x0CCCCCCCCCCCCCCCD >> 67
Code:
;int64 to decimal string number !
 mov   r8, -3689348814741910323  ;magic = 0x0CCCCCCCCCCCCCCCD = ((1<<67)/10)
        mov   rsi,txt+40              ;in data txt db 42 dup (0)
        mov   rdi,4689348814741910323  ;19 digits !
.LBB1_1:                                ; =>This Inner Loop Header: Depth=1
        mov     rax, rdi
        mul     r8                        ; mul to rdx
        shr     rdx, 3                   ;>> 67
        lea     eax, [rdx + rdx]
        lea     eax, [rax + 4*rax]
        mov     ecx, edi
        sub     ecx, eax
        or      cl, '0'
        mov     byte [rsi - 1], cl
        dec     rsi                       ;what faster ? dec rsi or add rsi,-1 ?
        cmp     rdi, 9
        mov     rdi, rdx
        ja      .LBB1_1        
        invoke  MessageBox,0,rsi,0,0   
    
Post 02 Jul 2022, 14:19
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Jul 2022, 04:54
Code:
.data
 result db 10 dup(0x30)
          db 0
 simple    dd 1000000000
              dd 100000000
              dd 10000000
              dd 1000000
              dd 100000
              dd 10000
              dd 1000
              dd 100
              dd 10
 number  dd ?
.code
  xor rdx,rdx
  mov eax,[number]
begin:
  cmp  eax,[simple+rdx*4]
  jb @F
  sub eax,[simple+rdx*4]
  add byte[result+rdx],1
  jmp begin
@@:
  inc rdx
  cmp rdx,9
  jne  begin
  add byte[result+rdx],al
    

You're welcome
Post 03 Jul 2022, 04:54
View user's profile Send private message Visit poster's website Reply with quote
I



Joined: 19 May 2022
Posts: 58
I 03 Jul 2022, 10:38
Umm, some results. Maybe someone should check if they are right?

Roman
Code:
 1 Million Iterations @2GHz, Counter Frequency 10000000

          Number Used            Number Ret          ms   Clocks
================================================================
  9999999999999999999                            48.028       96
  9999999999999999999                            47.998       96
  9999999999999999999                            47.933       96

      999999999999999                            36.403       73
      999999999999999                            36.410       73
      999999999999999                            36.431       73

          99999999999                            25.098       50
          99999999999                            25.115       50
          99999999999                            25.102       50

              9999999                            15.095       30
              9999999                            15.111       30
              9999999                            15.106       30

                  999                             7.001       14
                  999                             7.014       14
                  999                             7.003       14

  5555555555555555555                            47.939       96
  5555555555555555555                            47.954       96
  5555555555555555555                            47.948       96

      555555555555555                            36.411       73
      555555555555555                            36.409       73
      555555555555555                            36.414       73

          55555555555                            25.107       50
          55555555555                            25.109       50
          55555555555                            25.109       50

              5555555                            15.096       30
              5555555                            15.095       30
              5555555                            15.095       30

                  555                             6.064       12
                  555                             6.457       13
                  555                             6.065       12

                    0                             2.251        5
                    0                             2.250        5
                    0                             2.250        5

                    1                             2.251        5
                    1                             2.251        5
                    1                             2.250        5

 18446744073709551615                            50.859      102
 18446744073709551615                            50.856      102
 18446744073709551615                            50.865      102

 17777777777777777777                            50.892      102
 17777777777777777777                            50.879      102
 17777777777777777777                            50.873      102
    


Overclick adjusted to 64-bit number.
Code:
 1 Million Iterations @2GHz, Counter Frequency 10000000

          Number Used            Number Ret          ms   Clocks
================================================================
  9999999999999999999  09999999999999999999     235.319      471
  9999999999999999999  09999999999999999999     236.097      472
  9999999999999999999  09999999999999999999     235.538      471

      999999999999999  00000999999999999999     194.617      389
      999999999999999  00000999999999999999     194.554      389
      999999999999999  00000999999999999999     194.545      389

          99999999999  00000000099999999999     151.655      303
          99999999999  00000000099999999999     151.676      303
          99999999999  00000000099999999999     151.744      303

              9999999  00000000000009999999     118.357      237
              9999999  00000000000009999999     119.566      239
              9999999  00000000000009999999     119.229      238

                  999  00000000000000000999      47.596       95
                  999  00000000000000000999      47.626       95
                  999  00000000000000000999      47.560       95

  5555555555555555555  05555555555555555555     161.903      324
  5555555555555555555  05555555555555555555     161.908      324
  5555555555555555555  05555555555555555555     161.869      324

      555555555555555  00000555555555555555     134.227      268
      555555555555555  00000555555555555555     134.046      268
      555555555555555  00000555555555555555     134.054      268

          55555555555  00000000055555555555     112.019      224
          55555555555  00000000055555555555     112.050      224
          55555555555  00000000055555555555     111.968      224

              5555555  00000000000005555555      83.302      167
              5555555  00000000000005555555      83.572      167
              5555555  00000000000005555555      83.536      167

                  555  00000000000000000555      48.116       96
                  555  00000000000000000555      48.183       96
                  555  00000000000000000555      47.984       96

                    0  00000000000000000000      37.765       76
                    0  00000000000000000000      37.758       76
                    0  00000000000000000000      37.753       76

                    1  00000000000000000001      37.840       76
                    1  00000000000000000001      37.905       76
                    1  00000000000000000001      37.917       76

 18446744073709551615  18446744073709551615     227.419      455
 18446744073709551615  18446744073709551615     228.378      457
 18446744073709551615  18446744073709551615     229.771      460

 17777777777777777777  17777777777777777777     221.867      444
 17777777777777777777  17777777777777777777     221.654      443
 17777777777777777777  17777777777777777777     221.658      443

  5000000000000000000  05000000000000000000      45.061       90
  5000000000000000000  05000000000000000000      45.171       90
  5000000000000000000  05000000000000000000      45.275       91

     5000000000000000  00005000000000000000      42.174       84
     5000000000000000  00005000000000000000      41.875       84
     5000000000000000  00005000000000000000      41.656       83

        5000000000000  00000005000000000000      46.476       93
        5000000000000  00000005000000000000      46.386       93
        5000000000000  00000005000000000000      46.214       92

           5000000000  00000000005000000000      46.548       93
           5000000000  00000000005000000000      46.466       93
           5000000000  00000000005000000000      46.606       93

              5000000  00000000000005000000      41.679       83
              5000000  00000000000005000000      34.602       69
              5000000  00000000000005000000      34.600       69

                 5000  00000000000000005000      34.523       69
                 5000  00000000000000005000      34.519       69
                 5000  00000000000000005000      34.513       69

                   50  00000000000000000050      34.616       69
                   50  00000000000000000050      34.621       69
                   50  00000000000000000050      34.610       69
    


Both unsigned, one with leading zero's, one without.


Last edited by I on 04 Jul 2022, 06:14; edited 1 time in total
Post 03 Jul 2022, 10:38
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 03 Jul 2022, 10:56
So it was clear that Overclick gave a very slow garbage bust. There are up to 200 iterations of a cycle with a 20-digit 64-bit number.
Naturally, he will lose outright to the cycle that performs division and allocates a digit from a number. At the same time, no more than 20 iterations take place.
Post 03 Jul 2022, 10:56
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Jul 2022, 11:15
Quote:

Without div or mul.

right?
Post 03 Jul 2022, 11:15
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 03 Jul 2022, 11:46
Code:
.data
 result    db 20 dup(0x30)
           dd 0 ; align 8
 number    dq ?
 simple    dq 10000000000000000000
           dq 1000000000000000000
           dq 100000000000000000
           dq 10000000000000000
           dq 1000000000000000
           dq 100000000000000
           dq 10000000000000
           dq 1000000000000
           dq 100000000000
           dq 10000000000
           dq 1000000000
           dq 100000000
           dq 10000000
           dq 1000000
           dq 100000
           dq 10000
           dq 1000
           dq 100
           dq 10
.code
  xor rdx,rdx
  mov rax,[number]
begin:
  mov r9, [simple+rdx*8]
  cmp  rax, r9
  jb skip
  shl r9, 3
  mov r8l, 8
digit:
  mov rcx, rax
  sub rcx, r9
  jc @f
  mov rax, rcx
  add byte[result+rdx], r8l
  cmp rax, [simple+rdx*8]
  jc skip
@@:
  shr r9, 1
  shr r8l, 1
  jnz digit
skip:
  inc rdx
  cmp rdx,19
  jl  begin
  add byte[result+rdx],al    
It will still lose, but at least it has no more than 76 iterations, not 180.


Last edited by macomics on 03 Jul 2022, 15:46; edited 1 time in total
Post 03 Jul 2022, 11:46
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 03 Jul 2022, 13:22
For my example, the worst number is 7, not 9. If you check, then check for a number consisting of all 7.
Code:
number dq 17777777777777777777    
Post 03 Jul 2022, 13:22
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 03 Jul 2022, 13:57
But shr reg,1 is also a division. Razz
Post 03 Jul 2022, 13:57
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 03 Jul 2022, 14:49
revolution wrote:
But shr reg,1 is also a division. Razz
Logical bit shift. Otherwise, then let's remove multiplication from sib as well!
Post 03 Jul 2022, 14:49
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Jul 2022, 15:20
Quote:

For my example, the worst number is 7, not 9.

You talk about optimization I just showed concept. Believe me there is a lot of optimizations can be done but it is Roman's headache, not ours. He want it without div mul for some reason, just give it to be.
Also there you missing something with your rcx register at beginning
Post 03 Jul 2022, 15:20
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1004
Location: Russia
macomics 03 Jul 2022, 15:42
Overclick wrote:
Also there you missing something with your rcx register at beginning
Thanks. I saw a typo.


Last edited by macomics on 03 Jul 2022, 15:47; edited 1 time in total
Post 03 Jul 2022, 15:42
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Jul 2022, 15:47
It may jump as rcx is randomized
Code:
cmp  rax, rcx
 jb skip
    
Post 03 Jul 2022, 15:47
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1821
Roman 03 Jul 2022, 19:01
Overclick
Thanks , interesting example.
I modify your example:
Code:
align 8
simple        dq 1000000000
              dq 100000000
              dq 10000000
              dq 1000000
              dq 100000
              dq 10000
              dq 1000
              dq 100
              dq 10
  xor rdx,rdx
  mov eax,9999 ;[number]
beg:
  sub bl,bl
 mov ecx,dword [simple+rdx*8]
begin:
  cmp  eax,ecx
  jb @F
  sub eax,ecx 
  ;add byte[result+rdx],1
  inc bl
  jmp begin
@@:      add [result+rdx],bl
  ;sub bl,bl
  inc rdx
  cmp rdx,9
  jnz  beg
  add byte[result+rdx],al  
    


PS: User I please test this variant. How its faster ?


Last edited by Roman on 03 Jul 2022, 20:55; edited 1 time in total
Post 03 Jul 2022, 19:01
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Jul 2022, 20:53
U can minimize memory access by move dword [simple+rdx*8] to register next to beg:
Post 03 Jul 2022, 20:53
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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.