I am struggling with a problem in assembly, where I have to take the first byte (FF) of the hex code and copy it over the entire value:
0x045893FF input
0xFFFFFFFF output
What I did is:
movl $0x04580393FF, %eax
shl $24, %eax # to get only the last byte 0xFF000000
Now I want to copy this byte into the rest of the register.
You could do it for instance like this:
mov %al, %ah #0x0458FFFF
mov %ax, %bx #0xFFFF
shl $16, %eax #0xFFFF0000
mov %bx, %ax #0xFFFFFFFF
Another way would be:
movzx %al, %eax
imul $0x1010101, %eax
The last one is possibly faster on modern architectures.
I am used to NASM assembly syntax, but this should be fairly simple.
; this is a comment btw
mov eax, 0x045893FF ; mov to, from
mov ah, al
mov bx, ax
shl eax, 16
mov ax, bx
; eax = 0xFFFFFFFF
Another solution using one register (EAX) only:
(reg=dcba)
mov ah, al (reg=dcaa)
ror eax, 8 (reg=adca)
mov ah, al (reg=adaa)
ror eax, 8 (reg=aada)
mov ah, al (reg=aaaa)
This is a bit slower than the above solution, though.
User contributions licensed under CC BY-SA 3.0