Sign in to follow this  

movzx and movsx question again

This topic is 2340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=arial, verdana, tahoma, sans-serif][size=2]here is a disasm of my program below



[code]




void function( short int Var1 )[/size][/font]
{
return;
}
int main()
{
short int Var1 = -3;
int Var2;

Var2 = Var1;

function( Var1 );
return 0;
}[/code]




[code]
_main PROC ; COMDAT

; 7 : {

push ebp
mov ebp, esp
sub esp, 8

; 8 : short int Var1 = -3;

mov eax, -3 ; fffffffdH
mov WORD PTR _Var1$[ebp], ax

; 9 : int Var2;
; 10 :
; 11 : Var2 = Var1;

movsx ecx, WORD PTR _Var1$[ebp]
mov DWORD PTR _Var2$[ebp], ecx

; 12 :
; 13 : function( Var1 );

movzx edx, WORD PTR _Var1$[ebp]
push edx
call ?function@@YAXF@Z ; function
add esp, 4

; 14 : return 0;

xor eax, eax

; 15 : }

mov esp, ebp
pop ebp
ret 0
_main ENDP [/code]


i understand why the first movsx is used to cast the signed integer value into a DWORD size and keep it's sign( negative in this case ). What i don't understand is why when Var1 is passed into the parameter of function() it is moved into a DWORD register using movzx? Doesn't this mean we loose the negative value of the sign? therefore it will treat this as an unsigned integer within the function? Am i interpreting this right or no? if so why does it happen like this?

Share this post


Link to post
Share on other sites
[quote name='nuclear123' ]
i understand why the first movsx is used to cast the signed integer value into a DWORD size and keep it's sign( negative in this case ).
[/quote]

Correct

[quote name='nuclear123' ]
What i don't understand is why when Var1 is passed into the parameter of function() it is moved into a DWORD register using movzx?
[/quote]

This is because your function takes a short instead of an int. Movsx would have converted your value to a DWORD but you only want a WORD.

[quote name='nuclear123' ]
Doesn't this mean we loose the negative value of the sign?
[/quote]

No, you don't loose any information, your 2 bytes aren't changed in any way, they are just padded with 2 bytes (of value zero, 0x00).

[quote name='nuclear123' ]
therefore it will treat this as an unsigned integer within the function? Am i interpreting this right or no?
[/quote]

This is a wrong assumption, you assume your function will treat your value as an int because it gets stored as a dword, however your function is empty and you cannot see how it treats your data. Add something like Var1++; inside the function to see what really happens.
Here is my prediction: your function will treat the data as an unsigned WORD and ignore the two 00 bytes that get added by the movzx function.

[quote name='nuclear123' ]
if so why does it happen like this?
[/quote]

If im correct it has to do with memory allignment, on a 32 bits system accessing the memory works more efficient (in terms of time it takes to access it), by alligning them on 32bits --> 4 bytes. even though you dont use up all the data. Look it up and you'll find a way better explanation than i can ever give.

Hope this clears it up.

I haven't posted on this site for a very very long time, but i started reading again a few weeks ago after regaining interest in game programming and logged back in because i found your question interesting :) sorry if i used unnecesary quotes or anything.

Share this post


Link to post
Share on other sites

This topic is 2340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this