A86: Math program


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

A86: Math program




I'm converting a math program I had written in Basic to ASM.
It will do somethind like

prompt A,B,C
(-B+(sqrt)(B^2-4*A*C))/(2*A)->X
(-B-(sqrt)(B^2-4*A*C))/(2*A)->X
Disp X,Y
-B/(2*A)->X
-(B^2-4*A*C)/(4*A)->Y
Disp X,Y

However with some number like A=5 B=6 and C=5 I keep getting either ERROR 15
MEMORY
or 02 DIV BY ZERO or I get different numbers for the X and Y

Can someone take a look at it?

Also, I copied a prompt routine from a triangle tester program of Elroy
(elroy@usa.net)
and still dont understand it. 
What is call _mov10b for? And (_asap_ind) ? And call _exec_pg3?

Thanks




#define equ .equ
#define EQU .equ
#define end .end



_asm_exec_ram   equ     		0D748h
_clrLCD         equ     		4A7Eh
_runindicoff    equ     		4AB1h
_clrWindow		equ				4A86h
_puts           equ             4A37h
_curRow         equ     		0C00Fh
_homeUp			equ				4A95h
_dispOP1		equ				515Bh
_asap_ind		equ				0D623h
_exec_pg3		equ				5714h
_homeUp			equ				4A95h
_ioprompt		equ				0C324h
_mov10b			equ				427Bh
_formReal   	equ 			5191h
_getkey         equ             55AAh

;------Floating Point Stack------

_push_OP1 		equ 			4813h
_push_OP2 		equ 			480Fh
_push_OP3 		equ 			480Bh
_push_OP4 		equ 			4807h
_push_OP5 		equ 			4803h
_push_OP6 		equ 			47FFh
_pop_OP1 		equ 			47B7h
_pop_OP2 		equ 			47B3h
_pop_OP3 		equ 			47AFh
_pop_OP4 		equ 			47ABh
_pop_OP5 		equ 			47A7h
_pop_OP6 		equ 			47A3h

;-----------OPs--------------

_OP1TOOP2       equ             4233h
_OP1TOOP3       equ             421Fh
_OP1TOOP4       equ     		4207h
_OP1TOOP5       equ     		426Bh
_OP1TOOP6       equ     		4263h
_OP2TOOP1       equ             426Fh
_OP3TOOP1       equ             424Bh
_OP3TOOP2       equ             421Bh
_OP4TOOP1       equ             4243h
_OP4TOOP2       equ             4213h
_OP5TOOP1       equ             4247h
_OP5TOOP2       equ             4227h
_OP6TOOP1       equ             423Fh
_OP6TOOP2       equ             423Bh
_ZEROOP1        equ     		436Bh
_ZEROOP2        equ     		436Fh
_ZEROOP3        equ     		4373h
_OP1SET0        equ             435Fh
_OP1SET2        equ             431Fh
_OP1SET4        equ             4313h
_OP2SET0        equ            435Bh
_OP2SET4        equ             42FBh
_CPOP1OP2		equ				41FBh

;---------Math--------------

_FPMULT         equ             548Ch
_FPSQUARE       equ             5488h 
_FPADD          equ             5478h 
_SQROOT         equ             54ACh 
_FPSUB          equ             5474h 
_FPDIV          equ             54A8h 
_MINUS1         equ             5470h



.org _asm_exec_ram


	call _clrLCD
	call _homeUp
	call _clrWindow
	call _runindicoff
	ld a,0
	ld (nozero),a

Input:
	ld bc,$0004
	ld (_curRow),bc
	ld de,_ioprompt
	ld hl,PergA
	call Prompt
	call _OP1TOOP4		;OP4 = A
	ld de,_ioprompt
	ld hl,PergB
	call Prompt
	call _OP1TOOP5		;OP5 = B
	ld de,_ioprompt
	ld hl,PergC
	call Prompt
	call _OP1TOOP6		;OP6 = C
	call _ZEROOP1
	call _ZEROOP2
	call _ZEROOP3

Calculo:
	call _OP4TOOP1		;OP1 = A
	call _OP6TOOP2		;OP2 = C
	call _FPMULT		;A*C
	call _OP2SET4		;OP2 = 4
	call _FPMULT		;OP1 = 4*A*C
	call _push_OP1		;FPS = 4*A*C
	call _OP5TOOP1		;OP1 = B
	call _FPSQUARE		;OP1 = B^2
	call _pop_OP2		;OP2 = 4*A*C
	call _FPSUB			;OP1 =B^2-4AC
	call _push_OP1		;FPS = (B^2-4*A*C)
	call _OP1TOOP2		;OP2= B^2-4AC
	call _ZEROOP1		;OP1= 0
	call _CPOP1OP2		;OP1-OP2
	jp m,Exit			;If (OP1-OP2) < 0, Goto Exit
	call _OP2TOOP1		;OP2 = B^2-4*A*C
	call _SQROOT		;OP1 = (sqrt)(B^2-4AC)
	call _push_OP1		;FPS = (B^2-4*A*C);((sqrt)(B^2-4*A*C))
	call _OP1SET0		;OP1 = 0
	call _MINUS1		;OP1 = -1
	call _OP5TOOP2		;OP2 = B
	call _FPMULT		;OP1 = -B
	call _push_OP1		;FPS = (B^2-4*A*C);((sqrt)(B^2-4*A*C));-B
	
