




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































-^ Advanced Assembly Programming–^ Program = data structures + algorithm•^ Data structures to be discussed–^ Stacks: a first-in-last-out data structure–^ Arrays: a set of elements of the same type–^ Strings: a sequence of characters terminated by a special character–^ Stack Frames•^ Examples–^ Finding a value in an unsorted array–^ Finding a value in a sorted arrayFinding a value in a sorted array–^ Binary to BCD to ASCII outputs and inputs–^ ASCII word matching, insertion–^ Subroutines ECE 2510^
-^ A simple means to store and retrieve register values temporarily–^ Notice that words inc/dec stack by 2•^ The SP can also be used in most instances where X and Y are used–^ Auto-inc/dec addressing modes
Mnemonic^
Function^
Operation
PSHA^ Push A^ (SP) – 1^ ⇒^ SP; (A)
⇒^ M(SP)
PSHB^ Push B^ (SP) – 1^ ⇒^ SP; (B)
⇒^ M(SP)
PSHC^ Push CCR^ (SP) – 1^ ⇒^ SP; (A)
⇒^ M(SP)
PSHD^ Push D^ (SP) – 2^ ⇒^ SP; (A : B)
⇒^ M(SP) : M(SP+1)
PSHX^ Push X^ (SP) – 2^ ⇒^ SP; (X)
⇒^ M(SP) : M(SP+1)
PSHY^ Push Y^ (SP) – 2^ ⇒^ SP; (Y)
⇒^ M(SP) : M(SP+1)
PULA^ Pull A^ (M(SP))^ ⇒^ A; (SP) + 1
⇒^ SP
PULB^ Pull B^ (M(SP))^ ⇒^ B; (SP) + 1
⇒^ SP
PULC^ Pull CCR^ (M(SP))^ ⇒^ CCR; (SP) + 1
⇒^ SP
ECE 2510^
PULD^ Pull D^ (M(SP) : M(SP+1))
⇒^ A : B; (SP) + 2
⇒^ SP
PULX^ Pull X^ (M(SP) : M(SP+1))
⇒^ X; (SP) + 2^ ⇒
SP
PULY^ Pull Y^ (M(SP) : M(SP+1))
⇒^ Y; (SP) + 2^ ⇒
SP
-^ RAM area for HCS
Æ^ $0800 - $3FFF
-^ Data Memory
Æ^ $0800 - $
-^ Program Memory
Æ^ $122A - $3BFF
-^ Stack Pointer
Æ^ $3C
-^ Stack Pointer
Æ^ $3C
-^ D-Bug12 RAM
Æ
$3C00-$3FFF
ECE 2510^ Freescale Semiconductor, Inc., 9S12DP512DGV1/D Device GuideV01.25, Original Release Date: 27 Nov , Revised: July 5, 2005.
-^ Example 4.
-^ Write a program to find out if the array vec_x contains a value ‘key’.–^ The array has 16-bit elements and is not sorted.• Since the array is not sorted, we need to compare the key with everyl^
il i i^ f^
d
array element until it is found.• Searching an array is a common operation for microcontrollerapplications–^ If the arrays is not sorted, the only way to find out whether a value is inthe array is by performing the sequential search–^ Sequential search algorithm compares the value to be searched with everyelement in the array• If an array needs to be searched frequently then sequential search isi^ ffi i^ t ECE 2510^ very in-efficient–^ A better approach is to sort the array and perform a binary searchalgorithm
; array count
notfound^
notfound^
key^
; define the searching key
. org^
vec_x:^ .word^
.word^
. org^
result:^ .blkw^
; reserve a word for result .text_main::^
; starting point of the program ldy^
; set up loop count y^
;^ p^ p ldd^ #notfound std^ result^ ; initialize the search result ldd^ #key ldx^ #vec_x^ ; place the starting address of vec_x in X loop:^ cpd^
; compare the key with array element & move pointer loop:^ cpd^
; compare the key with array element & move pointer beq^ found dbne^ Y,loop^ ; have we gone through the whole array? bra^ done found:^ dex^
; need to restore the value of X to point to the
ECE 2510^
dex^
; matched element stx^ result done:^ swi
st^ and last array index values?
-^ No – your done and have no match– Two: Check to see if max<min•^ If true – you are done and have no match– Third: Compare the key to the middle index (max-min)/2•^ Equal to – you are done and have a matchq^
-^ Greater than – set max to current index -1 and goto step 2•^ Less than – set min to current index +1 and goto step 2 ECE 2510^
n^
; array count key^
; key to be searched .org^
arr:^ .byte^
.byte^
.byte^
arrend:arrend:.org^
max^ .blkb^
; maximum index value for comparison min^ .blkb^
; minimum index value for comparison blkb^1
th^
f^ d^ i
mean^ .blkb^
; the average of max and min result^ .blkb^
; search result .text_main::_ clrastaa^ min^ ; initialize min to 0 staa^ result^ ; initialize result to 0 ldaa^ #n- staa^ max^ ; initialize max to n 1
ECE 2510^
staa^ max^ ;^ initialize max to n- ldx^ #arr^ ; use X as the pointer to the array
loop:^ ldab^ min cmpb^ max lbhi^ notfound^ ; branch if min>max addb^ max^ ; compute mean, unsigned add lsrb^
; divide by two stab^ mean^ ; save mean ldaa^ b,x^ ; get a copy of the element arr[mean] ldaa^ b,x^ ; get a copy of the element arr[mean] cmpa^ #key beq^ found^ ; if equal - found bhi^ search_lo^ ; if arr[mean] >key – in search_hi:^ ldaa^ mean iincastaa^ min^ ; place mean+1 in min to continue bra^ loop search_lo:^ ldaa^ mean decastaa^ max bra^ loop found:^ ldaa^
staa^ result notfound:^ swi ECE 2510^ notfound:^ swi
ECE 2510^
Signed binary numbers to ASCII strings
ConversionConversion
-^ Step1:p –^ Check if the number is negative using bmi instruction–^ If negative first step is to add $2D (ASCII equivalent of -) to thestart of the ASCII stringstart of the ASCII string•^ Step2:–^ Compute the 2’s complement of the number•^ Step3:–^ Divide the binary number by 10 ($0A) repeatedly until the quotientis zerois zero–^ For each remainder the number $30 needs to be added to convertthe number into a recognizable ASCII string ECE 2510^