divw
Divide Word - 7C 00 07 D6
divw

Instruction Syntax

Mnemonic Format Flags
divw rD,rA,rB OE = 0, Rc = 0
divw. rD,rA,rB OE = 0, Rc = 1
divwo rD,rA,rB OE = 1, Rc = 0
divwo. rD,rA,rB OE = 1, Rc = 1

Instruction Encoding

0
1
1
1
1
1
D
D
D
D
D
A
A
A
A
A
B
B
B
B
B
OE
1
1
1
1
0
1
0
1
1
Rc

Field Bits Description
Primary Opcode 0-5 011111 (0x1F)
rD 6-10 Destination register
rA 11-15 Dividend register
rB 16-20 Divisor register
OE 21 Overflow Exception
XO 22-30 111101011 (491)
Rc 31 Record Condition Register

Operation

rD ← (rA) ÷ (rB)

The signed contents of register rA are divided by the signed contents of register rB. The signed quotient is placed in register rD. Both operands and the result are interpreted as 32-bit signed integers.

Note: Division by zero and overflow conditions are handled according to the OE bit setting. If the quotient cannot be represented in 32 bits, the result is undefined.

Affected Registers

Condition Register (CR0 field)

(if Rc = 1)

Note: CR0 field may not reflect the correct comparison if overflow occurs (see XER below).

XER (Exception Register)

(if OE = 1)

Overflow occurs when:

For more information on condition codes see Section 2.1.3, "Condition Register," and Section 2.1.5, "XER Register," in the PowerPC Microprocessor Family: The Programming Environments manual.

Examples

Basic Division

li r4, 100          # Dividend = 100
li r5, 4            # Divisor = 4
divw r3, r4, r5     # r3 = 100 / 4 = 25

Signed Division

li r4, -100         # Dividend = -100
li r5, 3            # Divisor = 3
divw r3, r4, r5     # r3 = -100 / 3 = -33 (truncated toward zero)

Division with Condition Register

li r4, 50           # Dividend
li r5, 7            # Divisor
divw. r3, r4, r5    # r3 = 50 / 7 = 7, sets CR0
# CR0[LT] = 0, CR0[GT] = 1, CR0[EQ] = 0

Division with Overflow Detection

li r4, 100          # Dividend
li r5, 0            # Divisor = 0 (will cause overflow)
divwo r3, r4, r5    # Division by zero, sets XER[OV] and XER[SO]
# Result is undefined, overflow exception may occur

Safe Division with Check

# Safe division that checks for zero divisor
lwz r4, dividend    # Load dividend
lwz r5, divisor     # Load divisor
cmpwi r5, 0         # Check if divisor is zero
beq div_by_zero     # Branch if zero
divw r3, r4, r5     # Perform division
b div_done          # Skip error handling

div_by_zero:
li r3, 0            # Set result to 0 for zero division
# Or handle error as appropriate

div_done:
# Continue with result in r3

Integer Division with Remainder

# Calculate both quotient and remainder
li r4, 17           # Dividend = 17
li r5, 5            # Divisor = 5
divw r3, r4, r5     # r3 = quotient = 3
mullw r6, r3, r5    # r6 = quotient * divisor = 15
subf r7, r6, r4     # r7 = remainder = 17 - 15 = 2

Related Instructions

divwu, mullw, mulhw, subf

Back to Index