230 likes | 370 Views
ECE 265 – Lecture 10. PROGRAMMING STRUCTURES. Lecture Overview. Programming structures Translation of HLL structures to assembler HLL = Higher Level Language In line code Conditional code statements Loop control statements Control Structures Nested loops REF: Chapter 4.
E N D
ECE265 ECE 265 – Lecture 10 PROGRAMMING STRUCTURES
Lecture Overview • Programming structures • Translation of HLL structures to assembler • HLL = Higher Level Language • In line code • Conditional code statements • Loop control statements • Control Structures • Nested loops • REF: Chapter 4 ECE265
Translation to Assembler • Common HLL structures • Straight line code – code to compute results • Loop code – code to establish a repetitive loop based on a count or condition • Decision code – code to direct execution flow to take one path or the other. • Nested Loops ECE265
In line code • These are direct blocks of code in a flow chart square box • Consider the HLL equation y = a + b - c • Memory locations are • y $20 • a $21 • b $22 • c $23 • Coded into assembler • LDAA $21 • ADDA $22 • SUBA $23 • STAA $20 ECE265
Conditional Code Statements • Very few computational tasks involve simply a unconditional sequence of tasks. In an embedded system (the real world) there is always a variability in the inputs or the data to be acted on. • This gives rise to a couple of possible logical constructs • IF a given condition is true THEN take a given action • IF a given condition is true THEN take a given action . Otherwise (ELSE) take and alternative action ECE265
The IF THEN structure • Graphical form of the structure • HLL pseudocode • IF <expression> THEN • [process] • END • Generally the process is straight inline code ECE265
IF THEN ELSE structure • General Form of the structure • HLL pseudocode • IF <expression> THEN • [process for True] • ELSE • [process for FALSE] • END • Generally the processes are straight in-line code but this structure could be nested ECE265
Examples and translation • These HLL structures can be translated to assembler language • Example: compare the A and B accumulators. If A accum is less than B accum add 5 to A (BCD) and store in the variable result • STAB temp • IF A <LT> temp THEN • ADDA #5 • DAA • END • STAA result • Will come back to this after covering the remaining structures ECE265
Loop Structures • In programming there is typically the need to repeatedly perform an action until a condition is met. • These break down into two structures • REPEAT UNTIL structure • REPEAT note that action • [process] is performed at least once • UNTIL <expression> • DO WHILE structure • WHILE <expression> DO expression is • [process] evaluated before • END computation is done • (possible for no action) ECE265
The For construct • A very common task in programs is to perform an action n times. • Sometimes called a DO loop or a For loop. • In most HLL the increment can be positive of negative • FOR op1=op2 TO op3 [by op4] DO • [process] • END ECE265
Control Structures • Simple control structures are easy to translate • IS A = B or IS A>B • Consider the example from slide 8 • IF A <LT> temp THEN • How does this translate to assembler • First compare the contents of A with temp • CMPA temp • This sets the CC register bits • BGE end jump over action for structure • so you want the complementary • condition if you are jumping over action • (go back and look at the structure) ECE265
Full assembler for example • temp RMB 1 • result RMB 1 • STAB temp • CMPA temp • BGE end • ADDA #5 • DAA • end STAA result ECE265
Control Structures (2) • Simple control structures can be handled by one branch. Typically the branch condition is the complement of what is desired. The example showed this. • Complex test structures • Consider low < A < high • These are compound expressions that test for test multiple conditions • Consider A < 5 AND B = 5 ECE265
A coding • A possible coding for testing for • 3 < A < 12 • The data A is in the A accumulator • CMPA #3 compare A to 3 • BLE brout know A is > 3 • CMPA #12 compare A to 12 • BGE brout know A is >= 12 • A in range so perform actions • brout the is the first instruction after test & action ECE265
Compound test coding • Coding for A < 5 AND B = 5 • CMPA #5 • BGE outfail • CMPB #5 • BNE outfail • when you are here you know A<5 AND B=5 • outfail the label beyond the code for a TRUE • condition ECE265
An example problem • Code up a program to find the value of an unknown integer between 0 and 99 after verifying the number is in this range. • The algorithm: • If 100 <= unknown the unknown is out of range • Branch to END • Set the initial increment to 50 and val = 100 • Loop: Test val to unknown • If val> unknown subtract increment, divide increment by 2 and repeat loop • If val<unknown then add increment, divide increment by 2 and repeat loop • Repeat until val = unknown • Note: all divide increment by 2 round up • Val = guess ECE265
The pseudocode algorithm • Algorithm to find value of unknown between 0 and 99. The pseudocode • Set guess = 100 and incr = 50 • WHILE guess NEQ unknown REPEAT • IF guess > unknown THEN • guess = guess – incr • incr = ceiling(incr/2) • ELSE • guess = guess + incr • incr = ceiling(incr/2) • ENDIF • ENDWHILE why no check for =????? ECE265
The coding • The code • ORG $0010 • unknown FCB 47 set unknown value • guess RMB 1 location for guess var • incr RMB 1 location for increment var • ORG $C000 • LDAA #100 • STAA guess • LSRA divide by 2 • STAA incr • LDAB incr • LSRB ECE265
Coding the heart of the algorithm • tol CMPA unknown • BEQ done • BLT low • * guess is too high • SBA subtract increment • incadj LSRB incr = incr/2 • BCC ceilgd • INCB carry was 1 so make ceil • ceilgd BRA tol • low ABA add increment • BRA incadj • done STAA guess done ECE265
Board work of algorithm • An example of the program in action will be worked on the black board of the algorithm and coding. • Consider starting with unknown values of • 47 • 31 • 77 • 8 ECE265
Nested Loops • Decisions within decisions. • What is the error translating to flow chart? • Fig 4.9 has error in B=4 decision should be B=5 ECE265
Lecture summary • Program Design • General Overview of software • Program Design Methodology • Assembler Language in general • Assembler Directives for the 68HC11 ECE265
Assignment • Look at • Page 126/127 • Problem 1 • Problem 4 • Problem 8 • Problem 14 ECE265