repeat / broadcast a byte to every position of an integer register

2

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.

assembly
x86
bit-manipulation
asked on Stack Overflow Mar 20, 2012 by juliensaad • edited Feb 16, 2021 by Peter Cordes

3 Answers

5

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.

answered on Stack Overflow Mar 20, 2012 by Gunther Piez • edited Mar 20, 2012 by Gunther Piez
1

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
answered on Stack Overflow Mar 20, 2012 by Kendall Frey
1

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.

answered on Stack Overflow Mar 4, 2013 by Luis Paris

User contributions licensed under CC BY-SA 3.0