Docsity

Converting Signed 16-bit Binary Numbers to ASCII Strings - Prof. Bradley J. Bazuin, Study notes of Microcomputers

Partial preview of the text

Download Converting Signed 16-bit Binary Numbers to ASCII Strings - Prof. Bradley J. Bazuin and more Study notes Microcomputers in PDF only on Docsity!

ECE 4510/

Microcontroller ApplicationsMicrocontroller Applications

Ch^

t^4

Chapter 4^ Dr. Bradley J. BazuinAssociate ProfessorDepartment of Electrical and Computer EngineeringCollege of Engineering and Applied SciencesCollege of Engineering and Applied Sciences

Chapter 4p

-^ 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^

Subroutines

Simple Stack Operationsp

p

-^ 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

g Stack Operation Instructions

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

HC12 Memory Allocation

y

-^ 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.

Indexed Data Structures

•^ Vectors and matrices are indexed data structures.•^ The first element of a vector is associated with the index 0in order to facilitate the address calculation.•^ Assemblers directives

.byte or .blkb

are used to define

arrays of 8-bit elements.• Assemblers directives

word or blkw

are used to define

•^ Assemblers directives

.word or .blkw

are^ used to define

arrays of 16-bit elements. ECE 2510^

Example 4.

p

-^ Example 4.

i^

fi d^ if h

i^ l^

‘k^ ’

-^ 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

Example 4.2 Code

p

N^

=^

; 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^

#N^

; 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^

2 X+^

; compare the key with array element & move pointer loop:^ cpd^

2,X+^

; 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

Searching a Sorted Array

g^

y

•^ If an array needs to be searched frequently then sort the

y^

q^ y

array first and perform a different search algorithm.• Binary Search–^ First: Is the key between the 1

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^

y

-^ 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^

Code (1)

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

Code (2)

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

Binary to BCD ASCII Strings

y^

g

•^ Numerical input and output is typically performed as an

p^

p^ yp

y p

ASCII BCD representation–^ A number in the computer is represented as a binary number.A number must be converted to ASCII code before output so that it–^ A number must be converted to ASCII code before output so that itcan be understood.–^ The ASCII equivalent for decimal 0 – 9 is 30 – 39.• To convert a binary into an ASCII string, we divide thebinary number by 10 repeatedly until the quotient is zero.y^

y^ p

y^

q

For each remainder, the number $30 is added. ECE 2510^

Example 4.4: ASCII BCD Output

p^

p

•^ Write a program to convert the unsigned 8-bit binary

p^ g^

g^

y

number in accumulator A into ASCII BCD digitsterminated by a NULL character. Each digit is representedin ASCII codein ASCII code.• There are from 2 to 4 bytes needed for the resulte e a e

o^

to^ bytes

eeded o t e esu t

(max >=100 plus NULL , min 0 plus NULL )• Work from LSdigit to MSdigit–^ Push results on the stack so they can be “output” in the properorder, MS_BCD first ECE 2510^

Example 4.4: Code (2)

p^

normal:^

movb^

#0,1,-sp^

; push a NULL into the stack

clra

loop:^

ldx^

idivaddb^

#$^

; convert to ASCII code

pshb^

; push into stack

p^

p

cpx^

beq^

reverse^

; if result of division is zero, reverse

xgdx^

; put quotient back to B

bra^

loopp

reverse:^

movb^

0,sp,1,y+

tst^

0,sp

beq^

done^

; when the null is found, done

insbra^

reverse

done:^

ins

ECE 2510^

swi

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^