2

I've tried reading the documentation but the functionality of this instruction is still cloudy to me. For an example, I would like to know what is stored in RDX after these instructions:

mov    edx, 0x26d1
mov    eax, 0x40d
add    eax, edx
movsxd rdx, eax

Personally, I think it is 0x0000000000002ade because I don't think the signed bit at bit position 31 was present in eax at the time. (If that makes any sense?) Any help would be appreciated and maybe an explanation that doesn't leave me confused would be awesome as well :) Thanks and have a good day!

2

movsxd moves the dword by sign extending the dword into qword

so for this example rdx will be eax+edx

C:\>python -c "print( hex(0x26d1+0x40d))
0x2ade

you can use some emulators like unicorn
or use a debugger and patch this instructions some place and loop
or compile a small source as below to get an understanding
(the code below sign extends a 16bit input to a 32bit output )
in your example a 32bit input is taken to output a 64bit
there is also an 8bit input and 16 bit output
movsxb(8in160ut),movsxw(16in320ut),movsxd (32in640ut)

#include <stdio.h>
int main(void)
{
    printf("movsxd demo\n");
    signed short edx = 0x26d1;
    signed short eax = 0x40d;
    for (int i = 0; i < 25; i++ ){
        edx = edx + eax;
        printf("%x\n",edx);
    }
}

compiled and executed you can see how and when it gets sign extended

:\>cl /Zi /W4 /analyze /EHsc /nologo /Od movsxd.cpp /link /release
movsxd.cpp

:\>movsxd.exe
movsxd demo
2ade
2eeb
32f8
3705
3b12
3f1f
432c
4739
4b46
4f53
5360
576d
5b7a
5f87
6394
67a1
6bae
6fbb
73c8
77d5
7be2
7fef
ffff83fc <<<<<<<<<<
ffff8809 <<<<<<<<<
ffff8c16 <<<<<<<<<<<<<

just to show a simplified disassembly i refactored the code to eliminate superfluous print's, assignments , etc and compiled it with full optimizations and disassembled
code

#include <stdio.h>
int main(void)
{
    signed short edx = 0x26d1;
    for (int i = 0; i < 25; i++ ){
        edx = edx + 0x40d;
        printf("%x\n",edx);
    }
}

disassembly see how word from si (16 bit of ESI)is sign extended to eax(32 bit)

:\>cdb -c "uf movsxd!main;q" movsxd.exe |awk "/Reading/,/quit/"
0:000> cdb: Reading initial command 'uf movsxd!main;q'
movsxd!main:
01291000 56              push    esi
01291001 57              push    edi
01291002 bed1260000      mov     esi,26D1h
01291007 bf19000000      mov     edi,19h
0129100c 0f1f4000        nop     dword ptr [eax]

movsxd!main+0x10:
01291010 81c60d040000    add     esi,40Dh

01291016 0fbfc6          movsx   eax,si <<<<<<<<<
when si will be  > 0x7fff (max signed short) eax will
get sign extended.

01291019 50              push    eax
0129101a 6890012d01      push    offset movsxd!__xt_z+0x8 (012d0190)
0129101f e85c000000      call    movsxd!printf (01291080)
01291024 83c408          add     esp,8
01291027 83ef01          sub     edi,1
0129102a 75e4            jne     movsxd!main+0x10 (01291010)

movsxd!main+0x2c:
0129102c 5f              pop     edi
0129102d 33c0            xor     eax,eax
0129102f 5e              pop     esi
01291030 c3              ret
quit:
| improve this answer | |
  • Thank you very much! This cleared some stuff up for me! – Chase Opsahl Feb 14 at 17:24

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.