; **** quadr.asm **** ; ; integer quadratic (integer a,b,c,x) ; ; this subroutine evaluates the quadratic ; f(x) = ax^2 + bx + c ; the coefficients and x are single byte values. ; MUL is used to calculate ax^2, and since MUL takes ; A and B and replaces them with D=A*B, x cannot ; exceed 15 because 16*16=256, requiring 9 bits ; ; paramaters: ; a,b,c: quadradic coefficients (1 byte) ; (limited to 0<=c<=255) ; x : variable value (1 byte) ; (limited to 0<=x<=15) ; return values: ; f(x): result of expressing (two bytes) ; ; stack offsets ; (note: these are determined after we know how many ; local variables we need). the stack offsets are ; in decimal, so preceed them with "!". q_coefa equ !15 q_coefb equ !14 q_coefc equ !13 q_x equ !12 q_fx equ !10 q_tmp equ !0 quadratic: ; start by preserving registers, make space for the ; local variable (using pshx) and establish Y as the ; frame pointer psha pshb pshx pshy pshx tsy ; begin evaluation, starting with c. accumulate the ; value of f(x) in q_tmp. clra so that the top byte ; of D is empty. ldab q_coefc,y clra std q_tmp,y ; calculate bx and add it to q_tmp ldaa q_coefb,y ldab q_x,y mul addd q_tmp,y std q_tmp,y ; calculate ax^2 and add it to q_tmp. this time, ; store the result in the return slot ldaa q_x,y tab mul ; now D has x^2 (in low byte only ; b/c x<=15) ldaa q_coefa,y mul addd q_tmp,y std q_fx,y ; now clean up and return. the first PULX removes ; the local variable pulx puly pulx pulb pula rts