Re: A86: ASM Programming Test: div 3
[Prev][Next][Index][Thread]
Re: A86: ASM Programming Test: div 3
why are you dividing then? use fixed point to multiply by .3333...
you can do 16 bit left shifts
256*.333=$00.56=%00000000.01010110 (rounded up)
;de=number
;h.l=de/3
push de
ld h,d
ld l,e
add hl,hl
add hl,hl ;*$0004
add hl,de ;*$0005
ld d,h
ld e,l
add hl,hl
add hl,hl
add hl,hl
add hl,hl ;*$0010
add hl,de ;*$0055
pop de
add hl,de ;*$0056
this will work up to $02fa, which gives 762/3=255.something instead of
254.whatever
l is the fractional part, h is the integer part of the answer
-josh
On Wed, 9 Dec 1998 11:43:59 -0600 "David Phillips" <electrum@tfs.net>
writes:
>
>I spent about a day trying to figure this out. It's not possible.
>To
>divide by shifting requires at least a 16 or 32 bit fixed-point
>number, and
>it still comes out a little too bit too small (reference: "More Tricks
>of
>the Game Programming Gurus"). For example, to divide x by 3 in C (the
>">>"
>operator means shift right N places...same as the assembler uses):
>
>x_div_3 = (x >> 2) + (x >> 4) + (x >> 6) + (x >> 8)
>
>I ended up using a little subtraction loop to divide a number (code
>cut from
>program):
>
> ld a,(MapY) ; load y coord
> ld b,3 ; need to divide by 3
> ld c,0 ; it starts at 1, always add once
>DoRadarL:
> inc c ; increase quotient
> sub b ; subtract divisor
> jr nc,DoRadarL ; loop until it's negative
>
>The dividend is in A, the divisor in B and C is the quotient. It
>just
>increments the quotient and decrements the dividend by the divisor
>until the
>dividend is less than 0. If you add the divisor to the dividend at
>the end,
>then you will have the remainder as well.
>
>-----Original Message-----
>From: Dux Gregis <assets@eden.rutgers.edu>
>To: assembly-86@lists.ticalc.org <assembly-86@lists.ticalc.org>
>Date: Wednesday, December 02, 1998 3:26 PM
>Subject: Re: A86: ASM Programming Test
>
>
>>
>>How about for the challenge write a call that divides the accumulator
>by 3?
>
>
___________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com/getjuno.html
or call Juno at (800) 654-JUNO [654-5866]