Skip to main content
added 331 characters in body
Source Link
matja
  • 606
  • 4
  • 5

You can rephrase it by saying how to multiply by the reciprocal of 3 which is binary 0.01010101(..)

You can calculate a*01010110 (rounding up to handle exact multiples) and then return the high byte.

Z80 code to divide a by 3:

 ld b, 0 ;  ld c, $56 ; initialize with reciprocal = ceil((1/3)*256) ld hl, 0   ; accumulate ldin e,8BC loop: rra ; divide A by 2, putting low bit in carry jr nc, skip ; if low bit was 0, don't accumulate add hl, bc ; low bit was 1, accumulate the reciprocal skip: sla c ; multiply reciprocal by 2 rl b ; " or a ; any bits left? jr nz, loop ; yes? - then do next bit ld a, h ; copy high byte of result to A  

You can rephrase it by saying how to multiply by the reciprocal of 3 which is binary 0.01010101(..)

You can calculate a*01010110 (rounding up to handle exact multiples) and then return the high byte.

Z80 code to divide a by 3:

 ld b,0 ld c,$56 ld hl,0   ld e,8 loop: rra jr nc, skip add hl,bc skip: sla c rl b or a jr nz, loop ld a,h 

You can rephrase it by saying how to multiply by the reciprocal of 3 which is binary 0.01010101(..)

You can calculate a*01010110 (rounding up to handle exact multiples) and then return the high byte.

Z80 code to divide a by 3:

 ld b, 0 ;  ld c, $56 ; initialize with reciprocal = ceil((1/3)*256) ld hl, 0 ; accumulate in BC loop: rra ; divide A by 2, putting low bit in carry jr nc, skip ; if low bit was 0, don't accumulate add hl, bc ; low bit was 1, accumulate the reciprocal skip: sla c ; multiply reciprocal by 2 rl b ; " or a ; any bits left? jr nz, loop ; yes? - then do next bit ld a, h ; copy high byte of result to A  
Source Link
matja
  • 606
  • 4
  • 5

You can rephrase it by saying how to multiply by the reciprocal of 3 which is binary 0.01010101(..)

You can calculate a*01010110 (rounding up to handle exact multiples) and then return the high byte.

Z80 code to divide a by 3:

 ld b,0 ld c,$56 ld hl,0 ld e,8 loop: rra jr nc, skip add hl,bc skip: sla c rl b or a jr nz, loop ld a,h