Monday, July 9, 2007

Modifing the size of your integer value

When moving a integer, from say a smaller size field (word) to say a doubleword, you should not just movw to the new register.

movw %ax, %bx

This should not be done because you can not be certain that the upper part of the EBX register is zeroed out ahead of time. To do this you should first zero out the EBX (destination) and then move your intended value there.

movl $0, %ebx
movw %ax, %bx

Intel provides another instruction that can do this with one instruction, movzx. It takes a source (a register or memory location) and converts to a larger size (register only) destination.

movl $300, %ecx
movzx %cl, %ebx

Just thought this was interesting.

No comments: