150 likes | 332 Views
Reg Hahne. Atholton High School Columbia, Maryland. Nifty Assignments: Mighty Cabbage Patch Micro. Operands. Operators. Postfix Expressions. ABC*+. Prefix Expressions. +A*BC. Infix Expressions. A + B * C. Converting. Infix Expressions. Postfix String. Assembly Language Code. Step 1
E N D
Reg Hahne Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro
Operands Operators Postfix Expressions ABC*+ Prefix Expressions +A*BC Infix Expressions A + B * C
Converting Infix Expressions Postfix String Assembly Language Code
Step 1 Creating a postfix string using a stack Let’s try a simple one: A + B * C
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
Let’s try a more difficult one: A – B / C $ D * E
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
What about parentheses? (A + B) * (C – D) $ E * F
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 *
Step 2 Creating assembly language code using a stack
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
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
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
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
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