fundamentals of programming
play

Fundamentals of Programming Session 2 Instructor: Reza - PowerPoint PPT Presentation

Fundamentals of Programming Session 2 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 Sharif University of Technology Outlines Computer Organization Programming Language Binary numbers Addition


  1. Fundamentals of Programming Session 2 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 Sharif University of Technology

  2. Outlines  Computer Organization …  Programming Language  Binary numbers  Addition  Subtraction  Division  Multiplication  One’s and Two’s Complement 2

  3. Computer Organization …  a central processing unit (CPU) consists of  an arithmetic/logic unit (ALU) where math and logic operations are performed,  a control unit which directs most operations by providing timing and control signals,  and registers that provide short-term data storage and management facilities.  an arithmetic/logic unit (ALU)  The type of operation that the ALU needs to perform is determined by signals from the control unit.  The data can come either from the input unit or from the memory unit.  Results of the operation can either be transferred back to the memory unit or directly to the output unit. 3

  4. Computer Organization …  control unit  contains logic and timing circuits that generate the appropriate signals necessary to execute each instruction in a program  It fetch es an instruction from memory by sending an address and a read command to the memory unit.  After decoding this instruction, the control unit transmits the appropriate signals to the other units in order to execute the specified operation.  This sequence of fetch and execute is repeated by the control unit until the computer is either powered off or reset. 4

  5. Programming Language  A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication.  Many programming languages have some form of written specification of their syntax (form) and semantics (meaning). Some languages are defined by a specification document. For example, the C programming language is specified by an ISO Standard. Other languages, such as Perl, have a dominant implementation that is used as a reference. 5

  6. Programming Language …  Evolution of Programming Languages:  First Generation: Machine languages  Strings of numbers giving machine specific instructions  Example: 1300042774 1400593419 1200274027  Computer only understands machine language instructions.  Second Generation: Assembly languages  English-like abbreviations representing elementary computer operations (translated via assemblers) 6

  7. Programming Language …  Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY  Third Generation : High-level languages  Codes similar to everyday English  Use mathematical notations (translated via compilers)  Example: grossPay = basePay + overPay 7

  8. Programming Language … 8

  9. Common Software  Operating System  Assemblers  Compilers  Interpreters 9

  10. Binary numbers  Binary numbers  Why binary?  Computers are built using digital circuits  Inputs and outputs can have only two values  True (high voltage) or false (low voltage)  Converting base 10 to base 2  Octal and hexadecimal  Integers  Unsigned integers  Integer addition  Signed integers  C bit operators  And, or, not, and xor 10  Shift-left and shift-right

  11. Base 10 and Base 2  Base 10  Each digit represents a power of 10  4173 = 4*10 3 + 1*10 2 + 7*10 1 + 3*10 0  Base 2  Each bit represents a power of 2  10110 = 1*2 4 + 0*2 3 + 1*2 2 + 1*2 1 + 0*2 0 = 22  Question :  What is the binary representation of number 12 ?  Response:  1100 11

  12. Base 8 000 = 0  Octal (base 8) 001 = 1  Digits 0, 1, …, 7 010 = 2 011 = 3  Thus the 12 bit binary 110 010 101 001 100 = 4 101 = 5 number converted to Oct is: 110 = 6 6251 111 = 7 12

  13. Base 8  Question :  What is the octal representation of number 118 (in base 10) ?  Response:  166  Question :  What is the octal representation of number 1011000101 (in base 2) ?  Response:  1305  Question :  What is the binary representation of number 1472 (in base 8) ?  Response:  1100111010 13

  14. Base 16  Hexadecimal (base 16)  Digits 0, 1, …, 9, A, B, C, D, E, F  Thus the 16-bit binary number 1011 0010 1010 1001 converted to Hex is: B2A9 0000 = 0 1000 = 8 0001 = 1 1001 = 9 0010 = 2 1010 = A 0011 = 3 1011 = B 0100 = 4 1100 = C 0101 = 5 1101 = D 0110 = 6 1110 = E 0111 = 7 1111 = F 14

  15. Base 16  Question :  What is the hexadecimal representation of number 375 (in base 10) ?  Response:  177  Question :  What is the hexadecimal representation of number 1011000101(in base 2) ?  Response:  2C5  Question :  What is the binary representation of number 6A4D2 (in base 16) ?  Response:  01101010010011010010 15

  16. Integers  Fixed number of bits in memory  Short: usually 16 bits  Int: 16 or 32 bits  Long: 32 bits  Unsigned integer  No sign bit  Always positive or 0  Example of unsigned int  00000001  1  00001111  15  00010000  16  00100001  33  11111111  255 16

  17. Decimal Addition 1) Add 8 + 7 = 15 Add 3758 to 4657: Write down 5, carry 1 2) Add 5 + 5 + 1 = 11 1 1 1 Write down 1, carry 1 3 7 5 8 3) Add 7 + 6 + 1 = 14 + 4 6 5 7 Write down 4, carry 1 8 4 1 5 4) Add 3 + 4 + 1 = 8 Write down 8 17

  18. Binary Addition Rules:  0 + 0 = 0  0 + 1 = 1  1 + 0 = 1 (just like in decimal)  1 + 1 = 2 10 = 10 2 = 0 with 1 to carry  1 + 1 + 1 = 3 10 = 11 2 = 1 with 1 to carry 18

  19. Binary Addition … Col 1) Add 1 + 0 = 1 Write 1 Example 1: Add Col 2) Add 1 + 0 = 1 binary 110111 to 11100 Write 1 Col 3) Add 1 + 1 = 2 ( 10 in binary) 1 1 1 1 Write 0 , carry 1 1 1 0 1 1 1 Col 4) Add 1+ 0 + 1 = 2 + 0 1 1 1 0 0 Write 0 , carry 1 Col 5) Add 1 + 1 + 1 = 3 ( 11 in binary) 1 0 1 0 0 1 1 Write 1 , carry 1 Col 6) Add 1 + 1 + 0 = 2 Write 0 , carry 1 Col 7) Bring down the carried 1 19 Write 1

  20. Binary Addition … Verification You can always check your 110111 2  answer by converting the 55 10 +011100 2 + 28 10 figures to decimal, doing the 83 10 addition, and comparing the answers. 64 32 16 8 4 2 1 1 1 0 1 1 1 1 0 1 0 0 1 1 = 64 + 16 + 2 +1 + 0 1 1 1 0 0 = 83 10 1 0 1 0 0 1 1 20

  21. Decimal Subtraction Try to subtract 5 – 7  can’t. 1) Subtract Must borrow 10 from next column. 4657 from 8025 : Add the borrowed 10 to the original 5. Then subtract 15 – 7 = 8 . Try to subtract 1 – 5  can’t. 2) 1 7 9 1 Must borrow 10 from next column. 8 0 2 5 But next column is 0, so must go to 1 1 column after next to borrow. - 4 6 5 7 Add the borrowed 10 to the original 0. Now you can borrow 10 from this column. 3 3 6 8 Add the borrowed 10 to the original 1.. Then subtract 11 – 5 = 6 3) Subtract 9 – 6 = 3 4) Subtract 7 – 4 = 3 21

  22. Decimal Subtraction … 8 0 2 5 - 4 6 5 7 3 3 6 8  So when you cannot subtract, you borrow from the column to the left.  The amount borrowed is 1 base unit, which in decimal is 10.  The 10 is added to the original column value, so you will be able to subtract. 22

  23. Binary Subtraction • In binary, the base unit is 2 • So when you cannot subtract, you borrow from the column to the left. • The amount borrowed is 2. • The 2 is added to the original column value, so you will be able to subtract. 23

  24. Binary Subtraction … Col 1) Subtract 1 – 0 = 1 Col 2) Subtract 1 – 0 = 1 Example 1: Subtract Col 3) Try to subtract 0 – 1  can’t. binary 11100 from 110011 Must borrow 2 from next column. But next column is 0, so must go to column after next to borrow. 1 2 Add the borrowed 2 to the 0 on the right. 0 0 2 2 Now you can borrow from this column (leaving 1 remaining). 1 1 0 0 1 1 Add the borrowed 2 to the original 0. Then subtract 2 – 1 = 1 - 1 1 1 0 0 Col 4) Subtract 1 – 1 = 0 Col 5) Try to subtract 0 – 1  can’t. 1 0 1 1 1 Must borrow from next column. Add the borrowed 2 to the remaining 0. Then subtract 2 – 1 = 1 24 Col 6) Remaining leading 0 can be ignored.

  25. Binary Subtraction … Verification Subtract binary 110011 2  51 10 11100 from 110011 : - 11100 2 - 28 10 1 2 23 10 0 0 2 2 64 32 16 8 4 2 1 1 1 0 0 1 1 1 0 1 1 1 - 1 1 1 0 0 = 16 + 4 + 2 + 1 = 23 10 1 0 1 1 1 25

  26. One’s and Two’s Complement  Consider only numbers in a range  E.g., five- digit car odometer: 0, 1, …, 99999  E.g., eight- bit numbers 0, 1, …, 255  Roll-over when you run out of space  E.g., car odometer goes from 99999 to 0, 1, …  E.g., eight- bit number goes from 255 to 0, 1, … 26

  27. One’s and Two’s Complement …  One’s complement: flip every bit  E.g., b is 01000101 (i.e., 69 in base 10)  One’s complement is 10111010  That’s simply 255 -69  Subtracting from 11111111 is easy (no carry needed!) 1111 1111 - 0100 0101 b one’s complement 1011 1010  Two’s complement  Add 1 to the one’s complement  E.g., (255 – 69) + 1  1011 1011 27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend