Why do assembly need repeated operation of movzx on eax?

307 Views Asked by At

Code - Difference is one method is for signed short int-s and another for unsigned short int.

short signedShortIntSwap(short int* a , short int* b)
{
    short tmp = *a;
    *a = *b;
    *b = tmp;
    return *a;
}

unsigned short unsignedShortIntSwap(unsigned short int* a ,unsigned short int* b)
{
    unsigned short tmp = *a;
    *a = *b;
    *b = tmp;
    return *a;
}

Assembly: - gcc -c -m64 -o func1 func1.c -O2 -fno-tree-vectorize

0000000000000000 <signedShortIntSwap>:
   0:   f3 0f 1e fa             endbr64 
   4:   0f b7 07                movzx  eax,WORD PTR [rdi]
   7:   0f b7 16                movzx  edx,WORD PTR [rsi]
   a:   66 89 17                mov    WORD PTR [rdi],dx
   d:   66 89 06                mov    WORD PTR [rsi],ax
  10:   0f b7 07                movzx  eax,WORD PTR [rdi]
  13:   c3                      ret    
  14:   66 66 2e 0f 1f 84 00    data16 nop WORD PTR cs:[rax+rax*1+0x0]
  1b:   00 00 00 00 
  1f:   90                      nop

0000000000000020 <unsignedShortIntSwap>:
  20:   f3 0f 1e fa             endbr64 
  24:   0f b7 07                movzx  eax,WORD PTR [rdi]
  27:   0f b7 16                movzx  edx,WORD PTR [rsi]
  2a:   66 89 17                mov    WORD PTR [rdi],dx
  2d:   66 89 06                mov    WORD PTR [rsi],ax
  30:   0f b7 07                movzx  eax,WORD PTR [rdi]
  33:   c3                      ret    
  34:   66 66 2e 0f 1f 84 00    data16 nop WORD PTR cs:[rax+rax*1+0x0]
  3b:   00 00 00 00 
  3f:   90                      nop
  1. Why do we have movzx eax,WORD PTR [rdi] repeated for each functions at address 4 && 10 and 24 && 30.
  2. Why is for both signed and unsigned function have identical instruction set. In which case it differs?
0

There are 0 best solutions below