Presentation is loading. Please wait.

Presentation is loading. Please wait.

5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing.

Similar presentations


Presentation on theme: "5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing."— Presentation transcript:

1 5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing

2 5-2 Ramesh Yerraballi Conditionals > < ≥ ≤ conditional branch instructions must follow a subtract compare or test instruction, such as  suba subb sbca sbcb subd cba cmpa cmpb cpd cpx cpy tsta tstb tst Equality/Inequality check

3 5-3 Ramesh Yerraballi Signed Conditional bge target ; Branch if signed greater than or equal to (), ;if (N^V)=0, or (~N.V+N.~V)=0 bgt target ; Branch if signed greater than (>), ;if (Z+N^V)=0, or (Z+~N.V+N.~V)=0 ble target ; Branch if signed less than or equal to (), ;if (Z+N^V)=1, or (Z+~N.V+N.~V)=1 blt target ; Branch if signed less than (<), ;if (N^V)=1, or (~N.V+N.~V)=1

4 5-4 Ramesh Yerraballi … Signed Conditional , checks

5 5-5 Ramesh Yerraballi Unsigned Conditional bhs target ; Branch if unsigned greater than or equal to (), ;if C=0, same as bcc bhi target ; Branch if unsigned greater than (>), ;if C+Z=0 bls target ; Branch if unsigned less than or equal to (), ;if C+Z=1 blo target ; Branch if unsigned less than (<), ;if C=1,same as bcs

6 5-6 Ramesh Yerraballi … Unsigned Conditional , checks

7 5-7 Ramesh Yerraballi Problem Solving When we solve problems on the computer, we need to answer these questions:  What does being in a state mean? List state parameters  What is the starting state of the system?Define the initial state  What information do we need to collect?List the input data  What information do we need to generate?List the output data  How do we move from one state to another? Actions we could do  What is the desired ending state?Define the ultimate goal

8 5-8 Ramesh Yerraballi Successive Refinement  Start with a task and decompose the task into a set of simpler subtasks  Subtasks are decomposed into even simpler sub-subtasks.  Each subtask is simpler than the task itself.  Make design decisions  Subtask is so simple, it can be converted to software code.

9 5-9 Ramesh Yerraballi Decomposition “do A then do B” → sequential “do A and B in either order” → sequential “if A, then do B” → conditional “for each A, do B” → iterative “do A until B” → iterative “repeat A over & over forever” → iterative (condition always true) ‏ “on external event do B” → interrupt “every t msec do B” → interrupt

10 5-10 Ramesh Yerraballi Successive Refinement: Example

11 5-11 Ramesh Yerraballi if-then-else Two alternative implementations

12 5-12 Ramesh Yerraballi while loop

13 5-13 Ramesh Yerraballi for loop

14 5-14 Ramesh Yerraballi Modular Design  Goal  Clarity  Create a complex system from simple parts  Definition of modularity  Maximize number of modules  Minimize bandwidth between them  Entry point (where to start)  The label of the first instruction of the subroutine  Exit point (where to end)  The rts instruction  Good practice, one rts as the last line

15 5-15 Ramesh Yerraballi Modular Design  Public (shared, called by other modules)  Add underline in the name, module name before  Private (not shared, called only within this module)  No underline in the name  Helper functions  Coupling (amount of interaction between modules)  Data passed from one to another (bandwidth)  Synchronization between modules

16 5-16 Ramesh Yerraballi Subroutines and the Stack classical definition of the stack  push saves data on the top of the stack,  pull removes data from the top of the stack  stack implements last in first out (LIFO) behavior  stack pointer (SP) points to top element many uses of the stack  temporary calculations  subroutine (function) return addresses  subroutine (function) parameters  local variables  psha push Register A on the stack  pshb push Register B on the stack  pshx push Register X on the stack  pshy push Register Y on the stack  desS=S-1(reserve space)  pula pull from stack into A  pulb pull from stack into B  pulx pull from stack into X  puly pull from stack into Y  insS=S+1 (discard top of stack)

17 5-17 Ramesh Yerraballi Registers to pass parameters Subroutine 3) Sees the inputs in registers 4) Performs the action of the subroutine 5) Places the outputs in registers High level program 1)Sets Registers to contain inputs 2)Calls subroutine 6) Registers contain outputs

18 5-18 Ramesh Yerraballi Introduction to Pointers Pointers are addresses pointing to objects. The objects may be data, functions, or other pointers: If register X or Y contains an address we say it points into memory

19 5-19 Ramesh Yerraballi Use of Index registers

20 5-20 Ramesh Yerraballi Functional Debugging Pt = Buf;ldx #Buf stx Pt Pt will point into the buffer. Pt must be initialized to point to the beginning, before the debugging begins. #define SIZE 20 unsigned char Buf[SIZE]; unsigned char *Pt; SIZE equ 20 Buf rmb SIZE Pt rmb 2 Instrumentation: dump into array without filtering Assume happy is strategic 8-bit variable.

21 5-21 Ramesh Yerraballi … Functional Debugging Next, you add jsr Save statements at strategic places within the system. Use the debugger to display the results after program is done void Save(void){ if(Pt < &Buf[SIZE]){ (*Pt) = happy; Pt++; } Save pshb pshx ;save ldx Pt ;X=>Buf cpx #Buf+SIZE bhs done ;skip if full ldab happy stab 0,X ;save happy inx ;next address stx Pt done pulx pulb rts The debugging instrument saves the strategic variable into the Buffer.


Download ppt "5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing."

Similar presentations


Ads by Google