Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning Outcome #1 Architecture and Programming Model

Similar presentations


Presentation on theme: "Learning Outcome #1 Architecture and Programming Model"— Presentation transcript:

1 Learning Outcome #1 Architecture and Programming Model
“An ability to program a microcontroller to perform various tasks” Architecture and Programming Model Instruction Set Overview Assembly Control Structures Control Structure Applications Table Lookup Parameter Passing Macros and Structured Programming How?

2 Objective Why? “Assembly Table Lookup”
1 Pre-compute values, store in table. Lookup result when you need it. Can create more efficient code 2 Can use tables to jump program to different code locations

3 Lookup and interpolate
Table Lookup Uses Jump Tables Non-Linear index // like a table of function pointers interrupt_handler* handlers[N]; // there are N potential interrupts, when interrupt 0 happens, // call code at first entry, etc… // Also useful for switch statements code* switch_cases[N]; // Like linear-index, except table has “holes” // Example: use BCD index to lookup days in month unsigned int days_in_month[] = {DCh, 31h, 28h, …}; Linear index Lookup and interpolate // Example: have some function. Don’t want to do // math for every value, instead pre-compute every value // store it in the table, lookup value at runtime // Example, y = x^3 + 10 unsigned int y_value[N] = {10, 13, 18, …}; // Table is incomplete, but has linear // characteristics between each data point. // Effectively, a piece-wise linear function // “Special” 9S12 instruction (TBL) does this // interpolation for you // ex: | _ _ // | / // |/_ _ _ // unsigned int y_val[] = {0 /*x=0*/, 2/*x=4*/, 2/*x=8*/, …};

4 Jump Tables A jump table is a special form of lookup table that contains addresses of subroutines Note: Here, the table entries are “double-byte” (16-bits) in length, since they represent addresses in memory Applications: select (“vector to”) a specific interrupt service routine under hardware control select a function/subroutine to execute based on an input stream that has been parsed by a command interpreter

5 Jump Tables +0 +2 +4 (N-1)*2 puts desired return address on stack

6 Jump Tables note use of indirect addressing mode
Less efficient, more straight-forward way: jsr [d,x] rts

7 Table Lookup - Linear Index

8 Table Lookup - Non-linear Index
A non-linear index simply means that the index only takes on a subset of all possible values, i.e., there are don’t cares in the table Example: Packed BCD number used as a table lookup index, say for a calendar program (note that there is no “month 00”, nor are there months “0A” through “0F”) Question: In such an application, is it more efficient to use the “raw” BCD value as a table lookup index (thus “wasting” space in the table), or convert the BCD value to binary before using it as a lookup index (thus “saving” space in the table)? A “pair-o-docs”: Sometimes, in order to save space, you have to waste it!

9 Table Lookup - Non-linear Index
wasted space

10 Table Lookup - Non-linear Index
Note: Same code as “normal” case

11 Lookup and Interpolate (TBL)
The “TBL” instruction can be used to perform a linear interpolation on values that fall between a pair of data entries stored in memory Example: Estimation of room temperature based on data read from an analog input channel interfaced to a silicon diode circuit Operation performed: (A)  (addr) + [ (B) X { (addr+1) – (addr) } ] where indexed addressing mode is used and (B) is interpreted as a binary fraction* *of form (unsigned)

12 Lookup and Interpolate (TBL)
Use of TBL instruction: set up index register to point to table entry “X1” (the table entry closest to, but less than or equal to, the desired lookup value) “X2” is the table entry that follows “X1”, and “XL” is the desired lookup point (between “X1” and “X2”) along the X-axis calculate (XL-X1)  (X2-X1) and place the resulting binary fraction in the B register (typically requires use of FDIV instruction) execute the TBL instruction; the result placed in the A register will be the interpolated result along the Y-axis: (A)  Y1 + [ (B) X (Y2 – Y1) ]

13 Lookup and Interpolate (TBL)
TBL in action... Y Y2 YL Y1 X X1 XL X2

