how does variable store in memory address

Discussion in 'Programmer's Corner' started by vead, May 10, 2017.

  1. vead

    Thread Starter Well-Known Member

    Nov 24, 2011
    712
    11
    Hello
    here is c program for LED blinking. I have tested this code
    Code (Text):
    1.    #include<regx51.h>
    2.     #define LED               P2
    3.     #define LED_ON       0xff
    4.     #define LED_OFF     0x00
    5.  
    6.     void delay (unsigned int i)
    7.     {
    8.         unsigned int j;
    9.         for (j = 0; j < 40000; j++);
    10.         {
    11.        
    12.         }
    13.     }
    14.  
    15.     void main()
    16.     {
    17.    
    18.       while (1)
    19.         {
    20.             LED = LED_ON;
    21.             delay(500);
    22.             LED = LED_OFF;
    23.             delay(5000);
    24.             LED = LED_OFF;
    25.        
    26.         }
    27.     }
    28.  
    I don't understand this part of code
    Code (Text):
    1.  void delay (unsigned int i)
    2.     {
    3.         unsigned int j;
    4.         for (j = 0; j < 40000; j++);
    5.         {
    6.        
    7.         }
    I understand j is variable. how does this part of code work on microcontroller ?
     
  2. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    6,530
    1,521
    It starts with j at zero and adds one to it repeatedly until j gets to 39999 and then stops. It just wastes some time resulting in a delay.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    23,093
    6,946
    It's the most basic kind of delay loop (hence the name of the function).

    Note that the pair of braces after it are meaningless and are not even part of the loop. The person that wrote the code might or might not know that; they might have put them in there as a visual clue that the loop is empty. It's not very good style because it lends itself to two problems. First, if they decide to do something in that loop, such as print out the value of j to verify that things are working, anything in the braces is not part of the loop no matter how it might look. Second, someone reading the code (including many compilers) will conclude that the semicolon after the for statement is a logic error and be tempted to remove it (or throw a warning, in the case of a compiler).

    Also, note that the function takes an argument, i, but then doesn't use it. Yet look at where the function is called -- it is called with two different values for i; presumably the intent is that the delay caused by the second loop is ten times as long as the delay caused by the first, yet both calls will produce the exact same delay. A better way to write this function would be

    Code (Text):
    1.  
    2. void delay (unsigned int i)
    3. {
    4.    unsigned int j;
    5.    for (j = 0; j < i; j++)
    6.    {
    7.        // EMPTY LOOP
    8.    }
    9. }
    10.  
    I actually have a #define for this case

    Code (Text):
    1.  
    2. #define EMPTY_LOOP
    3.  
    4. void delay (unsigned int i)
    5. {
    6.    unsigned int j;
    7.    for (j = 0; j < i; j++)
    8.       EMPTY LOOP;
    9. }
    10.  
     
    vead likes this.
  4. vead

    Thread Starter Well-Known Member

    Nov 24, 2011
    712
    11
    Thanks for your quick response. actually I am asking , which part of microcontroller work for this code counter or memory registers. how does it work during the execution of program?
     
  5. WBahn

    Moderator

    Mar 31, 2012
    23,093
    6,946
    That depends on how the code generator for the particular compiler works and what hardware resources the particular microcontroller has available.

    One of the risks of this code is that many newer compilers are likely to recognize that the code doesn't perform any computation and therefore may optimize it away completely. Compilers intended for embedded systems are less likely to do this in part because they traditionally haven't been as good at code optimization but also the compiler writer is more likely to be aware that programmers use these kinds of tricks for purposes other than computation.
     
  6. vead

    Thread Starter Well-Known Member

    Nov 24, 2011
    712
    11
    I am trying to understand delay function regarding to hardware. I see in every program there is always delay function I am having problem to understand part of code for delay . actually I don't understand how does this part of code work on microcontroller. I use 8051 (p89v51rd2) and keil compiler
     
  7. MrChips

    Moderator

    Oct 2, 2009
    17,092
    5,287
    There are various ways to implement delay functions on a computer, some in software, some in hardware.

    What you have shown are software solutions that simply waste computer processing time.

    The code sits in a loop executing dummy instructions for a set number of times chewing up computer cycles. No other computer tasks are processed during this time (except if there are interrupting events also being processed).
     
  8. WBahn

    Moderator

    Mar 31, 2012
    23,093
    6,946
    Do you understand how the following loop would be implemented?

    Code (Text):
    1.  
    2. unsigned int j;
    3. j = 0;
    4. while (j < 40000)
    5. {
    6.    j++;
    7. }
    8.  
    If so, then you understand how the delay loop works because this IS the delay loop.
     
  9. DickCappels

    Moderator

    Aug 21, 2008
    4,959
    1,510
    In an 8051 most likely the compiler would associate the variable "j" with a particular RAM or register location (or some number of RAM or register locations depending upon how many bytes "j" occupies). It would first set the value in those registers/memory locations to zero initially and then increment and test the contents of those registers/memory locations at each pass through the loop.

    As WBahn has noted a smart (or optimizing) compiler would recognize that this loop doesn't do anything and not even implement it.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    17,092
    5,287
    Are you asking how the 8051 chip executes this code in hardware?
    Code (Text):
    1.          MOV A, #255
    2. goback:  DEC A
    3.          JNZ goback
    Do you understand what the code above does?
     
  11. vead

    Thread Starter Well-Known Member

    Nov 24, 2011
    712
    11
    suppose j has memory address 0000 1001 and initially it has value 0000 0000 and then it increment 0000 0001- 0000 0010- 0000 0100---------- 00004000 when it reach 0000 0000 again increment from 0000 0000 to 0000 4000. am I getting something ?
     
  12. WBahn

    Moderator

    Mar 31, 2012
    23,093
    6,946
    am I getting something ?[/QUOTE]

    What's special about 0000 4000?

    When it no longer passes the test (being less than 40,000 in decimal) it fails the test and exits the loop. It does not reach 0000 0000 again.

    Not as near as I can tell.
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,852
    3,007

    You are over thinking this. All you need to know is that this is a basic loop. It will run machine instructions i times. So if you call delay(50) the loop will run 50 times. If you call delay(100) it will run 100 times or take approximately twice the time as delay(50) .
     
  14. WBahn

    Moderator

    Mar 31, 2012
    23,093
    6,946
    Not the code the TS posted. Calling delay(100) will take exactly the same amount of time as delay(50) -- in either case it will run the loop 40,000 times.
     
    ErnieM likes this.
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,852
    3,007

    Oh rats I had the code you posted on my brain. Either way the TS is over thinking this. It is not that complicated of a concept.
     
  16. DickCappels

    Moderator

    Aug 21, 2008
    4,959
    1,510
    As you can see opinions vary, but I think you are starting to get it.
     
Loading...