Skip to content

Instantly share code, notes, and snippets.

@billylindeman
Created September 21, 2011 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billylindeman/1232297 to your computer and use it in GitHub Desktop.
Save billylindeman/1232297 to your computer and use it in GitHub Desktop.
Quadratic Solver in HC12 Assembly
;*******************************************************************
;* author: Billy Lindeman & Christine Martin *
;* date: 8-23-2011 *
;* purpose: The purpose of this lab is to create a subroutine that *
;* calculates the real roots of a second order equation using the *
;* quadratic formula *
;* inputs: a,b,c as 8-bit signed integers *
;* outputs: both real roots of the quadratic equation *
;*******************************************************************
; Include derivative-specific definitions
INCLUDE 'derivative.inc'
; export symbols
XDEF Entry, _Startup, main
; we use export 'Entry' as symbol. This allows us to
; reference 'Entry' either in the linker .prm file
; or from C/C++ later on
XREF __SEG_END_SSTACK ; symbol defined by the linker for the end of the stack
; variable/data section
MY_EXTENDED_RAM: SECTION
; Insert internal variables here
ORG $3900
error_flag: dc.b 1
root1: dc.w 1
root2: dc.w 1
in_a: dc.b 1
in_b: dc.b 1
in_c: dc.b 1
; code section - don't mess with anything from here...
MyCode: SECTION
main:
_Startup:
Entry:
LDS #$3600
LDAA in_a
PSHA
LDAA in_b
PSHA
LDAA in_c
PSHA
JSR quadratic
LEAS 3,SP
LDAA #$01
PSHA
LDAA #$FC
PSHA
LDAA #$EB
PSHA
JSR quadratic
JMP endmain
quaderror: ;stack (a),(b),(c),[return]
MOVB #$FF,error_flag
RTS
det_lt_zero: ;stack (a),(b),(c),[return],[determinant],[-b],[2a]
LEAS 8,SP ;stack (a),(b),(c),[return]
BRA quaderror
quadratic:
LDAA 4,SP ;load a -> a
CMPA #$00
BEQ quaderror
LDD #0 ;clear d
LDY #0 ;clear y
LDAB 2,SP ;load c->b
SEX B,Y ;signextension in y
LDAB 4,SP ;load a->b
SEX B,D ;signextension in d
EMULS
LDY #$0004 ;y=4
EMULS
PSHD ;stack a,b,c,(4*a*c)(2b)
LDAB 5,SP ;load b->b
SEX B,Y ;sign extend b into y
SEX B,D ;sign extend b into d
EMULS ;multiply d*y ->d
PSHD ;stack a,b,c,[return](2b),(4*a*c)(2b),(b*b)(2b)
LDD 0,SP ;load b*b into D
SUBD 2,SP ;subtract 4*a*c from b*b
LEAS 4,SP
PSHD ;stack (a),(b),(c),[return],[determinant]
LDD #0
LDAB 5,SP ;load b->d
SEX B,D ;sign extend
LDY #$FFFF ;load -1 -> y
EMULS ;b*-1->d
PSHD ;stack (a),(b),(c),[return],[determinant],[-b]
LDD #0
LDAB 8,SP ;load a->b
SEX B,D
LDY #$0002 ;load 2->y
EMULS ;2*a->d
PSHD ;stack (a),(b),(c),[return],[determinant],[-b],[2a]
LDD 4,SP
CPD #$0000
BLT det_lt_zero ;if d<0, error out
CPD #$0000 ;
BEQ det_is_zero ;if d = 0, one root
BRA det_gt_zero ;if d > 0, two roots
det_is_zero: ;stack (a),(b),(c),[return],[determinant],[-b],[2a]
LDD 2,SP
LDX 0,SP
IDIVS
MOVB #$00,error_flag
STD root1
LEAS 8,SP
RTS
det_gt_zero: ;stack (a),(b),(c),[return],[determinant],[-b],[2a]
LDD 4,SP
JSR sqrt
PSHD ;stack (a),(b),(c),[return],[determinant],[-b],[2a],[sqrt(det)]
LDD 4,SP
SUBD 0,SP
LDX 2,SP
IDIVS
STX root1
LDD 4,SP
ADDD 0,SP
LDX 2,SP
IDIVS
STX root2
LEAS 8,SP
RTS
sqrt PSHD
LDD #0
PSHD
sqrtloop CPD 2,SP
BHI donesqrt
ADDD 0,SP
INC 1,SP
ADDD 0,SP
BRA sqrtloop
donesqrt PULD
DECB
LEAS 2,SP
RTS
endmain: BRA endmain ; end of program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment