Task to swap to 3 last bits fields in a byte
?? abc xyz - > ?? xyz abc.
for example byte 11 010 111 to 11 111 010
Optimization for size.
Nothing good came to my head yet,
so I want you to do my home work for me.
Posted on 2003-03-21 19:47:05 by The Svin
	mov	ah, al		; XY123abc XY123abc

shl al, 3+2 ; abc.....
shl ah, 1 ; Y123abc.
rcr al, 1 ; Xabc....
shl ah, 1 ; 123abc..
rcr al, 1 ; YXabc...
shr ah, 3+2 ; .....123
or al, ah ; [b]YX[/b]abc123
...or...
xlatb	; joking :)
I show the error in my code above - it is not a solution.
Posted on 2003-03-21 20:18:18 by bitRAKE
here is mine:



mov eax,11010111b ;D7 11 010 111
and eax,00111111b
mov ecx,eax
mov ebx,eax
and ecx,00111000b
shr cx,3
and ebx,00000111b
shl bx,3
mov eax,11010111b
and eax,11000000b
or eax,ebx
or eax,ecx ; FA 11 111 010


no need to use full 2 bit regs though, 16 is enough.
Posted on 2003-03-22 03:00:58 by wizzra
Fixed - I hope. :)
				; [COLOR=indigo]---AH--- ---AL---[/COLOR]

mov ah, al ; XY123abc XY123abc
shl al, 3 ; 23abc...
shl ah, 1 ; Y123abc.
rcl al, 1 ; 3abc...X
shl ah, 1 ; 123abc..
rcl al, 1 ; abc...XY
shr ah, 3+2 ; .....123
ror al, 2 ; XYabc...
or al, ah ; XYabc123
19 bytes -- really 21 bytes :)
Posted on 2003-03-22 03:47:15 by bitRAKE

no need to use full 2 bit regs though, 16 is enough.

But using 16-bit GPRs are slower in win32... (66h, an extra clock and byte)
Posted on 2003-03-22 04:11:19 by scientica
				; [COLOR=darkred]---AH--- ---AL---[/COLOR]

shl eax, 2 ; ......XY [COLOR=blue]123[/COLOR][COLOR=green]abc[/COLOR]oo
rol al, 3 ; [COLOR=green]abc[/COLOR]oo[COLOR=blue]123[/COLOR]
shl eax, 3 ; ...XY[COLOR=green]abc[/COLOR] oo[COLOR=blue]123[/COLOR]ooo
shl al, 2 ; [COLOR=blue]123[/COLOR]ooooo
shr eax, 5 ; ........ XY[COLOR=green]abc[/COLOR][COLOR=blue]123[/COLOR]
15 bytes :cool:
Posted on 2003-03-22 04:19:16 by bitRAKE
This is 17 bytes.


mov ah,al
shl ah,3
xor ah,al
and ah,56
xor al,ah
shr ah,3
xor al,ah
Posted on 2003-03-22 04:31:27 by Sephiroth3
scientica yes you are right, i forgot about the prefix :)
Posted on 2003-03-22 04:39:06 by wizzra
I was playing with this version but in the mean time bitRAKE and Sephiroth3 already beat me :). Anyway, here it is, it's 20 bytes

                        ; cl           ah           al

mov ah, al ; ?? ??? ??? | ZZ AAA BBB | ZZ AAA BBB
mov cl, 00111000b ; 00 111 000 | ZZ AAA BBB | ZZ AAA BBB
shl ah, 3 ; 00 111 000 | AA BBB 000 | ZZ AAA BBB
and ah, cl ; 00 111 000 | 00 BBB 000 | ZZ AAA BBB
and cl, al ; 00 AAA 000 | 00 BBB 000 | ZZ AAA BBB
and al, 11000000b ; 00 111 000 | ZZ AAA BBB | ZZ 000 000
shr cl, 3 ; 00 000 AAA | 00 BBB 000 | ZZ 000 000
or al, ah ; 00 000 AAA | 00 BBB 000 | ZZ BBB 000
or al, cl ; 00 000 AAA | 00 BBB 000 | ZZ BBB AAA


