Thread: a simple question

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    a simple question

    hello friends,

    I have a question,

    Let there be a expression

    g = a/2 + b*4/a - abc/3

    the value of variables is given, let a =10, b = 9, abc = 6

    Now I want to know when this expression is calculated in a C program,

    the computer has to access the values of the variables, does it replaces all the variables by their values in one go before solving the expression

    or

    it first take a particular operator ( like the first /) and accesses the values of the operands, then takes second operator and its operands values is accessed.

    Thanks a lot.
    Last edited by scorpion_best; 08-02-2007 at 05:34 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Huh,If you're trying to parse it http://en.wikipedia.org/wiki/Binary_tree
    Otherwise http://catb.org/~esr/faqs/smart-questions.html

    Although you haven't actually asked anything!?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, I'd say that it's "both" or "neither", depending on how you look at it.

    If the compiler can figure out that the variable isn't changing, it will calculate the result then and there - e.g. in the below code, it will assign the constant 19 to g[i].

    Code:
    int a = 10;
    int b = 9;
    int i;
    int g[10];
    for(i = 0; i < 10; i++)
      g[i] = a + b;
    In a case where the compiler can't determine that the variables are staying the same value, it will read the value from the memory location that belongs to that variable. There is no strict rule about which order it FETCHES the memory content (the compiler may decide to pre-load the variable a into register R1, then use it for both the divide and add operation from that register. Or it may fetch it at the divide, and then fetch it again when it adds the variable - if you want to GUARANTEE that the variable is accessed each time, you need to declare it using "volatile".

    In some processors, data can be accessed by for example add operations IN the memory location, so it doesn't need to be fetched into a register. Other processors the data MUST be in a register when it's being used for math operations. x86 (AMD and Intel processors) can do both memory and register based operations, but if the same variable is used multiple times, it will be slightly faster to run the code if it's in a register (and the code will be a little bit smaller that way, which isn't a bad thing).

    --
    Mats

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Some of those decisions are left up to the compiler as to how to evaluate an expression. You know the result and how it must get there from a mathematical and a C standpoint, though.

    This is how LCC broke down your line of code into assembly without optimizations:

    Code:
    ;    7  g = a/2 + b*4/a - abc/3;
            .line   7
            movl    -8(&#37;ebp),%edi
            movl    %edi,%eax
            movl    $2,%ecx
            cdq
            idivl   %ecx
            movl    %eax,-20(%ebp)
            movl    -12(%ebp),%esi
            movl    %esi,%eax
            sall    $2,%eax
            movl    %edi,%ecx
            cdq
            idivl   %ecx
            movl    -20(%ebp),%edi
            addl    %eax,%edi
            movl    -16(%ebp),%eax
            movl    $3,%ecx
            cdq
            idivl   %ecx
            subl    %eax,%edi
            movl    %edi,-4(%ebp)
    1. It loads a first, divides it by 2, and stores the result in a temp variable.
    2. It loads b into memory, fast multiplies by 4, divides by a, stores the result into the temp variable, and then adds the two temp values together.
    3. It moves the last variable, abc, divides by 3, subtracts it from the result of the addition of the previous two temp values, and finally sets G to the value.


    This is all without optimizations, and it's rather a silly way to write it into assembly, but it works, and it's easy enough to explain.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    aww pretty colours, but I don't think that's what he's looking for. Well guesswork really

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by zacs7 View Post
    aww pretty colours, but I don't think that's what he's looking for. Well guesswork really
    Who cares what he's looking for? I had a chance to post color-coded assembly code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM