Jump to content
• Advertisement

Public Group

# movzx and movsx question again

This topic is 2560 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

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

 _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 

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

##### Share on other sites
Advertisement

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 ).

Correct

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?

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.

Doesn't this mean we loose the negative value of the sign?

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).

therefore it will treat this as an unsigned integer within the function? Am i interpreting this right or no?

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.

if so why does it happen like this?

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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002325
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!