Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Help with startup code (esp SP)

Help
robert
2008-11-28
2013-03-12
  • robert
    robert
    2008-11-28

    I can't seem to get a handle on the startup code (MCS51).  For example - the SP never gets initialized and I can't force it to a place of my choosing.

    I have tried --stack-loc and --pack-iram, but the best I can do is reserve some stack size.  It will still put stack right after data - which might as well be a random location if I can not find it with some build directive or define (e.g. __start__stack).  But I can't see how or where that is defined or available.

    I tried looking for some documentation on the asx8051 (thinking it would help), but I have had no luck finding any.   Obviously I am missing something that everyone else has figured out.

     
    • Patryk
      Patryk
      2008-11-28

      I don't exactly understand what's your problem, but look in manual, 3.11.1 MCS51/DS390 Startup Code (p. 42 in sdccman.pdf, SDCC 2.8.0 2008-03-14 Rev. 5096).

       
      • robert
        robert
        2008-11-28

        I don't understand the problem either - which is the actual problem.  I have been reading the manual v2.8.4 which talks about startup code in section 3.12, but reading it a few dozen times has not really helped much.

        At first, it gave me the impression that SDCC would link in the necessary startup code, but it never initializes the SP.  So I have made and linked in an assembly file with sample code from the afore mentioned chapter - but I don't know how to get the definition for #__start__sack - during the build / compile - so the resulting SP initialization is even worse than not initializing it in the first place.

        I know everyone else out there is using some stack so I feel kind of silly, but there you go.

         
    • I think you have to use --no-pack-iram and --stack-loc...  Alternative you can overdrive the function __sdcc_gsinit_startup in file /device/lib/mcs51/crtstart.asm and force the stack anywhere.  Just copy the file to your working directory, modify, assemble, and link it to your project:

      asx8051 -plosgff crtstart.asm

      sdcc myfile.c crtstart.rel

      Jesus

       
      • robert
        robert
        2008-11-29

        I have tried multiple combinations of --no-pack-iram and --stack-loc, but the build map inevitably reports only one f two things:

        Stack starts at: 0x00 (sp set to 0xffffffff) with 0 bytes available.

        -or-

        Stack starts at: 0xff (sp set to 0xfe) with 1 bytes available
        *** WARNING: Only 1 byte available for stack.

        Neither of which is a viable condition.  Yes, I agree, I can force some value into SP, but the whole thing I am trying avoid is colliding with data.  So it is a kinda bad solution unless I can tell the compiler/linker where I put the stack or unless I can get that information from the linker/compiler.

        I know someone out there is using stack that is de-conflicted at build time - rather than just forcing some arbitrary value. 

        I suppose I could declare some variable at some base address with the same size of the stack - but golly - a dynamic stack is a rudimentary feature that should be available by default or at least easy to implement.

         
        • Maarten Brock
          Maarten Brock
          2008-11-29

          Robert,

          By default SDCC handles the stack for you. You don't have to set it up yourself.
          But if you get this little stack space I think you just ran out of (idata) memory. Have a look at the generated .mem file and verify it is not completely full.

          If you need more help, please post the .mem file and tell us which version of SDCC you're using.

          Maarten

           
    • robert
      robert
      2008-11-29

      Maarten,

      Well here is the version information: 2.8.4 #5253 (Oct 29 2008)

      And you can see - my resources abound:

      Internal RAM layout:
            0 1 2 3 4 5 6 7 8 9 A B C D E F
      0x00:|0|0|0|0|0|0|0|0|b|b|b|b|b|b|b|b|
      0x10:|c|c|Q|Q| | | | | | | | | | | | |
      0x20:|B|B|a|a|a|a|a|a|a|a|a|a|a|a|a|a|
      0x30:|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|
      0x40:|a|a|a|a|a|a|a|a|a|a|a|a|a| | | |
      0x50:| | | | | | | | | | | | | | | | |
      0x60:| | | | | | | | | | | | | | | | |
      0x70:| | | | | | | | | | | | | | | | |
      0x80:| | | | | | | | | | | | | | | | |
      0x90:| | | | | | | | | | | | | | | | |
      0xa0:| | | | | | | | | | | | | | | | |
      0xb0:| | | | | | | | | | | | | | | | |
      0xc0:| | | | | | | | | | | | | | | | |
      0xd0:| | | | | | | | | | | | | | | | |
      0xe0:| | | | | | | | | | | | | | | | |
      0xf0:| | | | | | | | | | | | | | | | |
      0-3:Reg Banks, T:Bit regs, a-z:Data, B:Bits, Q:Overlay, I:iData, S:Stack, A:Absolute

      Stack starts at: 0x00 (sp set to 0xffffffff) with 0 bytes available.

      Other memory:
         Name             Start    End      Size     Max    
         ---------------- -------- -------- -------- --------
         PAGED EXT. RAM                         0      256  
         EXTERNAL RAM     0x0000   0x0000       1    65536  
         ROM/EPROM/FLASH  0x0000   0x05a0    1441    65536 

      Also, I have now been trying to add an interrupt, but that is not getting handled automatically either (i.e. the vector does not get initialized).  The only thing I can figure is some setting I haven't figured out.  Here are the options I am using:

      SDCC.exe -c --vc --debug  --use-stdout -V -I$(PROJECT_DIR)\Include -I$(PROJECT_DIR)\$(BUILD_DIR)

      asx8051.exe -plosgff -I$(PROJECT_DIR)\Include

      SDCC.exe --debug --use-stdout -V

      Thanks for any assistance you can provide.
      Robert

       
      • Maarten Brock
        Maarten Brock
        2008-11-29

        Robert,

        This looks very weird. Can you please create a source (as small as possible) that reproduces this problem and attach it to a bug report? Also tell the exact commands used to compile, assemble and link the file(s). If multiple files put them in an archive (e.g. zip) first please.

        Maarten

         
    • robert
      robert
      2008-11-30

      I submitted a bug report with sample code and the build commands I used.  Is there an older version you have used which might work properly?

       
      • I just tried the code in the bug report.  Function 'main' is not defined... There is a typo in your code:  'Main' instead of 'main'.   Once fixed, the disassembled hex file looks good to me:

        ;  8051 Disassembly of main.hex
        ;  11/29/2008 18:06
        ;
            org    0
        ;
        X0000:    ljmp    X0013
        ;
            reti   
        ;
            org    0bh
        ;
            ljmp    X00d7
        ;
        X000e:    lcall    X0072
        X0011:    sjmp    X0011
        ;
        X0013:    mov    sp,#32h
            lcall    X00da
            mov    a,dpl
            jz    X0020
            ljmp    X000e
        .
        .
        .

         
    • robert
      robert
      2008-11-30

      HA!

      OK - see I was going crazy about this.  Sure its fine to call it a bug, but I work with a codebase that pre-dates ANSI C - so our coding standards are - uh abnormal?  So yeah, I would have never found this without your help - thank you very much.  Well I haven't tried it yet, but the memory map is much more encouraging.

      The very first thing I was confused about with SDCC - was how it decided on the entry point (main), and after testing my conclusion was SDCC just took the first function it found and decided it was main().  I didn't realize in my testing that I never provided a valid main().

      And don't get me wrong I am a fan of K&R, but criminy a fixed procedure name for the entry point of an entire language is surprisingly short sighted.