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

Use cdq / idiv for signed 32-bit / 32-bit => 32 bit division,xor edx,edx / div for unsigned. With the dividend in EAX to start with, and the divisor specified as an operand to DIV or IDIV. 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 … Read more

What does movslq do?

It’s movsxd: https://www.felixcloutier.com/x86/MOVSX:MOVSXD.html. You could figure this out yourself by assembling it with an AT&T assembler and disassembling with an Intel-syntax disassembler. (e.g. objdumpd -d -Mintel foo.o) And yes, it does 32->64-bit 2’s complement sign extension, extending by copying the sign-bit of the source to all the new upper bits. (i.e. dst[63:32] = src[31], and dst[31:0] = src[31:0].) Fun fact: it’s … Read more