Say I have an integer (int) as a dword in edx and an integer pointer (uint8_t*) as a qword in rcx passed by C++. I wish to add edx to rcx. Is it best to sign extend edx ("movsxd rdx,edx"?) to match the size of the qword or is it better to restrict the qword in rcx to it's lower 32bits and add edx to ecx instead.
Either zero or sign extending the 32 bit register would be the better option so that you maintain the native 64 bit data size.
I'm assuming it would be preferred to sign extend over zero extend where possible though? Currently the compiler will zero the upper 32 bits of rdx when the parameter (dword) is passed to assembly. Would the below code would be the solution if the dword was signed?
mov rdx,edx
If the dword was unsigned would sign extension and zero extension result in the same thing in these cases?
if you are handling signed integers, sign-extend the value
if you are handling unsigned integers, zero-extend it
a common issue with newbies is that they don't understand that a register may hold either type
it's a matter of context
i.e., the programmer typically determines whether it's signed or unsigned
let me offer a simple example.....
you have a value in register with all bits set to 1
0FFFFFFFFh = 32 bits set to 1
if it's an unsigned number, it is equal to 4,294,967,295 (easy)
if it's a signed number, it is equal to -1, as the "two's compliment" number system is used
all values with the high bit set are negative
two's compliment is used so that common operations are valid either way
for example, if we add +1 to -1, we get 0
if we add 1 to 4,294,967,295, we also get 0 :shock:
(the carry flag will be set)