1 / 42

Evaluating mathematical expressions

Evaluating mathematical expressions. How do computers evaluate x + y or any mathematical expression ? Answer : “Reverse Polish Notation” a + b - c / ( d + e ) is an infix expression a, b, c, d, and e are operands +, -, /, “(“, and “)” are operators

lev
Download Presentation

Evaluating mathematical expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Evaluating mathematical expressions • How do computers evaluate x + y or any mathematical expression ? • Answer : “Reverse Polish Notation” • a + b - c / ( d + e ) is an infix expression • a, b, c, d, and e are operands • +, -, /, “(“, and “)” are operators • Computers can’t evaluate infix expressions. They must first be converted to “reverse polish notation” or postfix expressions • a b + c d e + / - is a postfix expression 1BA3 G Lacey Lecture 5

  2. Push onto stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack a b 1BA3 G Lacey Lecture 5

  3. Push onto stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack b c a 1BA3 G Lacey Lecture 5

  4. Push onto stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack c d b a 1BA3 G Lacey Lecture 5

  5. Push onto stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack d e c b a 1BA3 G Lacey Lecture 5

  6. Pop off stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack e d c b a 1BA3 G Lacey Lecture 5

  7. Pop off stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack d c b a 1BA3 G Lacey Lecture 5

  8. Pop off stack • A data structure called a “stack” is used to convert infix to postfix • A stack is like a packet of PEZ or a coin holder on the Bus i.e. “last in = first out” • Things can only be put onto the top of the stack (push) • Things can only be taken from the top of the stack (pop) • Push a, b, c, d, e onto the stack • Pop 3 items off the stack Top of Stack c b a 1BA3 G Lacey Lecture 5

  9. Advantages of Stacks • Very simple to use • Use very little space • Only simple machine language instructions needed to implement a stack • thus they can run very fast • Algorithm to convert from infix to postfix is simple • Algorithm to evaluate postfix is simple 1BA3 G Lacey Lecture 5

  10. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) a POSTFIX EXPRESSION = 1BA3 G Lacey Lecture 5

  11. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) + ( POSTFIX EXPRESSION = a 1BA3 G Lacey Lecture 5

  12. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) b - + ( POSTFIX EXPRESSION = a 1BA3 G Lacey Lecture 5

  13. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) c / - ( POSTFIX EXPRESSION = b a + 1BA3 G Lacey Lecture 5

  14. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) ( d / ( POSTFIX EXPRESSION = ab+c- 1BA3 G Lacey Lecture 5

  15. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) + e / ( ( POSTFIX EXPRESSION = ab+c-d 1BA3 G Lacey Lecture 5

  16. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) / ( ( POSTFIX EXPRESSION = ab+c-de 1BA3 G Lacey Lecture 5

  17. Algorithm : Place parentheses around the expression ( a + b - c / ( d - e ) ) moving left to right until the end of the infix expression if the item is a number put into the post fix expression if the item is an open brace push it onto the stack if the item is an operator pop all operators of lower and equal precedence off of the stack and place them in the postfix expression, then push the operator onto the stack If the operator is a close brace pop all operators off the stack until an open brace, then delete Convert a + b - c / ( d + e ) to Postfix ( ( ) ( ( POSTFIX EXPRESSION = ab+c-de/ 1BA3 G Lacey Lecture 5

  18. Evaluate 3 2 + 4 3 1 + / * • Algorithm : • Moving left to right along the post fix expression • • if the item is a number push it onto the stack • • if the item is an operator pop two numbers from the • stack, execute the operator and push the result back • onto the stack • • when no more items in the postfix string pop the • answer off the stack ! + + / * 1BA3 G Lacey Lecture 5

  19. Summary of Simple to SML • Concept • High Level Language (HLL) translated into Machine Language by a compiler • Production rules translate Simple statements into SML • Code and Data space must be allocated • Maths equations need a more complex rule • A sequence of rules – an algorithm • The “Stack” data structure is used by the algorithm to convert infix maths expressions into postfix • A stack is also used to evaluate maths expressions • We will return to stacks later in the course 1BA3 G Lacey Lecture 5

  20. From Simpletron to 68332

  21. Basic Computer Architecture 1BA3 G Lacey Lecture 5

  22. Simple Overview of the MC6800 • The MC6800/MC68332 CPU has: • 8 x 32-bit Data Register, D1 to D7 • 8 x 32-bit Address Register, A1 to A7 • 1 x 32-bit Program Counter, PC • 1 x 16-bit Instruction Register, IR • 1 x 16-bit Status Register, SR • During one memory access the CPU can access 1 word (16-bit Data Bus). The Program Counter is 32 bits wide, but only 24 bits are used on the Robot (24-bit Address Bus). 1BA3 G Lacey Lecture 5

  23. The MC6800 CPU 1BA3 G Lacey Lecture 5

  24. The Unit of Memory • The fundamental unit of memory is the BIT (Binary Digit) • Each bit can take on the value Logic-1 or Logic-0 • Accessing these bits individually is not of great use • Each value can only be a Logic-1 or Logic-0 (not very useful for storing large numbers!) 1BA3 G Lacey Lecture 5

  25. Group BITs Together • View memory as a list of larger elements: • 4 bits = 1 NYBBLE • 8 bits = 1 BYTE • 16 bits = 1 WORD • 32 bits = 1 LONGWORD • When reading data/writing data to/from memory (using the MC68332), we can do so in units of bytes, words or longwords only 1BA3 G Lacey Lecture 5

  26. Data Bus The number of bits that can be read at any given time is determent by the BUS SIZE. BUS SIZE = Number of wires (data) connecting the CPU to the Memory 1BA3 G Lacey Lecture 5

  27. Memory Capacity • Memory size is expressed in terms of bytes: • 1024 Bytes = 1 KILOBYTE (Kb.) • 1024 Kilobytes = 1 MEGABYTE (Mb.) • 1024 Megabyte = 1 GIGABYTE (Gb.) • Individual bytes in memory (memory locations) are numbered sequentially. 1BA3 G Lacey Lecture 5

  28. Types of Memory: • CPU allowed to read and write contents of memory. Commonly known as Random Access Memory [RAM], 2 flavours: • Volatile: contents lost when power is switched off. • Non-Volatile: holds contents when power is switched off on computer • Read Only Memory [ROM]: memory contents are physically etched onto the chip during manufacture. Contents can only be read, and are not lost on power-off. 1BA3 G Lacey Lecture 5

  29. More Memory Types • Programmable ROM [PROM]: can write contents into it ONCE only (using a device called a PROM Programmer). Henceforth, it acts like ROM. • Erasable PROM [EPROM]: a PROM chip that may be written a number of times using an EPROM programmer • Electrically Erasable PROM [EEPROM] sometimes called FLASH • The robot has: • 64Kb RAM • 64Kb EPROM 1BA3 G Lacey Lecture 5

  30. Memory Location The number of a memory location is it’s address E.g.: The contents of memory location 3 is 67. 1BA3 G Lacey Lecture 5

  31. Bit Group Encoding Bit 3 Bit 2 Bit 1 Bit 1 0 0 0 0 = 0 0 0 0 1 = 1 0 0 1 0 = 2 0 0 1 1 = 3 0 1 0 0 = 4 0 1 0 1 = 5 0 1 1 0 = 6 0 1 1 1 = 7 1 0 0 0 = 8 1 0 0 1 = 9 1 0 1 0 = 10 1 0 1 1 = 11 1 1 0 0 = 12 1 1 0 1 = 13 1 1 1 0 = 14 1 1 1 1 = 15 1BA3 G Lacey Lecture 5 2nd Lecture, 1BA3, M. Manzke, Page: 31

  32. Bit: 7, 6, 5, 4, 3, 2, 1, 0 0 1 2 3 4 5 6 7 8 etc. Memory - Address - Bit Address: 3, 1BA3 G Lacey Lecture 5

  33. Operating Systems • A computer without software cannot: • Load and run programs from disk • Perform I/O (Input/Output) • Handle situations where the software to run requires more memory than is physically available on the computer • An Operating System = A Program which insulates the user from the technical detail of the machine. 1BA3 G Lacey Lecture 5

  34. The MONITOR A MONITOR is the simplest form of operating system. It is designed with a minimal specification and allows execution of low level assembly language programs. The monitor is a program (written in assembly language) which provides an interface to the CPU. Using knowledge from this course, you will write your own monitor in second year for a computer system (similar to the Robot) that you will design and build. 1BA3 G Lacey Lecture 5 Michael Manzke, Page: 34

  35. Monitor provides functions to allow: • Writing and editing assembly language code • Assembling of code to machine language • Execution of software • Reading/Writing memory and CPU registers • Connecting to external computer systems for saving and loading of code • Debugging 1BA3 G Lacey Lecture 5

  36. Robot Development System • The RDS contains an • Editor: used for writing and saving assembly code • Assembler • Downloader: loads code onto robot • Debugger • Terminal emulator: communicates with robot for I/O 1BA3 G Lacey Lecture 5 Michael Manzke, Page: 36

  37. Robot Development System • All software is written/saved on the PC. • Programs are downloaded and executed on the Robot. • The Robot monitor does not allow editing/assembling or saving/printing. • The monitor is saved in ROM  monitor remains even if the power is off. • User programs saved in RAM  program lost when power off. 1BA3 G Lacey Lecture 5

  38. Terminal Emulator The TE handles direct communication with the Robot. Every key hit in the TE is sent to the robot. Robot responds with messages which are printed in the TE window. 1BA3 G Lacey Lecture 5

  39. Development System Software 1BA3 G Lacey Lecture 5

  40. Development Cycle 1BA3 G Lacey Lecture 5

  41. How can the monitor and my program run at the same time? • Answer: they don’t. • Only one program can run at any given time on the CPU • Monitor stops when user program is run. • Monitor starts again when user program terminates (hopefully!). 1BA3 G Lacey Lecture 5

  42. Program Execution • How does the CPU execute code? • The program will exist as numbers in memory • The CPU must be told which program to execute (i.e. where code exists in memory -> memory location) 1BA3 G Lacey Lecture 5

More Related