Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

problem compiling .c for 8051

Help
Anonymous
2010-11-30
2013-03-12

  • Anonymous
    2010-11-30

    Hi,
    A newbie question:
    I'm compiling the following code:

    _ void main()
    {
    __asm
    mov dptr, #0x0101
    mov a, #0x77
    movx @dptr, a
    __endasm;_

    using <sdcc my_code.c>.
    I'm getting the following .ihx:
    :03   0000    00   020008   F3                                    (LJMP 00 08)

    :03   0008    00   758107   F8                                    (MOV 81(direct) 07)
    :0A   000B    00  12006BE5826003020003   9F   (LCALL 6B;  MOV A 82;  JZ 03(relative);  LJMP 00 03;
    :08   0015    00   7900E94400601B7A   48            (MOV R1, 00; etc etc)

    :04   006B   00   75820022   78                               (MOV 82, 00; RET)

    The funny thing is that the code in address 0x0003 is a call to execute the code in main(). It is never reached because of the "JZ 03" command, which compares A with zero. A is obviously zero, as that's what we put in it just a couple commands back.

    I've tried compiling with Keil, which gave similar results (the addresses were different).
    I've uploaded it to memory, and ran an NCSim simulation with Verilog code of a Synopsys DW8051 (that's the uCtrl I'm using, and the OpCodes I found in it's documentation). The simulation indeed showed the 8051 skipping the main() code.
    I've tried compiling different code. Same result.

    I'm sure it's a silly thing I'm missing here, and any help will be greatly appreciated.

    Thanks!
    Elli.

     
  • I compiled your code and then ran "d52 -nd test.ihx" over it.
    Everything seems fine.

    ;  D52 V3.4.1 8052 Disassembly of test.ihx
    ;  2010/11/30 19:05
    ;
            org     0x0
    ;
    X0000:  ljmp    X0008           ; 0000   02 00 08   ...
    ;
    X0003:  lcall   X0064           ; 0003   12 00 64   ..d
    X0006:  sjmp    X0006           ; 0006   80 fe      .~
    ;
    X0008:  mov     sp,#0x7         ; 0008   75 81 07   u..
            lcall   X006b           ; 000b   12 00 6b   ..k
            mov     a,dpl           ; 000e   e5 82      e.
            jz      X0015           ; 0010   60 03      `.
            ljmp    X0003           ; 0012   02 00 03   ...
    ;
    X0015:  mov     r1,#0x0         ; 0015   79 00      y.
            mov     a,r1            ; 0017   e9         i
            orl     a,#0x0          ; 0018   44 00      D.
            jz      X0037           ; 001a   60 1b      `.
            mov     r2,#0x0         ; 001c   7a 00      z.
            mov     dptr,#X006f     ; 001e   90 00 6f   ..o
            mov     r0,#0x0         ; 0021   78 00      x.
            mov     p2,#0x0         ; 0023   75 a0 00   u .
    X0026:  clr     a               ; 0026   e4         d
            movc    a,@a+dptr       ; 0027   93         .
            movx    @r0,a           ; 0028   f2         r
            inc     dptr            ; 0029   a3         #
            inc     r0              ; 002a   08         .
            cjne    r0,#0x0,X0030   ; 002b   b8 00 02   8..
            inc     p2              ; 002e   05 a0      . 
    X0030:  djnz    r1,X0026        ; 0030   d9 f4      Yt
            djnz    r2,X0026        ; 0032   da f2      Zr
            mov     p2,#0xff        ; 0034   75 a0 ff   u .
    X0037:  clr     a               ; 0037   e4         d
            mov     r0,#0xff        ; 0038   78 ff      x.
    X003a:  mov     @r0,a           ; 003a   f6         v
            djnz    r0,X003a        ; 003b   d8 fd      X}
            mov     r0,#0x0         ; 003d   78 00      x.
            mov     a,r0            ; 003f   e8         h
            orl     a,#0x0          ; 0040   44 00      D.
            jz      X004e           ; 0042   60 0a      `.
            mov     r1,#0x0         ; 0044   79 00      y.
            mov     p2,#0x0         ; 0046   75 a0 00   u .
            clr     a               ; 0049   e4         d
    X004a:  movx    @r1,a           ; 004a   f3         s
            inc     r1              ; 004b   09         .
            djnz    r0,X004a        ; 004c   d8 fc      X|
    X004e:  mov     r0,#0x0         ; 004e   78 00      x.
            mov     a,r0            ; 0050   e8         h
            orl     a,#0x0          ; 0051   44 00      D.
            jz      X0061           ; 0053   60 0c      `.
            mov     r1,#0x0         ; 0055   79 00      y.
            mov     dptr,#X0000     ; 0057   90 00 00   ...
            clr     a               ; 005a   e4         d
    X005b:  movx    @dptr,a         ; 005b   f0         p
            inc     dptr            ; 005c   a3         #
            djnz    r0,X005b        ; 005d   d8 fc      X|
            djnz    r1,X005b        ; 005f   d9 fa      Yz
    X0061:  ljmp    X0003           ; 0061   02 00 03   ...
    ;
    X0064:  mov     dptr,#X0101     ; 0064   90 01 01   ...
            mov     a,#0x77         ; 0067   74 77      tw
            movx    @dptr,a         ; 0069   f0         p
            ret                     ; 006a   22         "
    ;
    X006b:  mov     dpl,#0x0        ; 006b   75 82 00   u..
            ret                     ; 006e   22         "
    ;
    X006f:  mov     r7,a            ; 006f   ff         .
    

    The code at address 0x0003 is reached via ljmp from address 0x0061.

    (see _sdcc_external_startup(), -no-xinit-opt, -main-return)

     

  • Anonymous
    2010-12-01

    Hi frief!
    Thank you for the reply! I didn't know about D52, and it sure makes things easier.
    I'm afraid the code never reaches 0x0061, as it's stuck in a loop in 0x003A:

    X0037:    clr a ;                  0037    e4 d
                    mov r0,#0xff ;    0038    78 ff x.
    X003a:    mov @r0,a ;      003a    f6 v
                    djnz r0 X003a ; 003b    d8 fd X}

    I wouldn't be so sure, but unfortunately the simulation confirms it.

    You suggested reading about _sdcc_external_startup(), why is that important here? should I have used it? this code is not what i really want to do, it's just some sort of sanity-check to make sure i can start writing the real program.
    I guess I should be using -no-xinit-opt here, you're right. Not sure why -main-return is needed, though.

    Thanks again!
    Elli.

     
  • > as it's stuck in a loop in 0x003A

    X003a:  mov     @r0,a           ; 003a   f6         v
            djnz    r0,X003a        ; 003b   d8 fd      X}
    

    that's strange. The loop is expected to terminate when R0 reaches 0.

    The snippet is generated from crtclear.asm
    http://sdcc.svn.sourceforge.net/viewvc/sdcc/trunk/sdcc/device/lib/mcs51/crtclear.asm?view=markup
    (the last non comment change of this file dates a few years back so problems would have been noticed. )

    What is the lowest value R0 ever reaches in simulation?

    >You suggested reading about _sdcc_external_startup()
    because it's the function that is linked at address 006B (which returns 0 (meaning initialization is not skipped))

    > I guess I should be using -no-xinit-opt here, you're right.
    Well, some applications allow to use -no-xinit-opt. Usually one wants C to startup initialized and wouldn't use it.

    > Not sure why -main-return is needed, though.
    I didn't say you need it:) But most 8051 main loops never return, so usually having a return address on stack which is never used can be avoided. (your main loop seems to return, so you'd have to check whether you want to use it.)

     

  • Anonymous
    2010-12-02

    Thanks!
    For some reason I thought it was comparing with zero and jumping if NOT zero. when ran the simulation longer - it reached the main().

    any idea why it's set to 0xff? any way to change it?

    Thanks again!!

     
  • > any idea why it's set to 0xff?

    yes:^)
    See link in previous post (line 43)

    > any way to change it?

    -iram-size <something>