News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

How to convert 00RRGGBBh to 00BBGGRRh without bswap

Started by mc-black, June 21, 2012, 04:45:30 AM

Previous topic - Next topic

mc-black

Hi all!

I would like to convert colors represented in eax as 00RRGGBBh format to COLORREF type 00BBGGRRh and thus abandon the directive .486

Now it looks like this:
.486
.code
shl eax, 8
bswap eax


As you can easily do it with the .386 directive?

jj2007

.386
.model flat
.code
start:
; 0RGB
; 0BGR
movzx edx, al ; 000B
shl edx, 2*8 ; 0B00
shr eax, 8 ; eax=00RG
mov dh, al ; 0BG
mov dl, ah ; 0BGR
  ret
end start


Now your problem is to find a CPU that supports such ancient code :biggrin:

dedndave

if you are writing code for a machine that actually has a 386 - ok
otherwise - best to use the extended instruction set
however, i believe they are out there, in the world of embedded controllers, etc

        push    eax
        mov     ah,[esp+2]
        mov     [esp+2],al
        mov     [esp],ah
        pop     eax


maybe slightly faster...
        push    eax
        mov     [esp+2],al
        shr     eax,8
        mov     [esp],ah
        pop     eax


it only uses the source/dest register
could make a macro so it can be done on EAX, EBX, ECX, or EDX

jj2007

Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
2066    cycles for 100*push etc
256     cycles for 100*mov etc

2065    cycles for 100*push etc
261     cycles for 100*mov etc

mc-black

Thank you!

All three solutions are interesting to me to learn assembly language. I understand and agree that the extended instruction set has the advantage here. In my case, there is no restriction on the hardware, but performance is not critical.

The solution from jj2007, like a quick, and two solutions from dedndave not affect the values ​​of other registers.

dedndave

ouch - lol
Intel(R) Pentium(R) 4 CPU 3.00GHz (SSE3)
11766   cycles for 100*push etc
284     cycles for 100*mov etc

11752   cycles for 100*push etc
288     cycles for 100*mov etc

even worse on my P4

FORTRANS

Hi,

   If the DWORD is in memory, or you can put it into
memory, you might try the following.


RGB     DD      00RRGGBB        ; Dummy value (won't assemble).

; That would be the same as

RGB     DB      BB, GG, RR, 0   ; Dummies again.  Little endian storage.

; Then
        MOV     AL,[RGB]
        EXCH    AL,[RBG+2]      ; Use AH  and two MOVes for speed?
        MOV     [RGB],AL


Regards,

Steve N.

hutch--

Try this on an antique processor.



    mov eax, 00998877h
    push eax
    print hex$(eax),13,10
    pop eax

    xchg al, ah
    ror eax, 16
    xchg al, ah
    ror eax, 8

    print hex$(eax),13,10

dedndave

i seem to recall a bit-swap thread in the old forum
something about a "stir fry" method - lol

hutch--

Here are 3 variations.



    xchg al, ah
    ror eax, 16
    xchg al, ah
    ror eax, 8

    ror eax, 24
    xchg al, ah
    ror eax, 16
    xchg al, ah

    rol ax, 8
    rol eax, 16
    rol ax, 8
    rol eax, 24

mc-black

More examples. The last options is attractive, must be the fastest - all in the registers & just one register used. Thank you for your interesting ideas!

jj2007

Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
1321    cycles for 100*push etc Dave
261     cycles for 100*mov etc JJ
1036    cycles for 100*Hutch 1
1042    cycles for 100*Hutch 2
1044    cycles for 100*Hutch 3
730     cycles for 100*Hutch 4

1321    cycles for 100*push etc Dave
263     cycles for 100*mov etc JJ
1034    cycles for 100*Hutch 1
1039    cycles for 100*Hutch 2
1047    cycles for 100*Hutch 3
730     cycles for 100*Hutch 4

dedndave

hang on a second - lol

you're telling me that it only takes ~2.6 cycles to execute 5 instructions,
which includes MOVZX and 2 shifts with a count ?

i smell an Italian fish   :P

hutch--

NOTE that I recommended testing them on antique processors, BSWAP is a better option on anything later.  :biggrin:

Farabi

I think it is still faster and smaller using BSWAP.


bswap eax
shr eax,8


http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165