3

I'm quite confused with the

movsx(move with sign extension)

I'm trying to convert assembly code to C. but stuck with movsx part.

enter image description here

this is the code I got so far.

#include <stdio.h>
#include <windows.h>

int main(){
    char str[24] = "Aegisone security";//17+1

    char *a;
    a = &str[24]-24;
    char a2 = -*(a+6);


    //str[32] = *(a+6);

    //char str2[4]=a;
    MessageBox(0,"Hello","reversing",0);
    return 0;
}

can you help show me some example to usage of movsx thing in c code? the part I troubled is

MOVSX EDX , BYTE PTR DS:[ECX+6]
MOV DWORD PTR SS: [EBP-20],EDX'

I need some more detail explanation about this part
my C-code above showing little bit different

MOVESX EDX,BYTE PTR DS:[ECX+6]
NEG EDX
MOV BYTE PTR SS:[EBP-20],DL
  • guys, always it is I found answer myself after 30 minutes I asked a question. change char a2 = -*(a+6) to int a2 = *(a+6) – Mike Kim Nov 12 '16 at 15:18
1

The C compiler handles sign extension automatically for signed variables when they're converted into bigger/wider signed variables. However, you defined that variable as a char which the standard does not enforce sign-ness for, so the compiler is not obligated to sign-extend that variable assignment.

But the most important part is that only when you replaced the char with an int you created a variable 4 bytes long that could actually be sign extended.

Last but not least, removing the minus mark obviously removed the NEG EDX instruction.

If you didn't, looks like you still need to change the assignment line (str[32] = *(a+6);) to treat the destination as a dword sized array, because you're interested in assigning the entire dword. Your code will still have MOV BYTE PTR SS:[EBP-20],DL instead of MOV DWORD PTR SS: [EBP-20],EDX unless you have something like (int*)str[8] = *(a+6);

I replaced the 32 with an 8 because we're indexing 4-byte long array instead of a single byte long array.

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.