_Minus:
	call _pop_OP1		;OP1 = -B
	call _pop_OP2		;OP3 = (sqrt)(B^2-4*A*C)
	call _push_OP2		;FPS = (B^2-4*A*C);((sqrt)(B^2-4*A*C))
	call _push_OP1		;FPS = (B^2-4*A*C);((sqrt)(B^2-4*A*C));-B
	call _FPSUB			;OP1 = -B - (sqrt)(B^2-4*A*C)
	call _push_OP1		;fps = (B^2-4*A*C);((sqrt)(B^2-4*A*C));-B;-B -
(sqrt)(B^2-4*A*C)
	call _OP1SET2		;OP1 = 2
	call _OP4TOOP2		;OP2 = A
	call _FPMULT		;OP1 = 2*A
	call _OP1TOOP2		;OP2 = 2*A
	call _pop_OP1		;OP1 = -B -(srqt)(B^2 -4*A*C)
	call _FPDIV			;OP1 = (-B -(sqrt)(B^2 -4*A*C))/(2*A)
	call _push_OP1		;FPS = (B^2-4*A*C);((sqrt)(B^2-4*A*C));-B;Minus result

_Plus:
	call _pop_OP3		;OP3 = Minus result
	call _pop_OP2		;OP2 = -B
	call _pop_OP1		;OP1 = (sqrt)(B^2-4*A*C)
	call _FPADD			;OP1 = -B + (sqrt)(B^2-4*A*C)
	call _push_OP3  	;FPS = (B^2-4*A*C);Minus result	
	call _push_OP1		;FPS = (B^2-4*A*C);Minus result; -B + (sqrt)(B^2-4*A*C)
	call _OP1SET2		;OP1 = 2
	call _OP4TOOP2		;OP2 = A
	call _FPMULT		;OP1 = 2*A
	call _OP1TOOP2		;OP2 = 2*A
	call _pop_OP1		;OP1 = -B +(srqt)(B^2 -4*A*C)
	call _FPDIV			;OP1 = (-B +(sqrt)(B^2 -4*A*C))/(2*A)
	call _OP1TOOP2		;OP2 = Plus result
	call _pop_OP1		;OP1 = Minus result

Ordering:
	call _push_OP6		;FPS = (B^2-4*A*C);C
	call _push_OP5		;FPS = (B^2-4*A*C);C;B
	call _push_OP4		;FPS = (B^2-4*A*C);C;B;A
	call _push_OP2		;FPS = (B^2-4*A*C);C;B;A;Plus Result
	call _push_OP1		;FPS = (B^2-4*A*C);C;B;A;Plus Result;Minus Result
	call _CPOP1OP2		;OP1-OP2
	jp p,Change
	jr Displaying

Change:
	call _push_OP1
	call _OP2TOOP1
	call _pop_OP2
	jr Displaying

Exit:
	ld a,1
	ld (nozero),a

Displaying:
	call _clrLCD
	call _clrWindow
	ld hl,$0000
	ld (_curRow),hl
	ld hl,zeros
	call _puts
	ld a,(nozero)
	ld b,a
	ld a,0
	cp b
	jp nz,Wozeros
	ld hl,$0001
	ld (_curRow),hl
	call _dispOP1
	call _push_OP1
	call _OP2TOOP1
	call _pop_OP2
	call _dispOP1
	jr Vertice

Wozeros:
	ld hl,$0801
	ld (_curRow),hl
	ld hl,no_zero
	call _puts

Vertice:
	ld hl,$0003
	ld (_curRow),hl
	ld hl,vertice
	call _puts
	call _pop_OP1		;FPS = (B^2-4*A*C);C;B;A;Plus Result
	call _pop_OP1		;FPS = (B^2-4*A*C);C;B;A
	call _pop_OP4		;FPS = (B^2-4*A*C);C;B
	call _pop_OP5		;FPS = (B^2-4*A*C);C
	call _pop_OP6		;FPS = (B^2-4*A*C)
	call _OP1SET0		;OP1 = 0
	call _MINUS1		;OP1 = -1
	call _OP5TOOP2		;OP2 = B
	call _FPMULT		;OP1 = -B
	call _push_OP1		;FPS = (B^2-4*A*C);-B
	call _OP1SET2		;OP1 = 2
	call _OP4TOOP2		;OP2 = A
	call _FPMULT		;OP1 = 2*A
	call _OP1TOOP2		;OP2 = 2*A
	call _pop_OP1		;OP1 = -B
	call _FPDIV			;OP1 = -B/(2*A)
	ld hl,$0004
	ld (_curRow),hl
	call _push_OP6
	call _push_OP5
	call _push_OP4		;FPS = (B^2-4*A*C);C;B;A
	call _dispOP1
	call _pop_OP4
	call _pop_OP5
	call _pop_OP6
	call _OP1SET0
	call _MINUS1		;OP1 = -1
	call _pop_OP2		;OP2 = (B^2-4*A*C)
	call _FPMULT		;OP1 = -(B^2-4*A*C)
	call _push_OP1
	call _OP1SET4
	call _OP4TOOP2
	call _FPMULT		;OP1 = 4*A
	call _OP1TOOP2
	call _pop_OP1
	call _FPDIV
	call _push_OP6
	call _push_OP5
	call _push_OP4		;FPS = C;B;A
	ld hl,$0005
	ld (_curRow),hl
	call _dispOP1
	ret

Prompt:
	call _mov10b		;move to prompt buffer, 10 bytes
	ld a,0Dh
	ld (_asap_ind),a
	call _exec_pg3
	ret

no_zero:
	.db "nao tem zeros",0
nozero:
	.db 0
PergA:
	.db "A=?",0
PergB:
	.db "B=?",0
PergC
	.db "C=?",0
zeros:
	.db "Zero(s)",0
vertice:
	.db "Vertice",0



Follow-Ups: