Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Translation Module 6. Mid-Term Results.

Similar presentations


Presentation on theme: "Program Translation Module 6. Mid-Term Results."— Presentation transcript:

1 Program Translation Module 6

2 Mid-Term Results

3

4 Mid-term Above 93/100 is A.

5 Mid-term 93/100 or above is A. 60/100 or below is fail.

6 Mid-term Above 93/100 is A. 60/100 or below is fail. For students who got 120/200 or less at mid- term attendance in make-up is mandatory.

7 Mid-term Above 93/100 is A. 60/100 or below is fail. For students who got 120/200 or less at mid- term attendance in make-up is mandatory. Note that April 10 th 2008 is the course withdrawal deadline.

8 Mid-term Above 93/100 is A. 60/100 or below is fail. For students who got 120/200 or less at mid-term attendance in make-up is mandatory. Note that April 10 th 2008 is the course withdrawal deadline. I will send a private email to those students who are below the fail threshold so far and let them know how good they should perform from now (future exams and HWs) on to avoid being failed.

9 Questions Mid-term? Assignment 3? Assignment 3 is due mid-night April 4 th.

10 Program Translation Module 6

11 Introduction Computers are not smart. They do only what we ask them to do.

12 Introduction Computers are not smart. They do only what we ask them to do. That is they can do whatever we know how to order it to perform. But we use computers because they are fast and precise.

13 The Binary Machine A modern computer can run programs written in Java, C, C++, Lisp, JavaScript, Cobol and many others. So the computers appear to be Multilingual.

14 The Binary Machine A modern computer can run programs written in Java, C, C++, Lisp, JavaScript, Cobol and many others. So the computers appear to be Multilingual. But every computer ever built “understands” only one language:

15 The Binary Machine A modern computer can run programs written in Java, C, C++, Lisp, JavaScript, Cobol and many others. So the computers appear to be Multilingual. But every computer ever built “understands” only one language: the machine language it is wired to use.

16 The Binary Machine A modern computer can run programs written in Java, C, C++, Lisp, JavaScript, Cobol and many others. So the computers appear to be Multilingual. But every computer ever built “understands” only one language: the machine language it is wired to use. Thus, the machine language of an Intel PC is different than an IBM personal computer or Sun Workstation.

17 The Binary Machine If we want to use a language like JavaScript to write a program, first we need to have a tool to translate the program to machine language which our computer understands.

18 Program Translation The notion of translation is central to our discussion. A modern programming language is designed with two audiences in mind:

19 Program Translation The notion of translation is central to our discussion. A modern programming language is designed with two audiences in mind: The programmers who use the language to write programs for computers. The computer, which needs to execute (run) the instructions in the language.

20 Program Translation

21 Machine Language Here is a sample of a machine language for a hypothetical computer: 0000010010000000 0000000010000001 0000111100000000

22 Binary Presentation The instructions of a machine language are represented using just 0s and 1s.

23 Binary Presentation The instructions of a machine language are represented using just 0s and 1s. In the next module we’ll see that all computers represent all the information in binary notation.

24 Binary Presentation The instructions of a machine language are represented using just 0s and 1s. In the next module we’ll see that all computers represent all the information in binary notation because they can only distinguish between two values (low voltage level and high voltage level), which we label as 0 and 1.

25 Binary Presentation The instructions of a machine language are represented using just 0s and 1s. In the next module we’ll see that all computers represent all the information in binary notation because they can only distinguish between two values (low voltage level and high voltage level), which we label as 0 and 1. Binary notation: a notation which uses only two symbols, 0 and 1, to present information.

26 Binary Presentation If the computer can work only in terms of 0 and 1, how are we going to represent, store and manipulate the information?

27 Binary Presentation If the computer can work only in terms of 0 and 1, how are we going to represent, store and manipulate the information? We can use some sort of binary notation.

28 Binary Presentation For example we can have an easy form of binary representation called binary-coded decimal (BCD) notation, in which we use a table for converting a number to this notation and vice versa.

29 Binary Presentation For example we can have an easy form of binary representation called binary-coded decimal (BCD) notation, in which we use a table for converting a number to this notation and vice versa. Here is the table: DigitBinary CodeDigitBinary Code 0000050101 1000160110 2001070111 3001181000 4010091001

30 Examples of BCD Convert number 5683 to BCD format.

31 Examples of BCD Convert number 5683 to BCD format. 5 6 8 3

