Download presentation
Presentation is loading. Please wait.
Published byJasmine Eleanore Wiggins Modified over 8 years ago
1
Atholton High School Columbia, Maryland Nifty Assignments: Mighty Cabbage Patch Micro
2
Infix Expressions A + B * C Postfix Expressions ABC*+ Prefix Expressions +A*BC Operators Operands
3
Infix Expressions Converting 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
Infix String Operator Stack Postfix String A+B*CA+B*CA+B*CA+B*CRule 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. + AABA + *AB AB+ C+* * +
6
Let’s try a more difficult one: A – B / C $ D * E
7
Infix String Operator Stack Postfix String A–B/C$D*EA–B/C$D*EA–B/C$D*EA–B/C$D*E A – A B A – /AB AB– C–/ –/ $ABC –/$ ABC$/ – *–* D ABCD ABC$/DE 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 ( A B A – ( * (A+B)*(C–D)$E*F(A+B)*(C–D)$E*F(A+B)*(C–D)$E*F(A+B)*(C–D)$E*F ( +(+ AB+ AB+* ( AB+ AB+C *( AB+C *( AB+C D – *( AB+C D – * * $ AB+C D – *$ AB+C D – E * AB+C D – E $ * * AB+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 T 0 : Store value from accumulator in T 0
12
Postfix String Operand Stack ALC ABC*+ABC*+ABC*+ABC*+Rule When you meet an operator, pop two elements from the stack. The first element becomes the right operand; the other becomes the left. B A C A A BA LDA B MUL C STA T 0 T0T0T0T0 LDA A ADD T 0 STA T 1 T1T1T1T1
13
Postfix String Operand Stack ALC A LDA A B AB+CD–E$*F*AB+CD–E$*F*AB+CD–E$*F*AB+CD–E$*F* T0T0T0T0 C A ADD B STA T 0 T0T0T0T0 T0T0T0T0C D T0T0T0T0 LDA C SUB D STA T 1 T1T1T1T1 T0T0T0T0 T1T1T1T1E T0T0T0T0 LDA T 1 EXP E STA T 2 T2T2T2T2 T3T3T3T3 LDA T 0 MUL T 2 STA T 3 T3T3T3T3 F T4T4T4T4 LDA T 3 MUL F STA T 4 One for you to try: A B + C D E – / F + * G –
14
Pseudocode Infix Expression Postfix String Infix [i] Action 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 else done = true '(' 'A'…'Z' ')'
15
Pseudocode Postfix Expression ALC Postfix [i] Action Push onto operand stack operand.pop (rtopnd) operand.pop (ltopnd) Write ALC Push temp onto operand stack Increase value of temp position '+', '-', '*', '/' 'A'...'Z'
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.