Empirically, I've found that this routine (i8080) divides value from E by B and returns the result in D and E (result and remainder of division).
divide_by_12_with_rest:
MVI C, 9 ; @1
MOV A, D ; @2
loop_1:
MOV D, A ; @3
MOV A, E
RAL
MOV E, A ; @4
DCR C ; @5
JZ exit
MOV A, D ; @6
RAL ; @7
SUB B ; @8
JNC loop_1
ADD B ; @9 - this will always set CF
JMP loop_1
exit:
MOV A, E
CMA
MOV E, A ; @a
RET
Please, could you tell me whether my assumption is right? What is the math behind. Saw that DE is shifted left 8 times. Don't understand purpose of SUB B
/ADD B
. Thanks a lot. This code was written in 1976-7 and it was running in an arcade machine.
Updated
Debugging result
BC: 0c03
DE: 0030
HL: 0036
AF: 0046
divide_by_B_with_rest
@1 C: 9
@2 A = D: 00
Loop:
@3 D = A: 00 00 00 01 03 06 00 00 00
@4 E: 60 c1 83 07 0f 1f 3e 7d fb
@5 C: 8 7 6 5 4 3 2 1 0
@6 A = D: 00 00 00 01 03 06 00 00
@7 A: 00 00 00 03 06 0c 00 00
@8 A: f4 (cf) f4 (cf) f5 (cf) f7 (cf) fa (cf) 00 f4 (cf) f4 (cf)
@9 A: 00 (cf) 00 (cf) 01 (cf) 03 (cf) 06 (cf) -- 00 (cf) 00 (cf)
Exit:
@a E: 4, D: 0
Update 2
environment used to debug the code (Rust/React/MongoDB)
RAL
) in the third iteration of the loop the accumulator is zero?