32 Examples of BCD Convert number 5683 to BCD format. 5 6 8 3 By looking at BCD table and inserting the BCD code for each digit we have: 0101 0110 1000 0011

33 Examples of BCD Convert number 5683 to BCD format. 5 6 8 3 By looking at BCD table and inserting the BCD code for each digit we have: 0101 0110 1000 0011 So the BCD equivalent is: 0101011010000011

34 Examples of BCD Convert the BCD code 0100100000010000 to a decimal (base 10) number.

35 Examples of BCD Convert the BCD code 0100100000010000 to a decimal (base 10) number. 1- Divide the sequence into chunks of length 4. Why? 0100 1000 0001 0000

36 Examples of BCD Convert the BCD code 0100100000010000 to a decimal (base 10) number. 1- Divide the sequence into chunks of length 4. Why? 0100 1000 0001 0000 2- Use the BCD table to find the corresponding digit for each chunk. 4 8 1 0

37 Examples of BCD Convert the BCD code 0100100000010000 to a decimal (base 10) number. 1- Divide the sequence into chunks of length 4. Why? 0100 1000 0001 0000 2- Use the BCD table to find the corresponding digits for each chunk. 4 8 1 0 3- Put the numbers together in the same order: 4810

38 Do it yourself? a.Convert the number 873 into BCD format. b.Convert 0111000010010101 to a decimal number.

39 Do it yourself? a.Convert the number 873 into BCD format. answer: 100001110011 b.Convert 0111000010010101 to a decimal number. answer: 7095

40 BCD notation Disadvantages 1.For each decimal number of length n we have a BCD code of length 4n. Example: 211 and 001000010001 2. Arithmetic on numbers in BCD format is complicated.

41 Base-10 Notation In everyday life we represent numbers in base 10 Positional Notation. – positional: the value of a digit within a number depends on its position. Example: 342 and 432 – base 10 : the value of any position is a multiple of a power of 10. The digits we use in this notation are: {0, 1, 2, 3, 4, 5, 6, 7,8,9 }

42 Base-10 Notation Example: the number 7689 is interpreted as:

43 Base-10 Notation Example: the number 129 is interpreted as:

44 Base 2 Notation There is nothing special about base 10 notation. We can use any base to represent numbers. But since computers only understand 0 and 1 we use base 2 notation.

45 Base 2 Notation In base two notation we have only two digits: {0, 1} Each digit in this notation is a multiple of a power of 2 just as in base 10 where each digit is a multiple of a power of 10. 11010101 is an example of a number in base-2 format.

46 Base-2 Example The number 11010101 is interpreted as :

47 Base 2 Example 213 in base 10 = 11010101 in base 2

48 Do it yourself What base-10 number the following base-2 number represents? 10010

49 Do it yourself What base-10 number the following base-2 number represents? 10010 Answer: 18

50 How to Convert Base-10 to Base-2 We can convert a decimal number to base-2 using a simple algorithm. Lets see the algorithm through examples.

51 Example 1 Convert decimal number 29 to binary.

52 Example 1 Convert decimal number 29 to binary. 1.29 / 2 quotient = 14 remainder = 1

53 Example 1 Convert decimal number 29 to binary. 1.29 / 2 quotient = 14 remainder = 1 2.14 / 2 quotient = 7 remainder = 0

54 Example 1 Convert decimal number 29 to binary. 1.29 / 2 quotient = 14 remainder = 1 2.14 / 2 quotient = 7 remainder = 0 3.7 / 2 quotient = 3 remainder = 1

55 Example 1 Convert decimal number 29 to binary. 1.29 / 2 quotient = 14 remainder = 1 2.14 / 2 quotient = 7 remainder = 0 3.7 / 2 quotient = 3 remainder = 1 4.3 / 2 quotient = 1 remainder = 1

56 Example 1 Convert decimal number 29 to binary. 1.29 / 2 quotient = 14 remainder = 1 2.14 / 2 quotient = 7 remainder = 0 3.7 / 2 quotient = 3 remainder = 1 4.3 / 2 quotient = 1 remainder = 1 5.1 / 2 quotient = 0 remainder = 1 (Stop)

57 Example 1 Convert decimal number 29 to binary. 1.29 / 2 quotient = 14 remainder = 1 2.14 / 2 quotient = 7 remainder = 0 3.7 / 2 quotient = 3 remainder = 1 4.3 / 2 quotient = 1 remainder = 1 5.1 / 2 quotient = 0 remainder = 1 (Stop) 6.Sequence the reminders starting from last step to first: 11101. This is the binary (base-2) representation of 29.

