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.
?? 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.
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.here is mine:
no need to use full 2 bit regs though, 16 is enough.
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.
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 :)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)
; [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: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
scientica yes you are right, i forgot about the prefix :)
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
bitRAKE: Your first version seems to be 21 bytes, not 19...
Thomas
; 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
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.
bitRAKE: Your first version seems to be 21 bytes, not 19...
I assume eax holds those bits to swap
15 bytes so far
mov edx, eax
shr eax, 6
mov cl, 6
@@: shr edx, 1
rcl eax, 1
dec cl
jnz @B
15 bytes so far
masquer, that is not a solution because the bits within the fields are reversed.
XY123abc --> XYcba321
...the correct conversion is:
XY123abc --> XYabc123
XY123abc --> XYcba321
...the correct conversion is:
XY123abc --> XYabc123
; [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.
Save another byte...
...thanks, Svin. :)
; ---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. :)
What's the code behind the db magic?
aam i think...
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)?
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)?
btw, is "y" the Y bit or does y signify binary (like "b" in 1010001b)?
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...