Pseudo-Code Readability Pseudo-Code
Total Page:16
File Type:pdf, Size:1020Kb
9/14/20 Role of programming languages • What is a programming language? Pseudo-Code – Interface between user and machine • Trade-off – Ease of use - high level – Efficiency - low level CS4100 – Formal Method Dr. Martin • Describe a solution to a problem From Principles of Programming Languages: Design, • Organize a solution to a problem Evaluation, and Implementation (Third Edition), by • Reason about a solution to a problem Bruce J. MacLennan, Chapter 1, and based on slides by Istvan Jonyer. 1 2 1 2 Role of programming languages Readability • What is a programming language? • Is machine code readable? – A language that is intended for the – 000000101011110011010101011110 expression of computer programs and that • Assembly language? is capable of expressing any computer – mov dx tmp program. – add ax bx dx • Is high-level code readable? – http://www0.us.ioccc.org/years.html#2004 • http://www0.us.ioccc.org/2004/arachnid.c • http://www0.us.ioccc.org/2004/anonymous.c 3 4 3 4 Pseudo-Code Pseudo-Code Interpreters • An instruction code that is different than • Is programming difficult? that provided by the machine • In the 1950ʼs, it was… • Has an interpretive subroutine to – E.g.: IBM 650 execute • No programming language was available (not even assembler) • Implements a virtual computer • Memory was only a few thousand words – Has own data types and operations • Stored program and data on rotating drum • (Can view all programming languages • Instructions included address of next instruction so that rotating drum was under next instruction this way) to execute and no full rotations were wasted • Problem: What if address is already occupied? 5 6 5 6 1 9/14/20 Part of an IBM 650 program Program DESIGN Notations • Complexity led to development of program LOC OP DATA INST COMMENTS design notations 1107 46 1112 1061 Shall the loop box be used? 1061 30 0003 1019 – Memory layout 1019 20 1023 1026 Store C. – Control flow 1026 60 8003 1033 • Flow Diagrams (von Neumann & Goldstine) 1033 30 0003 1041 • Later: Flowcharts 1041 20 1045 1048 Store B. – Mnemonics 1048 60 8003 1105 • To help remember instruction codes 1105 30 0003 1063 • Like assembly language today 1063 44 1067 1076 Is an 02-operation called for? • These were designed to help the programmer, 1076 10 1020 8003 8003 69 8002 1061 Go to an 01-subroutine. not to be interpreted by computers 7 8 7 8 Floating Point Arithmetic • Earliest built-in floating point processing: • IBM 650 and card IBM 704 Operatorʼs Console IBM 704 reader http://www-03.ibm.com/ibm/history/exhibits/mainframe/mainframe_2423PH704C.html http://www-03.ibm.com/ibm/history/exhibits/650/650_album.html • Before that, it had to be simulated – Manual scaling • Multiply by constant factor • Use integer processor • Manually scale back result • Complicated and error-prone process 9 10 9 10 Indexing Pseudo-Code Interpreters • Array is one of most common data structures • Subroutines were commonly used to perform floating-point operations and indexing • Indexing – “Adding a variable index quantity to a fixed address • Consistent use of these simplified the in order to access the element of an array” programming process – Indexing was not supported by early computers • This simulated instructions not provided by – They used address modification the hardware • Alter the programʼs own data accessing instruction • Next logical step: • Compute actual address from pointer and offset, then write into instructionʼs data address portion – Use instruction set not provided by the computer – Pseudo-Code interpreter (a primitive, interpreted – Very error prone process programming language) 11 12 11 12 2 9/14/20 “Appendix D” A Virtual Computer • Why not simplify programming by providing • Pseudo-code interpreters implement an entire new instruction code that was simpler to use than the machineʼs own. – A virtual computer • Wilkes, Wheeler and Gill (1951) describe a – New instruction set pseudo-code and an “interpretive subroutine” – New data structures for executing it – Buried in the now famous Appendix D of The • Virtual computer: Preparation of Programs for an Electronic Digital – Higher level than actual hardware Computer • Provides facilities more suitable to applications – They must have not realized the significance of their work… • Abstracts away hardware details 13 14 13 14 Principles of Programming Design of a Pseudo-Code • The Automation Principle • Remember: itʼs 1950! – Automate mechanical, tedious, or error • Capabilities we want prone activities. – Floating point operation support (+,-,*,/,…) • The Regularity Principle – Comparisons (=,≠,<,£,>,³) – Regular rules, without exceptions, are – Indexing easier to learn, use, describe, and – Transfer of control implement. – Input/output 15 16 15 16 Hardware Assumptions Principles of Programming • The IBM 650 will serve as the hardware • Impossible error principle – 1 word: 10 decimal digits and 1 sign – Making errors impossible to commit is – 2000 byte memory preferable to detecting them after their • 1000 for data commission. • 1000 for program – E.g.: Cannot modify the program accidentally, since memory modifying operations are for “data memory” only http://www-03.ibm.com/ibm/history/exhibits/650/650_intro2.html 17 18 17 18 3 9/14/20 Hardware Language Design • 1 word: 10 decimal digits and 1 sign • 1 word can be enough to specify a 3- • +/- operand instruction – Operation: sign + 1 digit • How do we break them up? • Supports 20 operations • What do we need for instructions? – 3 3-digit operands • How many operations do we need? • Each accessing memory locations in data area – Orthogonal design: • How big is an address • Operations should be more intuitive than machine code • Use the sign to get more orthogonality 19 20 19 20 Principles of Programming Specifics • Orthogonality principle • Instruction format: – Independent functions should be controlled – op src1 src2 dst by independent mechanisms. – E.g.: x+yàz : +1 010 150 200 • “Add values at location 010 and 150, and save it to location 200” – Orthogonal design: subtract should be ʻ-1ʼ 21 22 21 22 Arithmetic Operations Comparisons • Comparisons alter control flow + - • if x < y then go to z 1 + - • First 2 operands are data locations, dst is 2 * / address of next instruction 3 x2 square root • Do we need < and >? 23 24 23 24 4 9/14/20 Extended Instruction Table What else do we need? • Moving + - • Could do “add 0” to an address, but that could be inefficient 1 + - 2 * / • Dedicate an operation to moving 3 x2 square root • Second operand is not used 4 = ≠ • “+0 src 000 dst” 5 ³ < • op src1 src2 dst • E.g.: x+yàz : +1 010 150 200 • “Add values at location 010 and 150, and save it to location 200” suppose 150 contains 0 25 26 25 26 Indexing Looping • Need – Base address • Looping through the elements of an – Index array is frequently used • Base and index take up 2 operands; what can we do with 3rd? • Whatʼs needed? – Save value of indexed element for other operations – Iterator variable (array index i) • Index operations: – Upper bound (n) – Get: xiàz : +6 xxx iii zzz – Put: xàyi : -6 xxx yyy iii – Address of beginning of loop (d) – “+7 iii nnn ddd” 27 28 27 28 Principles of Programming Input/Output • The abstraction principle • Program needs to read data from input – Avoid requiring something to be stated and write data to output more than once; factor out the recurring – Needs only a memory location to read from pattern. or write to – Read: “+8 000 000 dst” – Print: “-8 000 000 src” 29 30 29 30 5 9/14/20 Complete Instruction Set Program Structure + - 0 Move Initial data values 1 + - 2 * / +9999999999 3 x2 square root Program 4 = ≠ instructions 5 ³ < +9999999999 6 GetArray PutArray Input 7 Incr. & test data 8 Read Print 9 Stop 31 32 31 32 Implementing the Interpreter Data Structures • How to implement the interpreter for our • What data structures are needed to pseudo-coded program? simulate the IBM 650? – Model interpreter behavior after manual – Data memory execution – Program memory – Cheat: Implement using a high-level – Instruction pointer language J – We have to simulate the hardware in software 33 34 33 34 Structure of the Interpreter Instruction Pointer 1. Read the next instruction 1. Read the next instruction 2. Decode the instruction 2. Decode the instruction 3. Execute the operation 3. Execute the operation 4. Continue from step 1 4. Continue from step 1 Step 4? If there is a jump in Step 3…. Where do we update the instruction pointer Step 1 <- this is where (IP)? Increment at end of step 1; overwrite if Reasonable choices: Step 1 or Step 4 needed in step 3 for jumps What happens with a jump instruction? instruction := Program[IP]; IP := IP + 1; 35 36 35 36 6 9/14/20 Decoding Instructions Labeling • Extract part of instruction • What if we need to insert an instruction? – dst = instruction mod 1000 – All addresses would have to be shifted, and the code updated • Select operation • Solution: – Big switch-statement (case-statement) – Use labels for loops, instead of absolute memory addresses – Define label: • Arithmetic operations • -7 0LL 000 000 – Straight-forward • Only 100 numeric labels are possible (00-99) • Control-flow – Modify control flow instructions to jump to labels – IP may also need to be altered 37 38 37 38 Interpreting Labels Principles of Programming • How do we handle labels in the • Labeling principle interpreter? – Do not require users to know absolute – Look through all instructions from numbers or addresses. Instead associate beginning of program? labels with number or addresses. • Yes, but