58 Another view of the algorithm Converting decimal number 29 to binary 11101

59 Example 2 Convert decimal 213 to binary: 1.213 / 2 quotient = 106 Remainder = 1 2.106 / 2 quotient = 53 Remainder = 0 3.53 / 2 quotient = 26 Remainder = 1 4.26 / 2 quotient = 13 Remainder = 0 5.13 / 2 quotient = 6 Remainder = 1 6.6 / 2 quotient = 3 Remainder = 0 7.3 / 2 quotient = 1 Remainder = 1 8.1 / 2 quotient = 0 Remainder = 1 Stop. 9.Sequence from last to first: 11010101

60 Do it yourself Find the binary presentation of decimal 89.

61 How to Present Text in Binary Format Ok, we know how to represent numbers in binary format, but how about a piece of text?

62 How to Present Text in Binary Format Ok, we know how to represent numbers in binary format, but how about a piece of text? Like BCD notation we can have a table which maps each character to an integer code in binary.

63 How to Present Text in Binary Format Ok, we know how to represent numbers in binary format, but how about a piece of text? Like BCD notation we can have a table which maps each character to an integer code in binary. This coding may vary from machine to machine. But many computers use a well known coding for representing characters known as ASCII (American Standard Code for Informatio Interchange).

64 Examples of ASCII Entries CharacterInteger code in decimalInteger code in binary space3200100000 $3600100100 25000110010 35100110011 ?6300111111 “3400100010 A6501000001 B6601000010 a9701100001 b9801100010 z12201111010

65 Encoding Instructions Computers understand only binary.

66 Encoding Instructions Computers understand only binary. We need to feed the instructions to computer in binary format. But how do we do it?

67 Encoding Instructions Computers understand only binary. We need to feed the instructions to computer in binary format. But how do we do it? Again we need to have a table (mapping) for having a code in binary for each instruction.

68 Encoding Instructions Computers understand only binary. We need to feed the instructions to computer in binary format. But how do we do it? Again we need to have a table (mapping) for having a code in binary for each instruction. For example we can define an 8-bit operation code followed by 8 more bits which provides details about the instruction.

69 Bit and Byte What is a bit?

70 Bit and Byte What is a bit? A bit is a unit of storage which can store only 0 or 1.

71 Bit and Byte What is a bit? A bit is a unit of storage which can store only 0 or 1. A bit storage: 01

72 Bit and Byte What is a bit? A bit is a unit of storage which can store only 0 or 1. A bit storage: Every 8-bit is called a byte. 01

73 Bit and Byte What is a bit? A bit is a unit of storage which can store only 0 or 1. A bit storage: Every 8-bit is called a byte. 01 10011101

74 Bit and Byte What is a bit? A bit is a unit of storage which can store only 0 or 1. A bit storage: Every 8-bit is called a byte. Every 1024 byte is a kilo-byte. Every 1024 kilo-byte is a mega-byte Every 1024 mega-byte is a giga-byte. 01 10011101

75 Encoding Instructions We can have a 8-bit (one byte) operation code followed by 8 more bits (another byte) which gives details about the instruction.

76 Encoding Instructions We can have a 8-bit (one byte) operation code followed by 8 more bits (another byte) which gives details about the instruction. Example: 0000010000110110

77 The architecture of our Computer Our hypothetical computer has an 8-bit (one byte) Accumulator.

78 The architecture of our Computer Our hypothetical computer has an 8-bit (one byte) Accumulator. Accumulator is a special location used for storing intermediate results of computation.

79 The architecture of our Computer Our hypothetical computer has an 8-bit (one byte) Accumulator. Accumulator is a special location used for storing intermediate results of computation. For Example you want to calculate: 97 + (210 – 113) you might write the result of subtraction on a piece of paper before adding the result with 97.

80 The architecture of our Computer Also our architecture has 256 bytes of memory labeled (addressed) with 0 to 255.

81 The architecture of our Computer Also our architecture has 256 bytes of memory labeled (addressed) with 0 to 255. Memory (In computer terminology): Place for storing data in binary format. Smallest unit of memory is a bit.

82 The architecture of our Computer Also our architecture has 256 bytes of memory labeled (addressed) with 0 to 255. Memory (In computer terminology): Place for storing data in binary format. Smallest unit of memory is a bit. The size of the memory of today’s computers is something of magnitude of giga-bytes.

