#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    When does the loop terminate? ?


    Hi ,

    I am developing software driver for NOR Flash EEPROM.I was searching in internet for available drivers, i came across the followed link:

    http://
    read.pudn.com/downloads91/sourcecode/embed/348700/orion_ep93xx_wince_bsp_1-3-706/orion_ep93xx_wince_bsp_1-3-706/edb9301/eboot/intelj3_1x16.c__.htm

    While i was going through write flash function:

    BOOL WriteFlashJ3_1x16(DWORD dwDest, DWORD dwSource, DWORD dwLength);

    I have got one doubt may be i might have analysed wrongly , i could not understand how and when we will come out of the loop.

    In the link Code Start Line No : 403 and Code End Line No:409

    ulEndCount = ulCount + FLASH_BUFFER_SIZE;
    do
    {
    pusDest[ulCount>>1] = pusSource[ulCount>>1];
    ulCount += sizeof(USHORT);
    } while(ulCount < ulEndCount);


    Because the loop runs while the ulCount is lesser than ulEndCount.But when i observed i found that ulCount is always lesser than ulEndCount.

    So When does the loop terminate??


    Thanks
    Sree
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    You more or less answered it self: "the loop runs while the ulCount is lesser than ulEndCount"

    So when ulCount gets equal or greater it will stop the loop.

    Where in the code did you check the ulCount? after the loop?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    When does the loop terminate ??


    Hi Fuji,
    Thanks for the response.


    value of sizeof(USHORT)= 2 (If i am not wrong)

    And if you observe the code ulCount is assigned "0" value.And FLASH_BUFFER_SIZE=32,

    So now

    ulEndCount = 0 (ulCount) +32 (FLASH_BUFFER_SIZE);
    do
    {
    pusDest[0>>1]=pusSource[0>>1];
    ulCount+=sizeof(USHORT);
    }
    while(ulCount<ulEndCount);


    After first loop iteration:.
    ulCount += sizeof(USHORT); is as followed

    ulCount= 0+2=2;

    so since the value of ulCount (2) is lessthan ulEndCount(32)
    loop is executed again

    and it will be right shifted to 1 bit position in the next loop iteration and now the value becomes 1 ( 2=0010 and when shifted 0010 >>1 = 0001)


    and again ulCount= 1 (ulCount=1 after first iteratio) + 2(sizeof(USHORT));
    So now ulCount=3;

    Even now also the value of ulCount (3) is lessthan ulEndCount(32)
    so loop is executed again

    and it will be right shifted to 1 bit position in the next loop iteration and now the value becomes 1 ( 3=0011 and when shifted 0011 >>1 = 0001)

    Thus ulCount is always 1 & 3 and is not going to increase

    so my question is when does the loop terminate ?


    Thanks
    Sree
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    Would recommend that you try to insert some (debug) print call to check what the value of ulCount really is.
    Or is all the values you posted a output from that kind of test?

    >> alone should not save any value back into the variable (ulCount), for that effect you will need to write it like >>=

IMN logo majestic logo threadwatch logo seochat tools logo