Автор: ne_viens
Дата сообщения: 08.11.2012 18:17
Код:
;The 8051/8052 Microcontroller: Architecture, Assembly Language, And Hardware Interfacing
;By Craig Steiner
;R3, R2 - divident
;R5, R4 - divisor
mov R2, #0FFh
mov R3, #0FFh
mov R4, #81h
mov R5, #0
divide:
clr A
mov B, A ; initialize count
mov R0, A ; zero quotient
mov R1, A
sjmp L3
;---------------------------------------------------------
L1:
mov A, R3
clr C
subb A, R5 ; is divisor greater than dividend yet
jc L4 ; yes, go no further
jnz L2
mov A, R2
subb A, R4
jc L4
L2:
mov A, R4 ; shift divisor up one bit
clr C
rlc A
mov R4, A
mov A, R5
rlc A
mov R5, A
L3:
inc B ; increment count
mov A, R5 ; check for safe to shift some more
jnb ACC.7, L1 ; loop if top bit clear
;-------------------------------------------------------
L4:
mov A, R3
clr C
subb A, R5 ; is divisor greater than dividend
jc L5
jnz L6
mov A, R2
subb A, R4
jnc L6
L5:
clr C
sjmp L7
L6:
clr C ; subtract divisor from dividend
mov A, R2
subb A, R4
mov R2, A
mov A, R3
subb A, R5
mov R3, A
setb C ; now set bit for quotient
L7:
mov A, R0
rlc A
mov R0, A
mov A, R1
rlc A
mov R1, A
mov A, R5 ; shift divisor down
clr C
rrc A
mov R5, A
mov A, R4
rrc A
mov R4, A
djnz B, L4 ; and continue with the rest
;---------------------------------------------------------
; mov 5, R3
; mov 4, R2
; mov 3, R1
; mov 2, R0
ret