 Download Download Presentation Reg Hahne

# Reg Hahne

Download Presentation ## Reg Hahne

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Reg Hahne Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro

2. Operands Operators Postfix Expressions ABC*+ Prefix Expressions +A*BC Infix Expressions A + B * C

3. Converting Infix Expressions Postfix String Assembly Language Code

4. Step 1 Creating a postfix string using a stack Let’s try a simple one: A + B * C

5. Rule Every operator gets pushed onto the operator stack but not until all operators with equal or greater precedence have been popped. Operands are appended to the postfix string immediately. Infix String Operator Stack Postfix String A B A B + A B + * * A + B * C A + A + + * C

6. Let’s try a more difficult one: A – B / C \$ D * E

7. Infix String Operator Stack Postfix String A B A B – / – / A B C – / \$ A B C D A B C – A B C D \$ / – * * A A – B / C \$ D * E A – A B – – / C \$ D – \$ / * E One for you to try: V + W * X \$ Y – Z

8. What about parentheses? (A + B) * (C – D) \$ E * F

9. Infix String Operator Stack Postfix String ( + A B + A B + A B + ( A B * ( + C A B * ( – + C A B * + D A B C + – D A B C + – \$ D A B C * + – D A B E C + – D A B E C \$ * * (A+ B) * (C– D) \$ E * F ( ( A ( + A A B + * ( * C – D – * \$ * E * \$ * F *

10. Step 2 Creating assembly language code using a stack

11. Concept and Terminology Accumulator: Where arithmetic takes place LDA A: Load the accumulator with number stored in location A ADD B: Add to the accumulator the number stored in location B STA T0: Store value from accumulator in T0

12. Rule When you meet an operator, pop two elements from the stack. The first element becomes the right operand; the other becomes the left. Postfix String Operand Stack ALC A B LDA B A A B C * + MUL C A B C STA T0 T0 A LDA A T1 ADD T0 STA T1

13. Postfix String Operand Stack ALC T0 C T1 T0 A LDA A AB+CD – E\$*F* A B ADD B T0 STA T0 T0 C LDA C D SUB D T1 T0 STA T1 LDA T1 E T2 T0 EXP E T3 STA T2 T3 F LDA T0 T4 MUL T2 STA T3 One for you to try: A B + C D E – / F + * G – LDA T3 MUL F STA T4

14. Pseudocode Infix Expression Postfix String Infix [i] Action '(' 'A'…'Z' ')' Push onto operator stack postfix string += infix [i] operator.pop (temp) while (temp != '('){ postfix string += temp operator.pop (temp)} '+', '-', '*', '/' • while (!done && !operator.isEmpty ()) • if (precedence (operator.top (), infix [i]) • postfix string += operator.pop (temp) • else • done = true

15. Pseudocode Postfix Expression ALC Postfix [i] Action 'A'...'Z' Push onto operand stack '+', '-', '*', '/' operand.pop (rtopnd) operand.pop (ltopnd) Write ALC Push temp onto operand stack Increase value of temp position