Developing Software in Assembly Language
6812 Assembly Language Examples
By Jonathan W. Valvano
This article, which discusses assembly language programming,
accompanies the book Embedded Microcomputer Systems: Real Time Interfacing published by Brooks-Cole 1999. This document has four overall
parts
Overview
Syntax (fields, pseudo ops)
Local variables
Examples
Interpreter Examples
Decision tree
Vector table
Command table
Linked list
Binary tree
Other Examples
Arithmetic Examples on the 6812
Shift Examples on the 6812
Stack Usage and Interrupts on the 6812
FIFO Queue Examples on the 6812
Control Structure Examples on the 6812
It is better to use vector tables, command tables, linear linked list, or a tree-structured linked list instead of decision trees to build a command interpreter. The tables and linked lists will be easier to understand, quicker to debug, and simplier to modify. In the following examples the command interpreter operates on a single letter basis. All examples, except the vector table, can be modified to handle command strings, by replacing the simple beq with a string compare operation.
--------------------------------------------------------------------------------------
Decision Tree interpreter on the 6812
;*********** Decision Tree *****************
Exec jsr InChar ;Input a char in RegA
cmpa #'A'
beq ISA ;Check each
cmpa #'B'
beq ISB
cmpa #'C'
beq ISC
bra ERROR
ISA jsr ACCEPT ;Execute command
bra DONE
ISB jsr BYE ;Execute command
bra DONE
ISC jsr CANCEL ;Execute command
bra DONE
DONE bra Exec
--------------------------------------------------------------------------------------
Vector interpreter on the 6812
;************* Vector ********************
Exec jsr InChar ;Input a char in RegA
cmpa #'C'
bhi ERROR
ldx #VECTAB ;Vector table
suba #'A' ;Convert 'A' to 'C' to 0 to 2
bmi ERROR
lsla ;Calculate index
ldx A,X ;X points to the subroutine
jsr 0,X ;Execute command
bra Exec
VECTAB fdb ACCEPT ;Ptr to subroutines
fdb BYE
fdb CANCEL
-------------------------------------------------------------------------------------
Command Table interpreter on the 6812
;********** Command Table ********************
Letter equ 0 ;Index for command letter
SubAdr equ 1 ;Index for subroutine address
Exec jsr InChar ;Input a char in RegA
ldx #CMDTAB-3 ;Command table
TEST leax 3,x ;Next entry
tst Letter,X ;Done?
beq ERROR
cmpa Letter,X ;Is it this command?
bne TEST
ldx SubAdr,X ;X points to the subroutine
jsr 0,X ;Execute command
bra Exec
CMDTAB fcb 'A' ;Command letter
fdb ACCEPT ;Ptr to subroutine
fcb 'B'
fdb BYE
fcb 'C'
fdb CANCEL
fcb 0 ;No more commands
--------------------------------------------------------------------------------------
Linked List interpreter on the 6812
;********** Linked List Interpreter ********************
Exec jsr InChar ;Input a char in RegA
ldx #CMDA ;First entry Command linked list
TEST cmpa Letter,X ;Is it this command?
beq Found
ldx Next,X
cpx #null
bne TEST
bra ERROR
Found ldx SubAdr,X ;X points to the subroutine
jsr 0,X ;Execute command
bra Exec
null equ 0 ;Null pointer
Letter equ 0 ;Index for command letter
SubAdr equ 1 ;Index for subroutine address
Next equ 3 ;Index for next cmd entry
CMDA fcb 'A' ;Command letter
fdb ACCEPT ;Ptr to subroutine
fdb CMDB ;Ptr to next entry
CMDB fcb 'B'
fdb BYE
fdb CMDC ;Ptr to next entry
CMDC fcb 'C'
fdb CANCEL
fdb null ;No more commands
--------------------------------------------------------------------------------------
Binary Tree interpreter on the 6812
;********** Binary Tree Interpreter ******************
Exec jsr InChar ;Input a char in RegA
ldx Root ;First entry Command table
TEST cpx #null
beq ERROR ;Not found?
cmpa Letter,X ;Is it this command?
beq Found
bhi goRight
goLeft ldx Left,X ;RegA < Letter
bra TEST
goRight ldx Right,X ;RegA > Letter
bra TEST
Found ldx SubAddr,X ;X points to the subroutine
jsr 0,X ;Execute command
bra Exec
null equ 0 ;Null pointer
Letter equ 0 ;Index for command letter
SubAddr equ 1 ;Index for subroutine address
Left equ 3 ;Index for left next cmd entry
Right equ 5 ;Index for right next cmd entry
Root fdb CMDM
CMDM fcb 'M' ;Command letter
fdb MAKE ;Ptr to subroutine
fdb CMDH ;Ptr to left entry
fdb CMDV ;Ptr to right entry
CMDH fcb 'H' ;Command letter
fdb HIGH ;Ptr to subroutine
fdb CMDA ;Ptr to left entry
fdb null ;None to the right
CMDA fcb 'A' ;Command letter
fdb ACCEPT ;Ptr to subroutine
fdb null ;None to the left
fdb null ;None to the right
CMDV fcb 'V' ;Command letter
fdb VERIFY ;Ptr to subroutine
fdb null ;None to the left
fdb CMDZ ;Ptr to right entry
CMDZ fcb 'Z' ;Command letter
fdb ZAP ;Ptr to subroutine
fdb null ;None to the left
fdb null ;None to the right
This document has four overall parts
Overview
Syntax (fields, pseudo ops)
Local variables
Examples