flat assembler
Message board for the users of flat assembler.

Index > Main > fast print byte in binary string

Author
Thread Post new topic Reply to topic
MCD



Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 09 Jan 2008, 05:55
This is a little code to convert a byte in AL to a binary (ascii) string in [EDI].
who can improve this?
Code:
al_2_bin:
      movd    mm0,eax
     punpcklbw       mm0,mm0
     punpcklwd       mm0,mm0
     punpckldq       mm0,mm0
     pand    mm0,[_0102040810204080]
     pcmpeqb mm0,[_0102040810204080]
     pand    mm0,[_0101010101010101]
     paddb   mm0,[_3030303030303030]
     movq    [edi],mm0
;...
_0102040810204080  dq 0x0102040810204080
_0101010101010101      dq 0x0101010101010101

_3030303030303030  dq 0x3030303030303030
    

_________________
MCD - the inevitable return of the Mad Computer Doggy

-||__/
.|+-~
.|| ||
Post 09 Jan 2008, 05:55
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 09 Jan 2008, 13:15
Code:
b2bin:
        mov ah,al
        mov bx,ax
        shl eax,16
        mov ax,bx
        movd mm0,eax
        punpckldq mm0,mm0
        pand    mm0,[.01] 
        pcmpeqb mm0,[.01] 
        pand    mm0,[.02] 
        paddb   mm0,[.03] 
        movq    [edi],mm0 
;... 
align 8
.01  dq 0x0102040810204080 
.02  dq 0x0101010101010101 
.03  dq 0x3030303030303030 
    


working with little register is faster.
align Qword data on qword boundary improve the speed
giving shorter label makes the compilation faster too.
Post 09 Jan 2008, 13:15
View user's profile Send private message Visit poster's website Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 09 Jan 2008, 16:25
using a lookup table will be faster
but better don't waste time optimizing everything.
>working with little register is faster.
mixing 8-16bit instructions with 32bit instructions is slower.
Post 09 Jan 2008, 16:25
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 09 Jan 2008, 19:43
ok, modification:
Code:
and eax,0ffh
;movzx eax,al
mov ah,al
;or ah,al
mov ebx,eax
shl eax,16
add eax,ebx
;or eax,ebx
...
    


i agree with look up table.
it's the faster way to obtain a byte to string conversion.
and changing the lookup table changes the conversion type.

look up tables are commonly used in embeded DSP applications, to improve treatment speed.
Post 09 Jan 2008, 19:43
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 09 Jan 2008, 21:03
Code:
  shl ax,9
repeat 7
  adc al,$30
  stosb
  salc
  shl ax,1
end repeat
  adc al,$30
  stosb    
smaller, compresses better, doesn't pollute data cache... Laughing

maybe use : pshufw mm0,mm0,0 ?
Post 09 Jan 2008, 21:03
View user's profile Send private message Visit poster's website Reply with quote
Octavio



Joined: 21 Jun 2003
Posts: 366
Location: Spain
Octavio 10 Jan 2008, 09:13
Code:
mov eax,al
imul eax,1010101h
    
Post 10 Jan 2008, 09:13
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 10 Jan 2008, 10:33
Pure 64-bit, 74 bytes, unoptimized, 5.4-5.5 clocks in a repetitive loop.
One-shot is around 132-RDTSC time (which is around 29 clocks+64 latency)
Code:
        mov     eax, (not 101) and 0FFh
        mov     rbx, 0101010101010101h
        mov     r10, 8080808080808080h
        mov     r11, 3030303030303030h
        mov     rcx, 0102040810204080h
        imul    rax,rbx
        and     rax,rcx
        mov     rdx,rax
        not     rax
        sub     rdx,rbx
        and     rax,r10
        and     rax,rdx
        shr     rax,7
        add     rax,r11
    


Btw, isn't there a *magic* value? AL_BIN*MAGIC=AL_ASCII
EDIT: No there isn't Very Happy its not linear, but exponential.
Post 10 Jan 2008, 10:33
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger 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.