newb question

larytet
2007-01-15
2013-03-12
  • larytet
    larytet
    2007-01-15

    Hello,
    At this point i do not have real 8051, so i try to use sdcdb
    Linux environment (Ubuntu)
    xdesk:~/megab/8051$ sdcc -v
    SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.6.0 #4309 (Sep 28 2006) (UNIX)

    this is the code (single C file)

    static unsigned char idleTask(unsigned char context)
    {
      context++;
      return context;
    }

    static void mainLoop()
    {
       unsigned char context = 0;
       while (1)
       {
         context = idleTask(context);
       }
    }

    void main()
    {
      mainLoop();
    }

    This is how i compile it
    sdcc --model-small --debug test_00.c (see assembler below)

    I run debugger 
    sdcdb test_00
    and get
    (sdcdb) run
    Starting program
    Stack underflow
    (sdcdb)
    I tried to add a breakpoint to the idleTask. idleTask() is called 3 times and after that i get "Stack underflow"

    May you help me with this one ?
    I read the SDCC manual. I will appreciate any additional links to 8051 tutorials/beginners guides.

    Thank you

    ;--------------------------------------------------------
    ; File Created by SDCC : FreeWare ANSI-C Compiler
    ; Version 2.6.0 #4309 (Sep 28 2006)
    ; This file generated Mon Jan 15 19:34:02 2007
    ;--------------------------------------------------------
        .module test_00
        .optsdcc -mmcs51 --model-small
       
    ;--------------------------------------------------------
    ; Public variables in this module
    ;--------------------------------------------------------
        .globl _main
    ;--------------------------------------------------------
    ; special function registers
    ;--------------------------------------------------------
        .area RSEG    (DATA)
    ;--------------------------------------------------------
    ; special function bits
    ;--------------------------------------------------------
        .area RSEG    (DATA)
    ;--------------------------------------------------------
    ; overlayable register banks
    ;--------------------------------------------------------
        .area REG_BANK_0    (REL,OVR,DATA)
        .ds 8
    ;--------------------------------------------------------
    ; internal ram data
    ;--------------------------------------------------------
        .area DSEG    (DATA)
    ;--------------------------------------------------------
    ; overlayable items in internal ram
    ;--------------------------------------------------------
        .area OSEG    (OVR,DATA)
    ;--------------------------------------------------------
    ; Stack segment in internal ram
    ;--------------------------------------------------------
        .area    SSEG    (DATA)
    __start__stack:
        .ds    1

    ;--------------------------------------------------------
    ; indirectly addressable internal ram data
    ;--------------------------------------------------------
        .area ISEG    (DATA)
    ;--------------------------------------------------------
    ; bit data
    ;--------------------------------------------------------
        .area BSEG    (BIT)
    ;--------------------------------------------------------
    ; paged external ram data
    ;--------------------------------------------------------
        .area PSEG    (PAG,XDATA)
    ;--------------------------------------------------------
    ; external ram data
    ;--------------------------------------------------------
        .area XSEG    (XDATA)
    ;--------------------------------------------------------
    ; external initialized ram data
    ;--------------------------------------------------------
        .area XISEG   (XDATA)
        .area HOME    (CODE)
        .area GSINIT0 (CODE)
        .area GSINIT1 (CODE)
        .area GSINIT2 (CODE)
        .area GSINIT3 (CODE)
        .area GSINIT4 (CODE)
        .area GSINIT5 (CODE)
        .area GSINIT  (CODE)
        .area GSFINAL (CODE)
        .area CSEG    (CODE)
    ;--------------------------------------------------------
    ; interrupt vector
    ;--------------------------------------------------------
        .area HOME    (CODE)
    __interrupt_vect:
        ljmp    __sdcc_gsinit_startup
    ;--------------------------------------------------------
    ; global & static initialisations
    ;--------------------------------------------------------
        .area HOME    (CODE)
        .area GSINIT  (CODE)
        .area GSFINAL (CODE)
        .area GSINIT  (CODE)
        .globl __sdcc_gsinit_startup
        .globl __sdcc_program_startup
        .globl __start__stack
        .globl __mcs51_genXINIT
        .globl __mcs51_genXRAMCLEAR
        .globl __mcs51_genRAMCLEAR
        .area GSFINAL (CODE)
        ljmp    __sdcc_program_startup
    ;--------------------------------------------------------
    ; Home
    ;--------------------------------------------------------
        .area HOME    (CODE)
        .area CSEG    (CODE)
    __sdcc_program_startup:
        lcall    _main
    ;    return from main will lock up
        sjmp .
    ;--------------------------------------------------------
    ; code
    ;--------------------------------------------------------
        .area CSEG    (CODE)
    ;------------------------------------------------------------
    ;Allocation info for local variables in function 'idleTask'
    ;------------------------------------------------------------
    ;context                   Allocated to registers r2
    ;------------------------------------------------------------
        Ftest_00$idleTask$0$0 ==.
        C$test_00.c$2$0$0 ==.
    ;    test_00.c:2: static unsigned char idleTask(unsigned char context) reentrant
    ;    -----------------------------------------
    ;     function idleTask
    ;    -----------------------------------------
    _idleTask:
        ar2 = 0x02
        ar3 = 0x03
        ar4 = 0x04
        ar5 = 0x05
        ar6 = 0x06
        ar7 = 0x07
        ar0 = 0x00
        ar1 = 0x01
    ;    genReceive
        mov    r2,dpl
        C$test_00.c$4$1$1 ==.
    ;    test_00.c:4: context++;
    ;    genPlus
    ;     genPlusIncr
        inc    r2
        C$test_00.c$5$1$1 ==.
    ;    test_00.c:5: return context;
    ;    genRet
        mov    dpl,r2
    ;    Peephole 300    removed redundant label 00101$
        C$test_00.c$6$1$1 ==.
        XFtest_00$idleTask$0$0 ==.
        ret
    ;------------------------------------------------------------
    ;Allocation info for local variables in function 'mainLoop'
    ;------------------------------------------------------------
    ;context                   Allocated to registers r2
    ;------------------------------------------------------------
        Ftest_00$mainLoop$0$0 ==.
        C$test_00.c$8$1$1 ==.
    ;    test_00.c:8: static void mainLoop() reentrant
    ;    -----------------------------------------
    ;     function mainLoop
    ;    -----------------------------------------
    _mainLoop:
        C$test_00.c$10$1$1 ==.
    ;    test_00.c:10: unsigned char context = 0;
    ;    genAssign
        mov    r2,#0x00
        C$test_00.c$11$1$1 ==.
    ;    test_00.c:11: while (1)
    00102$:
        C$test_00.c$13$2$2 ==.
    ;    test_00.c:13: context = idleTask(context);
    ;    genCall
        mov    dpl,r2
        lcall    _idleTask
        mov    r3,dpl
    ;    genAssign
        mov    ar2,r3
    ;    Peephole 112.b    changed ljmp to sjmp
        C$test_00.c$15$1$1 ==.
        XFtest_00$mainLoop$0$0 ==.
        sjmp    00102$
    ;    Peephole 259.a    removed redundant label 00104$ and ret
    ;
    ;------------------------------------------------------------
    ;Allocation info for local variables in function 'main'
    ;------------------------------------------------------------
    ;------------------------------------------------------------
        G$main$0$0 ==.
        C$test_00.c$17$1$1 ==.
    ;    test_00.c:17: void main()
    ;    -----------------------------------------
    ;     function main
    ;    -----------------------------------------
    _main:
        C$test_00.c$19$1$1 ==.
    ;    test_00.c:19: mainLoop();
    ;    genCall
        C$test_00.c$20$1$1 ==.
        XG$main$0$0 ==.
    ;    Peephole 253.b    replaced lcall/ret with ljmp
        ljmp    _mainLoop
    ;
        .area CSEG    (CODE)
        .area CONST   (CODE)
        .area XINIT   (CODE)

     
    • > May you help me with this one ?

      Hmmm, seems you have found a bug in SDCC 2.6.0
      which still persists in the latest svn version!

      Please file a bug report...

      I don't like to, but if it's just to get started
      I'd currently suggest installing SDCC 2.5.0 and
      give it a whirl there.

      I just checked that:

      > sdcc --debug test_00.c
      > ddd -debugger 'sdcdb -cpu 8032 test_00'

      works fine with 2.5.0 (but *not* with a recent version).

      After starting ddd type "b main", "run" into ddd's
      lowermost window and then click Nexti to single-step
      through your source.
      If you are within your function mainLoop() you
      could then f.e. type "graph display context" to display
      the variable named context (or use a few mouse-clicks
      to add it within ddd).

       
      • this link gives an idea how ddd looks like:

        http://svn.sourceforge.net/viewcvs.cgi/\*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.png

        (it would be awkward to use sdcdb of SDCC 2.5.0 without a graphical frontend
        as the old sdcdb does not yet use GNU readline)

         
      • larytet
        larytet
        2007-01-16

        i appreciate your kind response.

        is this a bug in the compiler (SDCC) or in the simulation (SDCDB) ? Can it be fixed by modification of C code or assembler ?

        I would like to avoid downgrade of the SDCC, because i do not want to compile SDCC from the source code. That loops forever in the source code are not crucial for me and i can modify the code if it helps me to avoid reinstallation of the environment.

        thank you.

         
        • > I would like to avoid downgrade of the SDCC, because
          > i do not want to compile SDCC from the source code

          you can probably work around it without changing your
          source by using the 2.5.0 version of the debugger.
          At least it worked for me.

          I used the following procedure (recalling from memory:)

          cd /tmp
          wget http://downloads.sourceforge.net/sdcc/sdcc-2.5.0.tar.gz
          tar -xvzf sdcc-2.5.0.tar.gz
          cd sdcc
          ./configure
          make

          and now instead of the usual "sudo make install" I used:
          sudo cp bin/sdcdb /usr/local/bin/sdcdb_2.5.0

          Then:
          ddd -debugger "sdcdb_2.5.0 -cpu 8032 test_00"
          worked for me (maybe only on first sight).

          Nevertheless please file a bug report!)

           
    • Borut Ražem
      Borut Ražem
      2007-03-24

      I've fixed the ddd problem Frieder mentioned, but the arkady's problem still exists, at least according to may testing.

      Frieder, can you please verify if may fix solves the ddd problem and verify if arkady's problem really didn't exist in sdcc 2.5.0 (try to go trough the loop more than 3 times)?

      Borut