1 / 56

Chapter 6

Chapter 6. Low Level Programming and Psuedocode. Chapter Goals - The BIG slices. PIECE UNO : How a software works at the lowest possible level PIECE DOS: How algorithms are created using psuedocode. Chapter Goals – Smaller Slices. operations that a computer can perform

brewster
Download Presentation

Chapter 6

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. Chapter 6 Low Level Programming and Psuedocode

  2. Chapter Goals - The BIG slices • PIECE UNO: How a software works at the lowest possible level • PIECE DOS: How algorithms are created using psuedocode

  3. Chapter Goals – Smaller Slices • operations that a computer can perform • the Pep/8 virtual machine • immediate addressing modedirect addressing mode • machine language and assembly language

  4. Chapter Goals – Smaller Slices • Low-level algorithms • Using pseudocode to express and algorithm • Pseudocode functionality (what can it do?) • Translate a simple pseudocode algorithm into assembly language

  5. Some Definitions Machine Code: A binary code that tells the machine to do a particular instruction Assembly Language Uses “mnemonic” codes for instructions rather than “binary” codes. Example: “Add” instead of “0111” Pseudocode Shorthand English-like language people use to express algorithms (Each uses various levels of detail)

  6. THE WHOLE CHAPTER IN 4 SLIDES Brought to you by… The Power of Abstraction!

  7. SLIDE 1: What can Pep8 DO?? • Pep8 can do simple things like: • Get characters from the keyboard • Write characters to the screen • Get numbers from the keyboard • Write numbers to the screen • Add numbers • Store numbers to RAM • Jump around in the program • Etc…

  8. SLIDE 2: A Pseudocode algorithm Step 1) Write an ‘H’ character to the screen Step 2) Write an ‘e’ character to the screen Step 3) Write an ‘l’ character to the screen Step 4) Write an ‘l’ character to the screen Step 5) Write an ‘o’ character to the screen Step 6) Stop

  9. SLIDE 3: The Pep8 Assembly • CHARO 0x0048,i ;Output an 'H' • CHARO 0x0065,i ;Output an 'e' • CHARO 0x006C,i ;Output an 'l' • CHARO 0x006C,i ;Output an 'l' • CHARO 0x006F,i ;Output an 'o' • STOP ;Tell Pep8 to stop • .END ;Tell Assembler to stop

  10. SLIDE 4: Assembled… Assembly code is translated into binary for the machine. CHARO 0x0048,i ;Output an 'H' Becomes… 500048 Which is really.. 0101 0000 0000 0000 0100 1000

  11. Algorithm, Assembly, and Machine Code

  12. Pseudocode Pseudocode A way of expressing algorithms using English phrases and indention to make the steps in the solution explicit Example: Step 1 ) Write an ‘H’ character to the screen Etc…

  13. Assembly Language Assembly language Uses mnemonic codes to represent machine-language instructions Example: CHARO 0x0048,i Instead of… 500048

  14. Machine Language Machine language Binary coded instructions built into the hardware Circuits made of gates perform these instructions Why would anyone choose to use machine language? (Hint: they had no choice. Why?) Why would anyone choose to use machine language? (Hint: they had no choice. Why?)

  15. Machine Language Characteristics of machine language: • Each machine-language instruction does only one very low-level task • Every processor type has its own set of specific machine instructions • The relationship between the processor and the instructions it can carry out is completely integrated

  16. An Assembler Assembler A program that translates assembly code into machine code

  17. More fun with Assembly The Pep8 Virtual Machine

  18. Pep/8 Virtual Computer Virtual computer A hypothetical machine designed to contain the important features of a real computer that we want to illustrate Pep/8 A virtual computer designed by Stanley Warford that has 39 machine-language instructions No; we are not going to cover all of them!

  19. Pep/8 Assembly Language Remember the difference between immediate and direct addressing? i : immediate d: direct

  20. Pep/8 Assembly Language What is the difference between operations and pseudo operations?

  21. Pep/8 Assembly Language Our Assembly Code for "Hello" 21

  22. Assembly Process

  23. Pep/8 Assembly Language Our Assembly Code for "Hello“ … With Machine Code 23

  24. Now that we have some Pep8 vocabulary, we can write some more algorithms!

  25. A New Program Problem: Add 3 numbers

  26. Pseudocode for Add 3 numbers Step 1) load the sum (0) to the accumulator Step 2) Read first number from the keyboard Step 3) Add first number to the accumulator Step 4) Read second number from the keyboard Step 5) Add second number to the accumulator Step 6) Read third number from the keyboard Step 7) Add third number to the accumulator Step 8) Store accumulator back to the sum Step 9) Display the sum Step 10) Stop

  27. Our Completed Program • BR main • sum: .WORD 0x0000 • num1: .BLOCK 2 • num2: .BLOCK 2 • num3: .BLOCK 2 • main: LDA sum,d • DECI num1,d • ADDA num1,d • DECI num2,d • ADDA num2,d • DECI num3,d • ADDA num3,d • STA sum,d • DECO sum,d • STOP • .END

  28. More About Pseudocode We need to express more complicated algorithms Like Washing our Hair

  29. A Algorithm for Hair Step 0: Wet Hair Step 1: Apply shampoo Step 2: Lather Step 3: Rinse Step 4: If (Hair not clean enough) goto Step 1 Step 5: Done

  30. Pseudocode With Pseudocode we can express… Individual steps As Well as Structure… Repetition Selection

  31. Pseudocode Functionality Repetition Repeating a series of statements Set count to 1 WHILE ( count < 10) Write "Enter an integer number" Read aNumber Write "You entered " + aNumber Set count to count + 1 How many values were read?

  32. Pseudocode Functionality Selection Choose to execute one statement (or group of statements) or another statement (or group of statements) IF ( age < 12 ) Write "Pay children's rate" Write "You get a free box of popcorn" ELSE IF ( age < 65 ) Write "Pay regular rate" ELSE Write "Pay senior citizens rate"

  33. Another Program Problem: Sort pairs of numbers

  34. Pseudocode Example Problem: Read in pairs of positive numbers and print each pair in order. WHILE (not done) Write "Enter two values separated by blanks" Read number1 Read number2 Print them in order

  35. Pseudocode Example How do we know when to stop? Let the user tell us how many Print them in order? If first number is smaller print first, then second If first number if larger print second, then first 35

  36. Pseudocode Example Write "How many pairs of values are to be entered?" Read numberOfPairs Set numberRead to 0 WHILE (numberRead < numberOfPairs) Write "Enter two values separated by a blank; press return" Read number1 Read number2 IF(number1 < number2) Print number1 + " " + number2 ELSE Print number2 + " " number1 Increment numberRead 36

  37. Our Completed Program BR Main mesg1: .ASCII "Enter number\x00" mesg2: .ASCII "Enter pairs\x00" numRead: .WORD 0x00 numPairs: .BLOCK 2 number1: .BLOCK 2 number2: .BLOCK 2 Main: STRO mesg1, d DECI numPairs, d Begin: STRO mesg2, d DECI number1, d DECI number2, d LDA number1, d CPA number2, d BRLE less BR greater test: LDA numRead, d ADDA 1, i STA numRead, d CPA numPairs, d BRLT Begin BR End less: DECO number1, d DECO number2, d BR test greater: DECO number2, d DECO number1, d BR test End: STOP .END Focus on the structure

  38. The Data Section mesg1: .ASCII "Enter number\x00" mesg2: .ASCII "Enter pairs\x00" numRead: .WORD 0x00 numPairs: .BLOCK 2 number1: .BLOCK 2 number2: .BLOCK 2

  39. A Code Section Begin: STRO mesg2, d DECI number1, d DECI number2, d LDA number1, d CPA number2, d BRLE less BR greater

  40. Let’s get Bitty Pep8 Machine Code in more detail

  41. Architecture of Pep8 Pep/8 Registers/Status Bits Covered • The program counter (PC) (contains the address of the next instruction to be executed) • The instruction register (IR) (contains a copy of the instruction being executed) • The accumulator (A register) The memory unit is made up of 65,636 bytes of storage

  42. Architecture of Pep/8 Figure 6.1 Pep/8’s Architecture

  43. Instruction Format Figure 6.2 Pep/8 Instruction Format

  44. Instruction Format Operation code Specifies which instruction is to be carried out Register specifier Specifies which register is to be used (only use A in this chapter) Addressing-mode specifier Says how to interpret the operand part of the instruction

  45. Instruction Format Figure 6.3 Difference between immediate addressing mode and direct addressing mode

  46. Instruction Format Is there something we are not telling you about the addressing mode specifier? How can you tell?

  47. Some Sample Instructions Figure 6.4 Subset of Pep/8 instructions

  48. Sample Instructions What do these instructions mean?

  49. Sample Instructions What do these instructions mean?

  50. Sample Instructions What do these instructions mean?

More Related