83 The Architecture of Our Hypothetical Computer

84 Example of Instructions of our Computer InstructionInformationCodeExplanation LODX00000100Loads accumulator with contents of memory location X STOX00000101Stores accumulator value into memory location X HLTX00001111Halts (Stops) Execution ADDX00000000Adds contents of memory location X to accumulator

85 Example Now the piece of machine language we saw previously should have a meaning to you: 0000010010000000 0000000010000001 0000111100000000

86 Example [1] 0000010010000000 [2] 0000000010000001 [3] 0000111100000000 We separate the sequences into bytes (chunks of length 8):

87 Example [1] 0000010010000000 [2] 0000000010000001 [3] 0000111100000000 We separate the sequences into bytes (chunks of length 8): [1] 00000100 10000000 [2] 00000000 10000001 [3] 00001111 00000000

88 Example [1] 00000100 10000000 [2] 00000000 10000001 [3] 00001111 00000000 First byte is the code for instruction. So we must look for the corresponding instruction in the table:

89 Example [1] 00000100 10000000 [2] 00000000 10000001 [3] 00001111 00000000 First byte is the code for instruction. So we must look for the corresponding instruction in the table: LOD 10000000 ADD 10000001 HLT 00000000

90 Example The second byte represents details about Instruction which is an integer in binary format: LOD 10000000 ADD 10000001 HLT 00000000 10000000 represents 128 in decimal, 10000001 represents 129 in decimal and 00000000 is 0.

91 Example Putting all together we have : LOD 128 ADD 129 HLT 0 This is a piece of language called assembly Language.

92 Assembler A program that takes as its input a program written in assembly and outputs the same program written in Machine Language.

93 Assembler A program that takes as its input a program written in assembly and outputs the same program written in Machine Language. Sourcex LOD 128 ADD 129 HLT 0 Assembler 0000010010000000 0000000010000001 0000111100000000

94 Going Further Now assume a programmer writes a program in a high level language such as JavaScript. The programmer needs to programs to make her/his program understandable to computer: Compiler and Assembler.

95 Going Further Now assume a programmer writes a program in a high level language such as JavaScript. The programmer needs to programs to make her/his program understandable to computer: Compiler and Assembler. LOD 128 ADD 129 HLT 0 Assembler 0000010010000000 0000000010000001 0000111100000000 Compiler …. If(x == 4) Document. write( ); ….

96 Assembly There are many programmers who write their programs directly in assembly as opposed to any high level language such as Java, because

97 Assembly There are many programmers who write their programs directly in assembly as opposed to any high level language such as Java, because – It gives the programmer a precise control of the computer’s functionalities. – In most cases the programs written directly in assembly language are faster than programs written in high level languages.

98 Back to our piece of Assembly code LOD 128 ADD 129 HLT 0

99 Back to our piece of Assembly code LOD 128 ADD 129 HLT 0 LOD 128 : Loads the contents of memory location (a byte) 128 into accumulator.

100 Back to our piece of Assembly code LOD 128 ADD 129 HLT 0 LOD 128 : Loads the contents of memory location (a byte) 128 into accumulator. ADD 129: Adds the number 129 to the number already in accumulator and puts the result in accumulator.

101 Back to our piece of Assembly code LOD 128 ADD 129 HLT 0 LOD 128 : Loads the contents of memory location (a byte) 128 into accumulator. ADD 129: Adds the number 129 to the number already in accumulator and puts the result in accumulator. HLT : Stops the process.

102 Another Example LOD 213 ADD 214 ADD 215 ADD 216 ADD 217 STO 213 HLT What does this program do?

103 The assembly Instructions for our Machine Conventions: 1.All variables refer to 8-bit binary number.

104 The assembly Instructions for our Machine Conventions: 1.All variables refer to 8-bit binary number. ex: LOD X X is an 8-bit number ( [0-255])

