• Please review our updated Terms and Rules here

Z80 divide by 10

Dwight Elvey

Veteran Member
Joined
Jun 21, 2003
Messages
4,995
Location
Santa Cruz
Hi
I had this question up on cclassic list but thought I'd ask here as
well. For the floppy controller I'm writing code for, I need to divide
by 10 and keep the remainder. We've come up with some code that
we think is close to optimal but I thought I'd bring it to a larger
audience ( I know there is some overlap here ).

Here is what we have so far:

; HL dividend less then 1023d ( only need 0-799 )
; returns A quotient
; H remainder


Div10:
xor a
ld b,#7
ld de,#-640d
divloop:
add hl,de
jr c, div1
sbc hl,de
ora a ; not sure if this is needed here to clear carry
div1:
rl a
add hl,hl
djnz divloop
add hl,hl
ret

Any suggestions for improvements welcome.
Dwight
 
"Z80 divide by 10"?

Wouldn't that be Z8? :p

(Sorry, couldn't resist. I'm not much of a programmer, so I can't really help you out there. But snide remarks? Got those aplenty.)
 
I'm quite experienced with 8080/8085, and Z80 is similar, but what is the:

jr c,xxx

Instruction doing (or, if you know the hex OpCode, that would tell me too)?

The rest of it, I'm following...
Code:
Div10:    xor a
          ld b,#7
          ld de,#-640d

divloop:  add hl,de
          jr c, div1
          sbc hl,de
          ora a                 ; not sure if this is needed here to clear carry
div1:     rl a
          add hl,hl
          djnz divloop
          add hl,hl
          ret
 
Last edited:
Hi
Z80 does jump relatives. jr c,div1 will skip the restore
step after the trial subraction using add hl,de ( de negative) .
for Z80 instructions refer to:

http://z80.info/zip/z80cpu_um.pdf

special Z80 instructions include SBC HL,DE and the DJNZ as well as the JR.
Dwight


I'm quite experienced with 8080/8085, and Z80 is similar, but what is the:

jr c,xxx

Instruction doing (or, if you know the hex OpCode, that would tell me too)?

The rest of it, I'm following...
Code:
Div10:    xor a
          ld b,#7
          ld de,#-640d

divloop:  add hl,de
          jr c, div1
          sbc hl,de
          ora a                 ; not sure if this is needed here to clear carry
div1:     rl a
          add hl,hl
          djnz divloop
          add hl,hl
          ret
 
So JR C means jump relative (branch) if carry flag is set. I see from the listing that you have JR NC, JR Z (zero flag), JR NZ as well. The famous DJNZ command decreases B register, checks for non-zero and then branches.

What amazes me is that generally two different processors never share the same type of instruction set, much less mnemonics. There are some similarities within a family, like 8080/Z80 and 6800/6502, but these don't cross eachother. I believe more uncommon CPUs like TMS9900, RCA1802, CP1610, Fairchild F8 and so on also are quite distinct in command repertoaire, register layout and associated mnemonics.
 
Hi
I had this question up on cclassic list but thought I'd ask here as
well. For the floppy controller I'm writing code for, I need to divide
by 10 and keep the remainder. We've come up with some code that
we think is close to optimal but I thought I'd bring it to a larger
audience ( I know there is some overlap here ).

Here is what we have so far:

; HL dividend less then 1023d ( only need 0-799 )
; returns A quotient
; H remainder


Div10:
xor a
ld b,#7
ld de,#-640d
divloop:
add hl,de
jr c, div1
sbc hl,de
ora a ; not sure if this is needed here to clear carry
div1:
rl a
add hl,hl
djnz divloop
add hl,hl
ret

Any suggestions for improvements welcome.
Dwight

Hi
Made some more improvements. It isn't optimum for clocks but
it is minimum size:


; Divide a number 0-799 by 10
; HL = input
; returns:
; A = quotient
; H = remainder

Div10:
ld a,#2 ; flag for end of loop
ld de,#-1280d ; 10 shifted 8 times
Divloop:
add hl,hl ; early shift to use carry to check loop
add hl,de ; trial subtract
jr c,div1 ; if not carry, subracted too much
sbc hl,de ; so restore value
div1:
rl a ; shift carry into A and shift loop flag
jr c,divloop ; If carry then looped 7 times
add hl,hl ; align remainder
ret

Dwight
 
Back
Top