X86-64 Divide Instruction Flashcards
When a 4 byte value is written to a 4 byte register, the upper 4bytes are set to _____
0
%eax: 0x47372000
%edx: 0x00000757
We want 8-byte result: 0x000007574372000
Shlq $32, %rdx. //shift left 5 position
Orq %rdx, %rax //can also do addq
What if multiply ushering imulw %cx giving us a 4 byte result where the lease significant 2bytes are %ax and most significant 2 bytes are %dx.
- GARBAGE can be in the upper 6 bytes in both register
- Must combine 2bytes values in %ax and %dx so we have a 4 byte value
Mild for ________, doesn’t allow for use of more than one operant and always (_________) results in our 2w bit product being split between 2 registers. And we don’t have an option to compute a w-bit product
For unsigned values, except for 1-byte products
If we are using imulX (for signed values) we have two other options
ImulX Sri, Dest
ImulX Imm, Src, Dest
But these options produce 2-bit results, so overflow is possible
Imull %edx, %eax
Mule %dx
Full 4bytes result is in %eax
4-byte results is half of %dx and half of %ax
What if we have 4-byte unsigned values
- We could use mull %edx and have our 8-byte product split between %eax and %rdx
- We could use imulY with two 8-byte operands: imulq %rdx, %rax
Now we have an 8 byte results for the 2nd option in %rax, no comtamination of %rdx register and knowledge tha there is not overflow
What if we have two byte signed values??
Two byte unsigned values
%ax contains multiple ant and %dx contains multiplier
- Zero extend both register since upper bytes could have garbage and we know unsigned value
Movzwl %ax, %eax
Movzwl %dx, %edx - Use imulX with 2 4 byte operants
Imull %edx, %eax
2 byte signed value
%ax contains multiplicand, %dx contains multiplier
- Signe extend from 2 bytes to 4 bytes because we know signed value
Movswl %ax, %eax
Movswl %dx, %edx - Use imull with 2 4-byte operants
Imull %edx, %eax
Now we have 4 byte results in %eax, no contamination of %edx register
Cltq
Sign extends %eax register to %rax
Short form of Movslq %eax, %rax
Cqto
- Copies the sign bit of %rax to all the bits of %rdx
128 bit division
- ALU uses 2 8 byte register (%rax, %rdx)
- Dividend is a only in %rax, must se %rdx to all 1’s or 0’s (sign or zero extend)
- Either divq (unsigned) or idivq (signed) divide instruction is sued
Suppose division and
%rdx (some address *qp)
%rax (x)
%rdx: 3rd parameter
%rax: 0xfffffffffffffffb (-5)
Cqto
%rdx = 0xffffffffffffffff %rax= 0xfffffffffffffffb
Idivq %rsi (divide by y)
%rdx = 0xffffffffffffffff (x %y)
%rax = 0cfffffffffffffffe (-2) -> (x/y)
Unsigned division makes use of the _____ instruction. Typically, register %rdx is set to ____ beforehand rather than sign extending (using Cqto) and unsigned value because that can cause errors
Divq, 0
When dividing signed 32-bit values (4 byte) the dividen
1) Must be placed in register _____
2) you must signe extend ____ to _____ (cltq will take care of this)
3) you must replicate the top half of ______ in _____. There are atleast 2 ways to do this, but it can be accomplished with Cqto
%eax
%eax, %rax
%rax, %edx