105 The assembly Instructions for our Machine Conventions: 1.All variables refer to 8-bit binary number. ex: LOD X X is an 8-bit number ( [0-255]) 2.Variables can represent a number or an address. A number is indicated by a leading pound sign (#).

106 The assembly Instructions for our Machine Conventions: 1.All variables refer to 8-bit binary number. ex: LOD X X is an 8-bit number ( [0-255]) 2.Variables can represent a number or an address. A number is indicated by a leading pound sign (#). ex: LOD 123 and LOD #123 LOD 123  Load the contents of memory location 123 into the accumulator LOD #123  Load number 123 into the accumulator.

107 The assembly Instructions for our Machine Another ex: ADD 212 & ADD #212 ADD 212 : Add the contents of memory location 212 to accumulator. ADD #212 : Add number 212 to accumulator. 3. We use letter A to refer to the accumulator.

108 Data Flow Instructions InstructionVariableExplanation LODXLoad the contents of memory location specified with address X into accumulator.,,#XLoad the number X into accumulator. STOXStore the contents of memory location specified with address X Into accumulator.,,#XStore the number X into accumulator.

109 Arithmetic Instructions InstructionVariableExplanation ADDXAdd the contents of memory location specified by address X to contents of accumulator.,,#XAdd the number X to contents of accumulator. SUBXSubtract the contents of memory location specified by address X from contents of accumulator.,,#XSubtract number X from contents of accumulator. MULXMultiply the contents of accumulator by the contents of memory location specified by address X.,,#XMultiply the contents of accumulator by number X.

110 Control Instructions A program is a sequence of instructions.

111 Control Instructions A program is a sequence of instructions. In normal condition during the execution of the program (assembly program in our case), the computer starts with execution of first instruction, followed by second, third and all the way to the last instruction.

112 Control Instructions A program is a sequence of instructions. In normal condition during the execution of the program (assembly program in our case), the computer starts with execution of first instruction, followed by second, third and all the way to the last instruction. But sometimes we want to change this normal flow of instruction executions.

113 Control Instructions A program is a sequence of instructions. In normal condition during the execution of the program (assembly program in our case), the computer starts with execution of first instruction, followed by second, third and all the way to the last instruction. But sometimes we want to change this normal flow of instruction executions. We use control instructions to direct the flow of instruction execution in a way that we have in mind.

114 Control Instructions An example of situation where we want to change the flow of execution: We have sequence of three instructions: instruction 1 instruction 2 instruction 3 and we need to run this sequence 20 times.

115 Control Instructions One way to write such program: instruction 1 instruction 2 instruction 3 instruction 1 instruction 2 instruction 3. instruction 1 instruction 2 instruction 3 – That is to repeat the sequence of three instructions 20 times in our program.

116 Control Instructions Smarter algorithm: Step 1: Set memory location X to 20 Step 2: if the contents of memory location X is not zero execute the sequence: instruction instruction 2 instruction 3 Subtract the contents of memory location X by 1 Jump to step 2 else HLT

117 Control Instructions InstructionVariableExplanation JMPY Jump to instruction number Y (Go to Y-th instruction in the program ) JMZYJump to instruction number Y if the contents of accumulator is 0. Otherwise, go to next instruction. HLTHalt execution (Stop the execution). Don’t do anything more.

118 Example 1 LOD 213 ADD 214 ADD 215 ADD 216 ADD 217 STO 213 HLT

119 Example 2 LOD 212 ;Load the memory location 212 into accumulator ADD #1 ;Add one to accumulator STO 213 ;Store the contents of accumulator into memory ADD #1 ;location 213 STO 214 ADD #1 STO 215 HLT

120 Comment Comments are little explanatory notes for programmers inside the program.

121 Comment Comments are little explanatory notes for programmers inside the program. In our assembly language, a comment is anything from the semicolon (;) to the end of the line.

122 Comment Comments are little explanatory notes for programmers inside the program. In our assembly language, a comment is anything from the semicolon (;) to the end of the line. These notes are ignored by assembler. That is they have no effect on what the programs do.

123 Do it yourself. What number in binary format will be in memory location 197 at the end of the program? LOD #2 ;Load the number 2 into accumulator. MUL #2 MUL #2 ;Multiply by 2 the contents of accumulator. ADD #2 SUB #2 ;Subtract 2 from the contents of the accumulator. STO 197 HLT

124 Here is a program which uses some of the control instructions. [1] LD #0 [2] STO 123 [3] SUB 210 [4] JMZ 9 ; Jump to instruction 9 if the contents of acc. is 0 [5] LD 123 [6] ADD #1 [7] STO 123 [8] JMP 3 ;Jump to instruction 3 [9] HLT Can you write a simpler program which does the same job? What will the program do if we change number 210 to 55 in line 3 ? Using Control Instructions


Download ppt "Program Translation Module 6. Mid-Term Results."

Similar presentations


Ads by Google