Author Topic: How to convert 00RRGGBBh to 00BBGGRRh without bswap  (Read 18294 times)

mc-black

  • Guest
How to convert 00RRGGBBh to 00BBGGRRh without bswap
« on: June 21, 2012, 04:45:30 AM »
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:
Code: [Select]
.486
.code
shl eax, 8
bswap eax

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

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #1 on: June 21, 2012, 04:59:41 AM »
Code: [Select]
.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

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #2 on: June 21, 2012, 05:10:04 AM »
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

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

maybe slightly faster...
Code: [Select]
        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

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #3 on: June 21, 2012, 05:26:36 AM »
Code: [Select]
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

  • Guest
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #4 on: June 21, 2012, 05:42:00 AM »
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

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #5 on: June 21, 2012, 05:47:15 AM »
ouch - lol
Code: [Select]
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

  • Member
  • *****
  • Posts: 1238
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #6 on: June 21, 2012, 05:52:49 AM »
Hi,

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

Code: [Select]
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--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #7 on: June 21, 2012, 05:53:14 AM »
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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #8 on: June 21, 2012, 06:10:36 AM »
i seem to recall a bit-swap thread in the old forum
something about a "stir fry" method - lol

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #9 on: June 21, 2012, 06:24:15 AM »
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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

mc-black

  • Guest
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #10 on: June 21, 2012, 06:57:02 AM »
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

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #11 on: June 21, 2012, 07:06:27 AM »
Code: [Select]
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

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #12 on: June 21, 2012, 07:42:34 AM »
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--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #13 on: June 21, 2012, 08:12:53 AM »
NOTE that I recommended testing them on antique processors, BSWAP is a better option on anything later.  :biggrin:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Farabi

  • Member
  • ****
  • Posts: 968
  • Neuroscience Fans
Re: How to convert 00RRGGBBh to 00BBGGRRh without bswap
« Reply #14 on: June 21, 2012, 04:18:13 PM »
I think it is still faster and smaller using BSWAP.

Code: [Select]
bswap eax
shr eax,8

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

Contact me at Whatsapp: 6283818314165