sign-extension

When and why do we sign extend and use cdq with mul/div?

泄露秘密 提交于 2019-11-26 16:47:57
I had a test todayand the only question I didn't understand involved converting a doubleword to a quad word. That got me thinking, why/when do we sign extend for multiplication or division? In addition, when do we use instructions like cdq? Use cdq / idiv for signed 32-bit / 32-bit => 32 bit division, xor edx,edx / div for unsigned. If you zero EDX/RDX instead of sign-extending into EDX:EAX before idiv , you can get a large positive result for -5 / 2, for example . Using the "full power" of 64 / 32-bit => 32-bit division is possible, but not safe unless you know the divisor is large enough so

Is a sign or zero extension required when adding a 32bit offset to a pointer for the x86-64 ABI?

假如想象 提交于 2019-11-26 01:40:36
问题 Summary: I was looking at assembly code to guide my optimizations and see lots of sign or zero extensions when adding int32 to a pointer. void Test(int *out, int offset) { out[offset] = 1; } ------------------------------------- movslq %esi, %rsi movl $1, (%rdi,%rsi,4) ret At first, I thought my compiler was challenged at adding 32bit to 64bit integers, but I\'ve confirmed this behavior with Intel ICC 11, ICC 14, and GCC 5.3. This thread confirms my findings, but it\'s not clear if the sign