40
Machine Code, Number System, and and C Variables and Operations A. Sahu amd S. V .Rao Dept of Comp. Sc. & Engg. Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati 1

Lec03 MC NS CP · 2017. 4. 12. · Title: Microsoft PowerPoint - Lec03_MC_NS_CP Author: abs Created Date: 1/18/2017 4:06:12 AM

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • Machine Code, Number System,andand 

    C Variables and Operations

    A. Sahu amd S. V .RaoDept of Comp. Sc. & Engg.Dept of Comp. Sc. & Engg.

    Indian Institute of Technology Guwahati

    1

  • Outline• Compiler Phases for C Programming

    –Compiler, Assembler, Linker, Loader–Source, Assembly, Object, Executable , y, j ,

    • Number SystemBi O t l d H–Binary, Octal and Hex

    • Flow Charts• C Programming: Variable, data type and operationsand operations

    2

  • Machine code andMachine code and CompilationCompilation

    3

  • Compiling program: Object file, bl f l f lassembly file, exe file

    $gcc –S test.cG t bl l filGenerate assembly language  file .s

    $ gcc test.s$

    Source, Assembly TXT$gcc –c test.c

    Generate object file TXT

    $gcc test.oGenerate executable file

    Object, EXEBinary

    $./a.outrun the executable

    4

  • Compilation flow

    test.c C Source file 

    gcc test.cgcc ‐S test.c

    t ttest.s

    gcc ‐c test.sgcc ‐c test.c

    ASM file 

    gcc test.sgcc c test.s

    Test.o

    gcc c test.c

    Obj file 

    gcc test.s

    gcc ‐c test.o

    5a.out

    Exe file 

  • Human‐Readable Machine LanguageHuman Readable Machine Language• Computers like ones and zeros…

    0001110010000110• Humans like symbols…

    ADD R6 R2 R6; increment index reg

    0001110010000110

    • Assembler is a program that turns symbols intomachine instructions.

    ADD R6,R2,R6; increment index reg.

    – ISA‐specific: Close correspondence between symbols and instruction set

    M i f d• Mnemonics for opcodes• Labels for memory locations

    – additional operations for allocating storage and

    7‐6

    additional operations for allocating storage and initializing data

  • Object File Format• Object file contains

    – Starting address (location where program must be loaded), followed by Machine instructions

    • Example– Beginning of “count character” object file looks like this:

    00110000000000000101010010100000

    .ORIG x3000

    AND R2, R2, #0

    LD R3 PTR00100110000100011111000000100011

    LD R3, PTR

    TRAP x23

  • Assembly Language: Human Readable%

    include 'system.inc'include system.inc section .data hello db 'Hello, World!',

    0Ah hb tes eq $ hello0Ah hbytes equ $-hello section .text global _start _start:

    push dword hbytespush dword hellopush dword hello push dword stdoutsys.writepush dword 0

    $nasm -f elf hello.asmpush dword 0

    sys.exit$ld -s -o hello hello.o$./hello

    8

    Hello, World!

  • Language LevelsLanguage LevelsHigh Level Language A

    S

    Assembler Language

    BST

    SP

    Assembler Language TRA

    EE

    Machine Language ATI

    D

    Micro ‐programming „Firmware“ ON

    9

    Hardware

  • High Level to Micro CodeHigh Level to Micro Code• High Level language

    – Formulating program for certain application areas

    –Hardware independent• Assembler languages

    –Machine oriented language–Programs orient on special hardware g pproperties

    –More comfortable than machine code 

    10(e.g. by using symbolic notations)

  • High Level to Micro CodeHigh Level to Micro Code• Machine code:

    –Set of commands directly executable via CPUvia CPU

    –Commands in numeric code–Lowest semantic level–Generally 2 executing oportunities:Generally 2 executing oportunities:

    • Interpretiv via micro codeDi l i i h d

    11

    • Directly processing via hardware

  • High Level to Micro CodeHigh Level to Micro Code• Micro programming:

    – Implementing of executing of machine commands (Control unit ‐ controller)

    –Machine command executed/shown as sequence of micro code commandssequence of micro code commands

    –Micro code commands:Si li t t lli• Simpliest process controlling

    –Moving of data, Opening of grids, Tests

    12

  • Number System y(binary, octal, dec and 

    hexa decimal)

    13

  • Computer: Number SystemComputer: Number System  • Computers like ones and zeros…

    • Humans like symbols…0001110010000110

    Humans like symbols…• We need to know: binary number system 

    Also Octal and Hex number system– Also Octal and Hex number system– Type conversions

    7‐14

  • Famous Number SystemFamous Number System• Decimal System: 0 ‐9

    –May evolves: because human have 10 finger • Roman Systemy

    –May evolves to make easy to look and feel–Pre/Post Concept: (IV V & VI) is (5‐1 5 &–Pre/Post Concept: (IV,  V  & VI) is (5‐1, 5 & 5+1) 

    • Binary System Others (Oct Hex)• Binary System, Others (Oct, Hex)–One can cut an apple in to two

    15

  • Significant DigitsSignificant Digits

    Binary: 11101101Binary: 11101101

    Most significant digit Least significant digitMost significant digit                   Least significant digit

    Decimal: 1063079Decimal:  1063079

    Most significant digit Least significant digitMost significant digit                   Least significant digit

  • Decimal (base 10)Decimal (base 10)• Uses positional representation• Each digit corresponds to a power of 10 based on its position in the number10 based on its position in the number

    • The powers of 10 increment from 0, 1, 2, etc. as you move right to left–1 479 = 1 * 103 + 4 * 102 + 7 * 101 + 9 *–1, 479 = 1   10 + 4   10 + 7   10 + 9   100

  • Binary (base 2)Binary (base 2) • Two digits: 0, 1• To make the binary numbers more readable, the digits are often put in groups of 4–1010 = 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 1010   1   2 + 0   2 + 1   2 + 0   2

    = 8 + 2 = 10 10 

    –1100  1001 = 1 * 27 + 1 * 26 + 1 * 23 + 1 * 20 = 128 + 64 + 8 + 1= 128 + 64 + 8 + 1 = 201

  • How to Encode Numbers: Binary NumbersNumbers

    • Working with binary numbersI b t h l t k f 10–In base ten, helps to know powers of 10• One, Ten, Hundred, Thousand, ...

    –In base two, helps to know powers of 2• One, Two, Four, Eight, Sixteen, .., , , g , ,• Count up by powers of two

    24 23 22 21 2029 28 27 26 25 24 23 22 21 2029 28 27 26 25a

    16 8 4 2 1512 256 128 64 32

  • Important Property of Binary NumberImportant Property of Binary Number• Number of different number can be possible for  a N bit binary numbera N bit binary number – 2N, for 2 bit number it is 4  (00, 01,10 and 11)S ti t N bit bi b t• Summation two N bit binary number cannot exceed 2*2N

    • 20+21+22+23+…+2N=2N+1‐1 example– 1+2+4+8=15=16‐1

  • Octal (base 8)Octal (base 8)• Shorter & easier to read than binary8 di it 0 1 2 3 4 5 6 7• 8 digits: 0, 1, 2, 3, 4, 5, 6, 7, 

    • Octal numbers to DecimalOctal numbers  to Decimal1368 = 1 * 82 + 3 * 81 + 6 * 80 8

    = 1 * 64 + 3 * 8 +  6 * 1= 94= 9410

  • Hexadecimal (base 16)Hexadecimal (base 16)• Shorter & easier to read than binary• 16 digits: 

    –0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F• “0x” often precedes hexadecimal numbers

    0x123 = 1 * 162 + 2 * 161 + 3 * 160 = 1 * 256 + 2 * 16 + 3 * 1 1   256   2   16    3   1= 256 + 32 +  3291= 291 

  • CountingDec Binary Oct Hex Dec Binary Oct Hex

    0 00000 0 0 8 01000 10 8

    1 00001 1 1 9 01001 11 9

    2 00010 2 2 10 01010 12 A

    3 00011 3 3 11 01011 13 B

    4 00100 4 4 12 01100 14 C

    5 00101 5 5 13 01101 15 D

    6 00110 6 6 14 01110 16 E

    7 00111 7 7 15 01111 17 F

    8 01000 10 8 16 10000 20 10

    23

  • Fractional NumberFractional Number • Point: Decimal Point, Binary Point, Hexadecimal point

    • Decimal247.75   = 2x102+4x101+7x100    +   7x10‐1+5x10‐2

    • BinaryBinary10.101= 1x21+0x20   +  1x2‐1+0x2‐2+1x2‐3

    • Hexadecimal• Hexadecimal6A.7D=6x161+10x160  +  7x16‐1+Dx16‐2

  • Converting To and From DecimalConverting To and From Decimal

    Successive Division

    Weighted M lti li ti

    Decimal100 1 2 3 4 5 6 7 8 9

    Weighted Multiplication

    Successive Division

    Multiplication

    Successive Di i i

    Weighted M lti li ti

    Octal80 1 2 3 4 5 6 7

    Hexadecimal160 1 2 3 4 5 6 7 8 9 A B C D E F

    Division Multiplication

    BinaryBinary20 1

    25

  • Decimal↔ BinaryDecimal ↔ Binary

    SuccessiveSuccessiveDivision

    a) Divide the decimal number by 2; the remainder is the LSB of the binary) y ; ynumber.

    b) If the quotation is zero, the conversion is complete. Otherwise repeat step (a) using the quotation as the decimal number. The new remainder is the ( ) g qnext most significant bit of the binary number.

    Weighted

    a) Multiply each bit of the binary number by its corresponding bit‐

    Multiplication

    weighting factor (i.e., Bit‐0→20=1; Bit‐1→21=2; Bit‐2→22=4; etc). 

    b) Sum up all of the products in step (a) to get the decimal number.  

  • Decimal to Binary : Division Methody• Divide decimal number by 2 and insert remainder into new binary number.y– Continue dividing quotient by 2 until the quotient is 0.

    • Example: Convert decimal number 12 to binary

    12 div 2  =   ( Quo=6 , Rem=0)   LSB6 div 2  =    (Quo=3, Rem=0)3 div 2 = (Quo=1,Rem=1)3 div 2        (Quo 1,Rem 1)1 div 2  =   ( Quo=0, Rem=1)   MSB 

    1210= 1 1 00 2 

  • Decimal to Octal ConversionDecimal to  Octal ConversionThe Process: Successive Division• Divide number by 8; R is the LSB of the octal number• Divide  number by 8;  R is the LSB of the octal number • While Q is 0 

    U i th Q th d i l b• Using the Q as the decimal number. • New remainder is MSB of the octal number.

    11

    1

    LSB 6 r 11 94 8 ←=

    0

    3 r 1 11 8 = 9410 = 1368

    MSB 1 r 0 1 8 ←= 28

  • Decimal to Hexadecimal ConversionDecimal to  Hexadecimal Conversion

    The Process: Successive Division• Divide  number by 16;  R is the LSB of the hex

    number • While Q is 0 

    • Using the Q as the decimal number.  • New remainder is MSB of the hex number.

    LSBE 5

    9416

    0

    LSB E r 94 16 ←=

    9410 = 5E16MSB 5 r

    0 5 16 ←=

    10 16

  • Substitution CodeSubstitution Code

    Convert 1110 0110 10102 to hex using the 4‐bit substitution code :

    E 6 A

    1110  0110  1010

    E 6 A

    E6A16

  • Flow chart and ProblemFlow chart and Problem solvingsolving

    31

  • Computing in this courseComputing in this course • Given a Problem : 

    – In English description • Solve using Computer

    – Design methods to solve the problem – Analyze the designed solution for correctnessy g– Design flow chart to for the design method to solve the problem

    – Write Pseudocode/source code– Compile and rule the code : You may get  some error p y g– Test the code (with some Input): you get some error

    32

  • Computing in this courseComputing in this course • Given a Problem : 

    Computing– In English description • Solve using Computer

    Computing 

    – Design methods to solve the problem – Analyze the designed solution for correctnessy g– Design flow chart to for the design method to solve the problem ProgrammingProgramming

    – Write Pseudocode/source code– Compile and rule the code : You may get  some error p y g– Test the code (with some Input): you get some error

    33

  • Problem Solving Example 1• Given a positive integer, is this number  divisible by 3?y

    • Method 1Di id h b b h d f h–Divide the number by three and test for the reminder• Repeated substation of  3 till you get the result less than 3

    • Divide using usual method and get reminder

    34

  • Problem Solving Example 1• Given a positive integer, is this number divisible by 3?

    • Method 2 – Sum all the digits of the number and test theSum all the digits of the number and test the divisibility of Sum by three, repeat the same.  

    – If sum is divisible by three then number is divisible yby three. How you got to know?

    – Some one must have solved this problem and proved the correctness of the solution.   • Solution will work for all the cases

    35

  • Problem Solving Example 2• Problem: Searching for your friend

    – Your friend is near to square and you are exactly q y yat the square

    – You don’t know which direction and how much distance from the square

    – You need to find your friend Friend

    • Any Solution?– How you will search him?How you will search him?– Any approachThink for 2 3 minutes you– Think for 2‐3 minutes

    36

    you

  • Problem Solving Example 2• Problem: Searching for your friend

    – You don’t know which direction and how much distance from the square

    • Solution?– K=1;– While(not found)

    Friend

    • For all directions – Go  K meter and return to square

    • K=k+1;K k 1;

    • Analysis :Total distance covered – 4*2(1+2+ +X)=8*X*(X+1)/2=4X2+4X you– 4 2(1+2+..+X)=8 X (X+1)/2=4X +4X

    37

    you

  • Problem Solving Example 2• Problem: Searching for your friend

    – You don’t know which direction and how much di t f thdistance from the square

    • Better Solution? YESK 1– K=1;

    – While(not found)• Go in Next directions

    Friend

    Go in Next directions – Go  K meter and return to square

    • K=k*2;

    A l i di t d• Analysis: distance covered – At max  (1+2+22+23+..+X+2X+4X+8X)

    < 16*X < 4X2+4X (earlier solution) you

  • Computing in this courseComputing in this course • Given a Problem : 

    Computing– In English description • Solve using Computer

    Computing 

    – Design methods to solve the problem – Analyze the designed solution for correctnessy g– Design flow chart to for the design method to solve the problem ProgrammingProgramming

    – Write Pseudocode/source code– Compile and rule the code : You may get  some error p y g– Test the code (with some Input): you get some error

    39

  • Thanks

    40