sna
06-06-2003, 09:04 AM
when converting from assembler into anything else we need to maintain the byte sign handling. the elements that form a string or simple array are usually byte-sized (8 bits). these elements will be treated as either signed or unsigned.
for example, a string with bytes treated as unsigned:
where esi is a base pointer and ecx is the index.
movzx eax, byte ptr [esi+ecx]
the movzx instruction (move with zero-extend) extends an 8-bit value to a 16-bit value, or an 8-bit or 16-bit value to a 32-bit value by padding the high-order with zeros. the result in this case is that al holds the source byte and the rest of eax is cleared.
on the contrary, when the source is treated as signed, the msb (most significant bit) of the source is used to extend the source value.
movsx eax, byte ptr [esi+ecx] ; move with sign-extend
now, had the source byte been signed, the result would have been that al is the source byte unchanged, and the rest of eax's bits are set to 1. had the source byte not been signed, the result would have been the same as if movzx had been used.
we'll look at a couple of actual cases to help clarify this further:
1) movzx eax, byte ptr [esi+ecx]
al will always hold the source byte and the rest of eax will always be cleared.
2) movsx eax, byte ptr [esi+ecx] * *; source byte is <= 127 dec
al will hold the source byte and rest of eax will be cleared.
3) movsx eax, byte ptr [esi+ecx] * *; source byte is > 127 dec
al will hold the source byte and the rest of eax's bits will be set to 1.
hope this makes sense and helps someone out there..
for example, a string with bytes treated as unsigned:
where esi is a base pointer and ecx is the index.
movzx eax, byte ptr [esi+ecx]
the movzx instruction (move with zero-extend) extends an 8-bit value to a 16-bit value, or an 8-bit or 16-bit value to a 32-bit value by padding the high-order with zeros. the result in this case is that al holds the source byte and the rest of eax is cleared.
on the contrary, when the source is treated as signed, the msb (most significant bit) of the source is used to extend the source value.
movsx eax, byte ptr [esi+ecx] ; move with sign-extend
now, had the source byte been signed, the result would have been that al is the source byte unchanged, and the rest of eax's bits are set to 1. had the source byte not been signed, the result would have been the same as if movzx had been used.
we'll look at a couple of actual cases to help clarify this further:
1) movzx eax, byte ptr [esi+ecx]
al will always hold the source byte and the rest of eax will always be cleared.
2) movsx eax, byte ptr [esi+ecx] * *; source byte is <= 127 dec
al will hold the source byte and rest of eax will be cleared.
3) movsx eax, byte ptr [esi+ecx] * *; source byte is > 127 dec
al will hold the source byte and the rest of eax's bits will be set to 1.
hope this makes sense and helps someone out there..