14 The (base 10) value that gets stored at location lookup is: A: 8 B: 10
E: none of the above org $800 ldx #table ldab #$40 tbl 0,x staa lookup ldab #$C0 tbl 1,x staa lookup+1 halt bra halt table fcb 8 fcb 16 fcb 24 lookup rmb 2 B = 10

15 The (base 10) value that gets stored at location lookup+1 is: A: 8
E: none of the above org $800 ldx #table ldab #$40 tbl 0,x staa lookup ldab #$C0 tbl 1,x staa lookup+1 halt bra halt table fcb 8 fcb 16 fcb 24 lookup rmb 2 D = 22

16 Example Application Write an interactive "stupid quote" generator that prompts the user for a single character identifier (here, "a" through "f") and prints the corresponding quote on the emulated terminal screen. If the character entered is "out of range", an error message should be printed. If the character q is entered, the program should terminate. The user interface should function as follows (user input is in bold): Welcome to the Stupid Quote Generator What do you want to say today (a-f)? a Your own favorite quote #1 What do you want to say today (a-f)? d Your own favorite quote #4 What do you want to say today (a-f)? g Message index is out of range What do you want to say today (a-f)? 2 What do you want to say today (a-f)? q Nice talking to you...

17 Example Application This program consists of three modular components:
a main program that implements the user interface. a pmsgx subroutine that prints the string pointed to by X at entry and terminates when an ASCII null character is encountered. a lookup subroutine that is passed the message index (in the range of 0 to N-1) in the A register and returns the starting address of the desired message string in the X register. In addition, two I/O library routines are provided: inchar — inputs an ASCII character from the terminal keyboard and returns it in the A register outchar — prints the ASCII character passed to it in the A register on the terminal screen

18 ; ; Stupid Quote Generator main ldx #welcome jsr pmsgx ; print welcome message mprompt ldx #prompt jsr pmsgx ; print prompt jsr inchar jsr outchar ; input and echo response cmpa #'q' beq mexit ; exit if 'q' entered suba #'a' ; subtract off bias of ASCII 'a' ; range should now be 0 to 5 (for a-f) bmi nok ; *not* OK if negative cmpa #N ; check to see if within range blt rok nok ldx #nogood jsr pmsgx ; if not within range, print error message bra mprompt ; and try again

19 rok jsr lookup ; if within range, perform message lookup jsr pmsgx ; and print desired string bra mprompt ; go back for more mexit ldx #bye jsr pmsgx ; program termination sloop bra sloop ; sit in infinite loop/wait for reset ; ; Message printing subroutine ; Prints message string pointed to by X register pmsgx ldaa 1,x+ cmpa #NULL beq pexit jsr outchar bra pmsgx pexit rts

20 ; ; Lookup subroutine ; Index of message (range: 0 to N-1) passed in A register ; Pointer to desired message string returned in X register lookup asla ldx #qtable ldx a,x rts qtable fdb quote1 fdb quote2 fdb quote3 fdb quote4 fdb quote5 fdb quote6

21 ; Message declarations
N equ 6 ; number of valid messages NULL equ 0 ; ASCII null character (string termination) RET equ $d ; ASCII return character LF equ $a ; ASCII line feed character welcome fcb RET,LF fcc "Welcome to the Stupid Quote Generator" fcb RET,LF,NULL prompt fcc "What do you want to say today (a-f)? " fcb NULL nogood fcb RET,LF fcc "Message index is out of range" bye fcb RET,LF fcc "Nice talking to you..."

22 quote1 fcb RET,LF fcc "Your own favorite quote #1" fcb RET,LF,NULL quote2 fcb RET,LF fcc "Your own favorite quote #2" quote3 fcb RET,LF fcc "Your own favorite quote #3" quote4 fcb RET,LF fcc "Your own favorite quote #4" quote5 fcb RET,LF fcc "Your own favorite quote #5" quote6 fcb RET,LF fcc "Your own favorite quote #6"


Download ppt "Learning Outcome #1 Architecture and Programming Model"

Similar presentations


Ads by Google