A86: Re: divide by 2


[Prev][Next][Index][Thread]

A86: Re: divide by 2




Hehe, that's a "classic" newbie question.  If you're talking floating point,
just use the call...

_FPDIV -- divide OP1 by OP2

For registers, this is very simple.  Think in binary when you program.  If
you shift a binary number left, it's multiplied by two.  If you shift it
right, it's...drumroll...divided by two...

  ld a,5
  or a    ; clear carry flag (probably not needed)
  rra     ; divide by two!!!

  ld c,17
  srl c   ; divide by two

  ld b,5
  sra b  ; multiply by two

  ld a,78
  rra     ; divide by 8
  rra
  rra
  and %00011111    ; mask off excess bits

If you're using A, you can use <rla> and <rra>, which is good because
they're one byte, 4 t-state instructions.  Just remember that the carry flag
is rotated into the other end, so if you are doing multiple shifts, you will
probably have to mask off (AND) the extra bits.  <sra> and <srl> can be used
with any register and set the other bit to 0 (which you want), but take up
two bytes and 8 t-states (twice an RLA or RRA).

>
>    I need to write an algorithm in z80 to divide by two.  I need to do it
>with on add and sub (no mult).  I know i should be able to figure this out
>but i can't.  Please help.
>    Also, the 86 displays about 12-14 digits on the screen (right?).  After
>that it goes to scientific notation (n * 10 ^ x).  I thought I heard
>somewhere that the 86 does internal calculations much more accurately.  Can
>anyone tell me about this, and is there any to access this internal value?