Top Banner

of 25

C program translation.ppt

Apr 14, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/27/2019 C program translation.ppt

    1/25

    Translating C programs into assembly

    1

  • 7/27/2019 C program translation.ppt

    2/25

    Program TranslationC is closer to assembly than almost any other language

    Arithmetic/logic/bitwise operations resemble machineoperations

    The use of pointers corresponds to memory access atmachine level

    No high-level language complex elements

    2

  • 7/27/2019 C program translation.ppt

    3/25

    Program TranslationFor now, usepseudo assembly instructions

    Will see full details of assembly

    Use of registers:

    Denote registers as r0, r1, r2,

    Registers can hold byte, half-word (short), word

    (integer) r0 holds zero

    3

  • 7/27/2019 C program translation.ppt

    4/25

    Program TranslationMemory operations:

    Load: Load a memory variable at a given memoryaddress to a register

    Store: Store a register value into a memory variable ata give memory address

    No other operations are allowed on memory variables

    load r1, &a ; r1

  • 7/27/2019 C program translation.ppt

    5/25

    Program TranslationData operations:

    Add+, sub -, mul*, div /, and&, or|,xor^, shift-left

    Can use two registers

    Can use one register, one constant

    Format:

    add r1, r2, r3add r1, r1, 100

    5

  • 7/27/2019 C program translation.ppt

    6/25

    Program TranslationCompare instruction: cmp

    Can compare two registers

    Can compare one register, one constant Cannot use memory

    cmp r1, r2

    cmp r1, 0

    6

  • 7/27/2019 C program translation.ppt

    7/25

    Program TranslationBranch instructions: non-sequential control flow.

    (1) Branch condition-when (2) Branch target-where

    Should follow a compare instruction beq: branch if equal

    bne: branch if not equal

    blt: branch if less than

    ble: branch if less than or equal

    cmp r1, 0

    beqlabel

    7

  • 7/27/2019 C program translation.ppt

    8/25

    Arithmetic ExpressionsExercise:

    a = b;

    a = 10;

    8

  • 7/27/2019 C program translation.ppt

    9/25

    Arithmetic ExpressionsExercises:

    c = a + b;

    e = (a & b) | (c & d);

    9

  • 7/27/2019 C program translation.ppt

    10/25

    Translating ifstatementC ifstatement:

    if (cond_expr)then_statement

    else

    else_statement

    10

  • 7/27/2019 C program translation.ppt

    11/25

    Translating ifstatementgoto version:

    t = cond_expr;

    if (!t) goto false;then_statement

    goto done;

    flase:

    else_statementdone:

    11

  • 7/27/2019 C program translation.ppt

    12/25

    Translating ifstatementExercise:

    if (x > y)

    max = x;else

    max = y;

    1. Writegoto version

    2. Translate into pseudo-assembly

    12

  • 7/27/2019 C program translation.ppt

    13/25

    Translating ifstatementExercise:

    if (x > 0)

    x = 0;1. Writegoto version

    2. Translate into pseudo-assembly

    13

  • 7/27/2019 C program translation.ppt

    14/25

    Translating do-while LoopC do-while statement:

    do

    body_statementwhile (cond_expr);

    14

  • 7/27/2019 C program translation.ppt

    15/25

    Translating do-while LoopGoto version:

    loop:

    body_statement

    t = cond_expr;

    if (t) goto loop;

    15

  • 7/27/2019 C program translation.ppt

    16/25

    Translating while LoopExercise: Calculate the parity bit of n

    m = n;

    parity = 0;do {

    parity ^= (m & 1);

    m >>= 1;

    } while (m != 0);

    16

  • 7/27/2019 C program translation.ppt

    17/25

    Translating while LoopC while statement:

    while (cond_expr)body_statement

    17

  • 7/27/2019 C program translation.ppt

    18/25

    Translating while Loopgoto version 1:

    loop:

    t = cond_expr;

    if (!t) goto done;

    body_statement

    goto loop;

    done:

    18

  • 7/27/2019 C program translation.ppt

    19/25

    Translating while LoopGoto version 2:

    goto test;

    loop:body_statement

    test:

    t = cond_expr;if (t) goto loop;

    done:

    19

  • 7/27/2019 C program translation.ppt

    20/25

    Translating while LoopExercise: Calculate the parity bit of n

    m = n;

    parity = 0;while (m != 0) {

    parity ^= (m & 1);

    m >>= 1;

    }

    20

  • 7/27/2019 C program translation.ppt

    21/25

    TranslatingforLoopC for statement:

    for (init_expr; cond_expr; update_expr)

    body_statement

    21

  • 7/27/2019 C program translation.ppt

    22/25

    TranslatingforLoopTranslated to while statement:

    init_expr;

    while (cond_expr) {body_statement

    update_expr;

    }

    22

  • 7/27/2019 C program translation.ppt

    23/25

    Translating for LoopGoto version:

    23

  • 7/27/2019 C program translation.ppt

    24/25

    TranslatingforLoopExercise: Calculate the sum of X[N]

    sum = 0;

    for (i = 0; i < N; i ++) {sum += X[i];

    }

    1. Writegoto version

    2. Translate into pseudo-assembly

    24

  • 7/27/2019 C program translation.ppt

    25/25

    Memory PointersHow to use pointers?

    int *pInt = &m;

    *pInt = m;

    25