X86-64 Divide Instruction Flashcards

1
Q

When a 4 byte value is written to a 4 byte register, the upper 4bytes are set to _____

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

%eax: 0x47372000
%edx: 0x00000757

We want 8-byte result: 0x000007574372000

A

Shlq $32, %rdx. //shift left 5 position
Orq %rdx, %rax //can also do addq

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

For unsigned values, except for 1-byte products

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If we are using imulX (for signed values) we have two other options

A

ImulX Sri, Dest
ImulX Imm, Src, Dest

But these options produce 2-bit results, so overflow is possible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Imull %edx, %eax
Mule %dx

A

Full 4bytes result is in %eax
4-byte results is half of %dx and half of %ax

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What if we have 4-byte unsigned values

A
  1. We could use mull %edx and have our 8-byte product split between %eax and %rdx
  2. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What if we have two byte signed values??

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Two byte unsigned values
%ax contains multiple ant and %dx contains multiplier

A
  1. Zero extend both register since upper bytes could have garbage and we know unsigned value
    Movzwl %ax, %eax
    Movzwl %dx, %edx
  2. Use imulX with 2 4 byte operants
    Imull %edx, %eax
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

2 byte signed value
%ax contains multiplicand, %dx contains multiplier

A
  1. Signe extend from 2 bytes to 4 bytes because we know signed value
    Movswl %ax, %eax
    Movswl %dx, %edx
  2. Use imull with 2 4-byte operants
    Imull %edx, %eax

Now we have 4 byte results in %eax, no contamination of %edx register

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Cltq

A

Sign extends %eax register to %rax
Short form of Movslq %eax, %rax

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Cqto

A
  • Copies the sign bit of %rax to all the bits of %rdx
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

128 bit division

A
  1. ALU uses 2 8 byte register (%rax, %rdx)
  2. Dividend is a only in %rax, must se %rdx to all 1’s or 0’s (sign or zero extend)
  3. Either divq (unsigned) or idivq (signed) divide instruction is sued
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Suppose division and
%rdx (some address *qp)
%rax (x)

%rdx: 3rd parameter
%rax: 0xfffffffffffffffb (-5)

A

Cqto
%rdx = 0xffffffffffffffff %rax= 0xfffffffffffffffb
Idivq %rsi (divide by y)
%rdx = 0xffffffffffffffff (x %y)
%rax = 0cfffffffffffffffe (-2) -> (x/y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

Divq, 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

%eax
%eax, %rax
%rax, %edx