bitRAKE: Your first version seems to be 21 bytes, not 19...

Thomas
Posted on 2003-03-22 04:51:34 by Thomas
Sephiroth3, nice one - the xor swap crossed my mind, too. :)

bitRAKE: Your first version seems to be 21 bytes, not 19...
Yeah, I was looking at the wrong one in the debugger.
Posted on 2003-03-22 04:53:38 by bitRAKE
I assume eax holds those bits to swap
	mov edx, eax

shr eax, 6
mov cl, 6
@@: shr edx, 1
rcl eax, 1
dec cl
jnz @B


15 bytes so far
Posted on 2003-03-22 05:33:31 by masquer
masquer, that is not a solution because the bits within the fields are reversed.

XY123abc --> XYcba321

...the correct conversion is:

XY123abc --> XYabc123
Posted on 2003-03-22 11:31:07 by bitRAKE

				; [COLOR=darkred]---AH--- ---AL---[/COLOR]

shl eax, 2 ; ......XY [COLOR=blue]123[/COLOR][COLOR=green]abc[/COLOR]oo
rol al, 3 ; [COLOR=green]abc[/COLOR]oo[COLOR=blue]123[/COLOR]
shl eax, 3 ; ...XY[COLOR=green]abc[/COLOR] oo[COLOR=blue]123[/COLOR]ooo
shl al, 2 ; [COLOR=blue]123[/COLOR]ooooo
shr eax, 5 ; ........ XY[COLOR=green]abc[/COLOR][COLOR=blue]123[/COLOR]
15 bytes :cool:

Very well done!
We can make 1 byte shorter:


db 0d4,40h
ror al,3
shl eax,3
shl al,2
shr eax,5

14 bytes.
All credits are yours of course.
Posted on 2003-03-22 13:47:43 by The Svin
Save another byte...
				; ---AH--- ---AL---

db 0D4h, 1000000y ; ......XY ..123abc
ror al, 3 ; abc..123
shl eax, 3 ; ...XYabc ..123...
shr al, 3 ; .....123
db 0D5h, 1000y ; ........ XYabc123
13 bytes
...thanks, Svin. :)
Posted on 2003-03-22 14:29:35 by bitRAKE
fun:)
Posted on 2003-03-24 06:15:36 by Homer
What's the code behind the db magic?
Posted on 2003-03-24 06:18:46 by Delight
aam i think...
Posted on 2003-03-24 07:32:29 by roticv
Opcodes... :)
D4 ib - aam ib (ASCII adjust ax after multiply, page 58 in Intel's IA-32 Dev. Manual Vol 2 - Instruction set ref)
D5 ib - aad ib (ASCII adjust ax before division, page 57 in Intel's IA-32 Dev. Manual Vol 2 - Instruction set ref)

Note for interested: aam, aad is really D4 0A and D5 0A, D4/D5 ib doesn't have any opcode but some debuggers, like OllyDbg, "transates" it to aam/aad imm8.

btw, is "y" the Y bit or does y signify binary (like "b" in 1010001b)?
Posted on 2003-03-24 09:24:28 by scientica

btw, is "y" the Y bit or does y signify binary (like "b" in 1010001b)?
"y" mean binary like "b" -- I just don't like using hex letters in numbers.
Posted on 2003-03-24 16:06:49 by bitRAKE

Task to swap to 3 last bits fields in a byte
?? abc xyz - > ?? xyz abc.
for example byte 11 010 111 to 11 111 010


As I see it, you want to swap reg/opcode with r/m field with Md=11. I make the same thing, when I change the opcode with the same mnemonic, such as

00C1 ADD CL,AL
02C8 ADD CL,AL


In this case have to be Md always 11, so that swapping bit fields is much more simple...
Posted on 2003-08-14 15:28:41 by MazeGen