DIVXS
DIVXS (DIVide eXtend as Signed)
Divide Signed
2. Programming solution for DIVXS.W R0, ER1
Example: Convert dividend and divisor to non-negative numbers, then use DIVXU programming
solution for zero divide and overflow
MOV.W
R0, R0
; Test divisor
BEQ
ZERODIV
; Branch to ZERODIV if R0 = 0
ANDC
#AF, CCR
; Clear CCR user bits (bits 6 and 4) to 0
BPL
L1
; Branch to L1 if N flag = 0 (positive divisor)
NEG.W
R0
; Take 2’s complement of R0 to make sign positive
ORC
#10, CCR
; Set CCR bit 4 to 1
L1:
MOV.L
ER1,ER1
; Test dividend
BPL
L2
; Branch to L2 if N flag = 0 (positive dividend)
NEG.L
ER1
; Take 2’s complement of ER1 to make sign positive
XORC
#50,CCR
; Invert CCR bits 6 and 4
L2:
MOV.W
E1, R2
;
EXTU.L
ER2
;
DIVXU.W
R0, E2
;
Use DIVXU.W instruction to divide non-negative dividend
MOV.W
E2, R1
;
by positive divisor
DIVXU.W
R0, ER1
;
32 bits ÷ 16 bits
→
quotient (32 bits) and remainder
MOV.W
R2, E2
(16 bits)
MOV.W
R1, R2
(See DIVXU Instruction, Zero Divide, and Overflow)
STC
CCR, R1L
; Copy CCR contents to R1L
BTST
#6, R1L
; Test CCR bit 6
BEQ
L3
; Branch to L3 if bit 6 = 1
NEG.W
E1
; Take 2’s complement of E1 to make sign of remainder negative
L3:
BTST
#4, R1L
; Test CCR bit 4
BEQ
L4
; Branch to L4 if bit 4 = 1
NEG.L
ER2
; Take 2’s complement of ER2 to make sign of quotient negative
L4:
RTS
ZERODIV:
; Zero-divide handling routine
This program leaves a 32-bit quotient in ER2 and a 16-bit remainder in E1.
88
ER1
E1
ER2
R0
Divisor
Dividend
Remainder
Quotient