2

I have been trying to figure out exactly what is happening with these instructions and can't make sense of them. I can see that the PSHUFLW instruction acts upon the first 16 bytes of the XMM register but can not figure it out. I have read the decription in the Intel® Architecture Instruction Set Extensions Programming Reference but just can't seem to get it. I am a very visual person so any help would be greatly appreciated. I have run some tests using the code

xxm0 = 00000000000000000000000000003E2D
PSHUFLW xmm0, xmm0, N

and have the following results

 N = 0, output = 00000000000000003E2D3E2D3E2D3E2D
 N = 1, output = 00000000000000003E2D3E2D3E2D0000
 N = 2, output = 00000000000000003E2D3E2D3E2D0000
 N = 3, output = 00000000000000003E2D3E2D3E2D0000
 N = 4, output = 00000000000000003E2D3E2D00003E2D
 N = 5, output = 00000000000000003E2D3E2D00000000
 N = 6, output = 00000000000000003E2D3E2D00000000
 N = 7, output = 00000000000000003E2D3E2D00000000
 N = 8, output = 00000000000000003E2D3E2D00003E2D
 N = 9, output = 00000000000000003E2D3E2D00000000
 N = 10, output = 00000000000000003E2D00003E2D3E2D
 N = 11, output = 00000000000000003E2D00003E2D0000
 N = 12, output = 00000000000000003E2D00003E2D0000
 N = 13, output = 00000000000000003E2D00003E2D0000
 N = 14, output = 00000000000000003E2D000000003E2D
 N = 15, output = 00000000000000003E2D000000000000
 N = 16, output = 00000000000000003E2D000000000000
 N = 17, output = 00000000000000003E2D000000000000
 N = 18, output = 00000000000000003E2D000000003E2D
 N = 19, output = 00000000000000003E2D000000000000
 N = 20, output = 00000000000000003E2D00003E2D3E2D

I would like to know how these instructions work, and maybe a visual guide on future instructions similar to this. I have only been reversing for a couple of weeks so I am very fresh. Thanks for any help you can provide.

  • So I can see a bunch of references, some mention pshuflw but no specific examples. Can you point me in the right direction? – Ando Jan 11 at 11:06
  • 1
    you might spot the pattern if you test using xmm0=0xAAAABBBBCCCCDDDD – Ian Cook Jan 11 at 12:59
2

The visual story of PSHUFLW is as follows:

enter image description here

  • I will use Position as same mean as Order here and starts from Zero (Zero-Indexed).

As you can see it selects words from source based on value of N. The Order/Position of selection will be chosen by 2 bit values of N. for example when N=4,

  1. According to first bite (2-bits) of N (= 00), it will select word at position/order 0 of source and copies it in Position 0 of destination.
  2. According to second bite (2-bits) of N (= 01), it will select word at position/order 1 of source and copies it in 1st Position of destination.
  3. According to third bite (2-bits) of N (= 00), it will select word at position/order 0 of source and copies it in 2nd Position of destination.
  4. According to fourth bite (2-bits) of N (= 00), it will select word at position/order 0 of source and copies it in 3rd Position of destination.

Next example when N=17,

  1. According to first bite (2-bits) of N (= 01), it will select word at position/order 1 of source and copies it in Position 0 of destination.
  2. According to second bite (2-bits) of N (= 00), it will select word at position/order 0 of source and copies it in 1st Position of destination.
  3. According to third bite (2-bits) of N (= 01), it will select word at position/order 1 of source and copies it in 2nd Position of destination.
  4. According to fourth bite (2-bits) of N (= 00), it will select word at position/order 0 of source and copies it in 3rd Position of destination.

PS: your output for N=17 in above question is wrong! PSHUFD is just same except it will select doublewords from source and copies in destination. so low and high quadword in PSHUFD will be used while in PSHUFLW the low quadword used.

  • Thank you so much, it is ridiculously simple now I can see it. – Ando Jan 11 at 22:14
  • The only question I have is, how did it produce the results above? This was calculated in a debugger by changing the values. – Ando Jan 11 at 22:52
  • producing results? I can't understand your question! – arman Jan 11 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.