;------------------------------------------------------------------ ; 16 bit fixpoint math rutines ver 1.0 ; ; ; ; 16 bit fixpoint and 2's complement 16 bit fixpoint ; ; math rutines by Dines Justesen Corp. 1996 ; ; ; ; Some rutines is based on work by or written by the following ; ; people: ; ; Magnus Hagander (e95_mha@e.kth.se) ; ; Darryl Nester (nesterd@bluffton.edu) ; ; ; ; You are allow to include the following rutines in your programs ; ; as long as you follow the rules stated below. ; ; ; ; If you use any of the functions in one of your programs give me ; ; credit for my work. Some of the functions included was made by ; ; others or based on some one elses work, they too should get the ; ; credit for their work. One way of doing this is by including ; ; lines like the ones below in the documentation. ; ; ; ; Fixpoint math by Dines Justesen (c958362@student.dtu.dk) ; ; Some rutines by/based on work by Magnus Hagander ; ; ; ; You are NOT ALLOWED to distribute any modified versions of these; ; rutines, or only parts of the document. ; ; ; ; The reason i decieded to release the source code for these ; ; rutines was to help other people getting stared with more math ; ; intensive programs. So please don't just rip things off, either; ; give methe credit or make your own rutines. ; ; ; ; This is the first version and some of the rutines are completly ; ; new,so there might by bugs in some rutines. The rutines to show ; ; numbers was written really fast so they a probably a bit slow. ; ; If you find any bugs please report them to me. Any suggestion of; ; new rutines or improvements of the in the document are very ; ; welcome, and i will include them in a future relase. ; ; ; ; Send comments and ideas to: ; ; Dines Justesen : c958362@student.dtu.dk or ; ; dines@MAILHOST.NET ; ; http://www.gbar.dtu.dk/~c958362/ ; ; Dines justesen april, 1996 ; ;------------------------------------------------------------------ #INCLUDE "TI-85.H" RES = $80DF ; Used for temporary storage .org 0 ;------------------------------------------------------------------ ; ; Program to test some of the fixpoint rutines ; ;------------------------------------------------------------------ .db "Fix Test",0 ld a,4 ; Clear LCD out (5),a ROM_CALL(CLEARLCD) ld l,11111111b ;Show a negative 2's complement number ld h,11111111b CALL_(ShowFix2) CALL_(Loop) ld bc,$0280 ;Multiply 2.5 and 16.125 ld de,$1020 CALL_(Mul16) ld h,c ld l,d CALL_(ShowFix2) CALL_(Loop) ld hl,1111110110000000b ;add -2.5 and -3.25 ld bc,1111110011000000b CALL_(Add162) CALL_(ShowFix2) KeyLoop1: call GET_KEY cp $37 ret z ; return if [EXIT] is pressed jr KeyLoop1 Loop: ; Label call GET_KEY ; Call getkey cp 9 ; Enter ? jr nz,Loop ; No, loop ret ;------------------------------------------------------------------ ; ; Below is the math rutines tested above ; ;------------------------------------------------------------------ ;------------------------------------------------------------------ ; ; Addition + Subtraction ; ; Both of these operations are buldt in the z80. Use sbc, add, and ; add. ; ;------------------------------------------------------------------ ;------------------------------------------------------------------ ; ; Add162 ; ; Addition of 2 16 bit fixpoint 2's complement number ; ; Entry : Hl = Op1 BC = Op2 ; Exit : HL = Op1+Op2 ; ;------------------------------------------------------------------ Add162: ;H bit 7 = x = B bit 7 check at res bit 7 = x bit 7,H ;Check bit 7 jr nz,First1 ;Jump if 1 bit 7,B ;Check bit 7 jr nz,Zeroes ;Jump if 0 AddIt: Add Hl,BC ;Add AND A ;Clear carry Ret ;Return First1: bit 7,b ;Check bit 7 jr z,AddIt ;If 0 then add Add HL,BC ;Add Bit 7,H ;Check bit 7 Jr z,AddOv ;If 0 then overflow !! AND A ;Clear carry Ret ;Return Zeroes: Add Hl,BC ;Add Bit 7,H ;Check bit 7 jr nz,AddOv ;If 1 then overflow !! AND A ;Clear carry Ret ;Return AddOv: Scf ;Set carry flag Ret ;Return ;------------------------------------------------------------------ ; ; Sub162 ; ; Subtraction of 2 16 bit fixpoint 2's complement number ; ; Entry : Hl = Op1 DE = Op2 ; Exit : Hl = Op1-Op2 ; ;------------------------------------------------------------------ ; bit 7 = X = not bit 7 res=X Sub16: ; (-x)-(y) = -z (x)-(-y) = +z bit 7,h jr nz,SFirst1 bit 7,d jr nz,PosRes JustSub: And A ;Clear carry Sbc Hl,DE ;Subtract And A ;Clear carry Ret ;Return SFirst1: bit 7,d jr nz,JustSub And A ;Clear carry Sbc Hl,De ;Subtract bit 7,h jr z,SOver And A ;Clear carry Ret ;Return PosRes: And A Sbc Hl,De bit 7,h jr nz,SOver And A Ret SOver: Scf ;Set carry Ret ;Return ;------------------------------------------------------------------ ; ; Mul16 ; ; Multiplication of two 16 bit numbers giving a 32 bit result. ; If fixpoint numbers are used then discard 8 LSB's if the 8 MSB's ; <>0 then the number is too big to stor as normal 16 bit fixpoint. ; ; Entry : BC = Mult1 DE= Mult2 ; Exit : BCDE = Mult1*Mult2 (B=MSB, E=LSB) ; Destroys hl ; ;------------------------------------------------------------------ Mul16: push af ;Save regs push hl ld hl,0 ;reset hl ld a,10h ;counter = 16 bits mulloop: add hl,hl ;shift left temp result rl e ;move next bit to carry rl d jr nc,mulover ;if bit=0 then next add hl,bc ;else add multplcant to temp. result jr nc,mulover ;if no overflow then continue inc de ;else inc high 16 bits mulover: dec a ;dec counter jr nz,mulloop ;if all bits done ex de,hl ;move low 16 bits of result to DE ex (sp),hl ;put high 16 bits on stack pop bc ;Bc = high 16 bits of result pop af ;Restore AF ret ;------------------------------------------------------------------ ; Cmp16 ; ; 16 bit Compare works for both normal fixpoint numbers and 2's comp. ; This rutine is from the book Z80 assembly language subrutines by ; Lance A. Leventhal. ; ; Entry : HL=Value1 De=Value2 ; Exit : if 2'scomplement is used then ; zero=1 and sign=0 then Value1=Value2 ; zero=0 and sign=0 then Value1>Value2 ; zero=0 and sign=1 then Value1ascii convertion of fracs. C6: .db $25,$00,$00,$00 C5: .db $12,$50,$00,$00 C4: .db $06,$25,$00,$00 C3: .db $03,$12,$50,$00 C2: .db $01,$56,$25,$00 C1: .db $00,$78,$12,$50 C0: .db $00,$39,$06 Cstart: ;Make it easy to point last byte .db $25 .END