stack overflow problem

Discussion in 'Embedded Systems and Microcontrollers' started by siangwei, Jan 5, 2011.

  1. siangwei

    Thread Starter New Member

    Jan 5, 2011
    6
    0
    Hi all

    I am new in assembly language..
    my project is about IC functional test which test TTL74

    the problem occur when i runing simulation on proteus and MPLAB SIM..

    before i replace the G_OR function with its full test coding..all work fine..after i replace the full coding of OR gate..this problem occur!

    i was busy finding the CALL and RETURN instruction but i cant find any wrong there..PLEASE HELP..

    thank you all

    attachment is the code and proteus circuit file
     
    Last edited: Jan 5, 2011
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You are getting problems because the code is over 2Kbytes in size - calls stop working because of the quoted reason:

    To find the problem you just look at the error:
    CORE-E0001: Stack over flow error occurred from instruction at 0x00001f

    and the line 1F in the disassembly listing:
    You can then see that address 0x06 is not where the call is intended to go.
    (It will still say 0x06 after you have made the below change but changing PCLATH fixes the problem).

    You can either change all the calls in the program as suggested below or optimise to reduce the code size below 2K.


    From AN556 downloadable from microchip.com
     
    Last edited: Jan 5, 2011
  3. siangwei

    Thread Starter New Member

    Jan 5, 2011
    6
    0
    Thanks your reply Markd77.

    it is imposible for me reduce my code below 2k..somemore i havent need to add in USART code.

    "SECTION 1
    CALL and GOTO Instructions
    When executing a CALL or GOTO, the low 11-bits are
    loaded directly from the instruction opcode. The high
    2-bits are loaded from bits 3 and 4 of the PCLATH reg-
    ister. It is a good practice to pre-load PCLATH with the
    high byte of the routine’s address before executing the
    routine. This can be done as follows:
    EXAMPLE 2:
    .
    .
    movlw HIGH Table ;load high 8-bit
    movwf PCLATH ;address of Table into PCLATH
    call Routine ;execute Call instruction
    SECTION 1
    CALL and GOTO Instructions
    When executing a CALL or GOTO, the low 11-bits are
    loaded directly from the instruction opcode. The high
    2-bits are loaded from bits 3 and 4 of the PCLATH reg-
    ister. It is a good practice to pre-load PCLATH with the
    high byte of the routine’s address before executing the
    routine. This can be done as follows:
    EXAMPLE 2:
    .
    .
    movlw HIGH Table ;load high 8-bit
    movwf PCLATH ;address of Table into PCLATH
    call Routine ;execute Call instruction
    SECTION 1
    CALL and GOTO Instructions
    When executing a CALL or GOTO, the low 11-bits are
    loaded directly from the instruction opcode. The high
    2-bits are loaded from bits 3 and 4 of the PCLATH reg-
    ister. It is a good practice to pre-load PCLATH with the
    high byte of the routine’s address before executing the
    routine. This can be done as follows:
    EXAMPLE 2:
    .
    .
    movlw HIGH Table ;load high 8-bit
    movwf PCLATH ;address of Table into PCLATH
    call Routine ;execute Call instruction
    "
    i dont get what it mean..can future explain it?
    thanks..
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This explains it in a different way, maybe it makes more sense.

    I can see quite a few optimizations that could reduce your code size, for example there are a lot of these:

    They could all be reduced to 1 call, which would save over 700 instructions.


    <ed> Also have a look at this, all the text to send is stored in the EEPROM and is sent using the message subroutine. It makes changes to the messages very easy and saves a lot of program memory.
    It has some USART code in which might be of use, but could need some tweaking for the different PIC model.
    http://forum.allaboutcircuits.com/showthread.php?t=40875
    <ed>


    [​IMG]
     
    Last edited: Jan 8, 2011
  5. siangwei

    Thread Starter New Member

    Jan 5, 2011
    6
    0
    From the file an225, i do not get it how its work and why should use this method..it mention about page1 and page2..how can we know whether the cide should put in page1 or page2??
    Then the pclath will contain 2bits of call and goto instruction..isnt important??
     
  6. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I'm not sure I can explain it any better than the datasheet, maybe someone else can. You start off in page 0 and you can't call anything in the other pages without changing PCLATH yourself.
    The safest way is to change every call and goto to this format:

    movlw HIGH Table ;load high 8-bit
    movwf PCLATH ;address of Table into PCLATH
    call Table ;execute Call instruction


    You've got plenty of space so I'd recommend doing it this way.

    There are ways of avoiding doing this to every single call and goto but it makes the code harder to maintain and debug and requires more understanding.
     
    siangwei likes this.
  7. siangwei

    Thread Starter New Member

    Jan 5, 2011
    6
    0
    thanks markd77 idea..can i ask..what is that "Table" for? inside the table what should i write? till now i still cant get how its work..thx for your helpful..
     
  8. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    "Table" is just an example.

    Here's a bit of your code:
    Code ( (Unknown Language)):
    1.             GOTO        G_EROR_B
    2.             MOVLW        A'6'
    3.             CALL        LCD_DISP
    4. G_EROR_B
    My (and Microchip's) recommendation would be to replace that with:
    Code ( (Unknown Language)):
    1.  
    2. [B]            movlw HIGH G_EROR_B
    3.             movwf PCLATH[/B]
    4.             GOTO        G_EROR_B
    5.  [B]           movlw HIGH LCD_DISP
    6.             movwf PCLATH[/B]
    7.             MOVLW        A'6'
    8.             CALL        LCD_DISP
    9. G_EROR_B
    Notice the placement of the second part in bold so that "W" contains the correct value.

    I'd still recommend optimising the code first as it will save work on this conversion and might avoid it altogether if you can keep the code under 2K. The 700 instruction saving I suggested earlier would make a good start.
     
    siangwei likes this.
  9. siangwei

    Thread Starter New Member

    Jan 5, 2011
    6
    0
    I finally get what you mean..thanks for your patience explaination..

    isnt for every CALL and GOTO..i need to add this 2 instruction:

    movlw HIGH xxx
    movwf PCLATH
    ???
     
  10. siangwei

    Thread Starter New Member

    Jan 5, 2011
    6
    0
    hi markd77
    i already try to add in the two instruction before all CALL and GOTO..
    but the overflow problem still occur..isnt the code is over 2k??
    of that can you please explain to me how to make the code in to 2nd pages? and how to call the subroutine in page 2nd page? thounsand thanks for your kindly help
     
  11. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    If you post your new code I can have a look at it and see if there are problems.
     
Loading...