This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

calling assembly routine from C in UV2

Hi,
below is the code snippet I am using to try out calling as asm func from a C module.
What happens is the asmtest func occupies memory location 0000h and C startup code is unable to execute correctly. How can I relocate the asm func to some other location ?

; in asm file
asmtest: MOV A, #64
MOV R0,#16
INC A
RET
END
-----------------------
// in C module
#include <reg52.h>

extern void asmtest(void);

void main()
{ char i = 2;
asmtest();
}

-----------------------

Parents
  • Actually what I wanted to get done is the return from the asm function should result in the next line of the C code executing. Thats why I put a dummy "i= integer" statement.
    But instead of returning here, it comes to NOP in disassembly.

    Here is a reproduction of part of the MAP file



    LX51 LINKER/LOCATER V3.56 07/15/2003 18:20:43 PAGE 1


    LX51 LINKER/LOCATER V3.56, INVOKED BY:
    C:\KEIL\C51\BIN\LX51.EXE ASM_TEST.OBJ, STARTUP.OBJ, TEST.OBJ SEGMENTS (ASMTEST (LAST))


    CPU MODE: 8051 MODE
    MEMORY MODEL: SMALL


    INPUT MODULES INCLUDED:
    ASM_TEST.OBJ (ASM_TEST)
    COMMENT TYPE 1: A51 / ASM51 Assembler
    STARTUP.OBJ (?C_STARTUP)
    COMMENT TYPE 1: A51 / ASM51 Assembler
    TEST.OBJ (TEST)
    COMMENT TYPE 1: C51 Compiler


    ACTIVE MEMORY CLASSES OF MODULE: ASM_TEST (ASM_TEST)

    BASE START END USED MEMORY CLASS
    ==========================================================
    C:000000H C:000000H C:00FFFFH 000019H CODE
    I:000000H I:000000H I:0000FFH 000001H IDATA
    I:000000H I:000000H I:00007FH 000008H DATA


    MEMORY MAP OF MODULE: ASM_TEST (ASM_TEST)


    START STOP LENGTH ALIGN RELOC MEMORY CLASS SEGMENT NAME
    =========================================================================

    * * * * * * * * * * * D A T A M E M O R Y * * * * * * * * * * * * *
    000000H 000007H 000008H --- AT.. DATA "REG BANK 0"
    000008H 000008H 000001H BYTE UNIT IDATA ?STACK

    * * * * * * * * * * * C O D E M E M O R Y * * * * * * * * * * * * *
    000000H 000002H 000003H --- OFFS.. CODE ?CO??C_STARTUP?0
    000003H 000006H 000004H BYTE INBLOCK CODE ?PR?MAIN?TEST
    000007H 000012H 00000CH BYTE UNIT CODE ?C_C51STARTUP
    000013H 000018H 000006H BYTE UNIT CODE ASMSEG



    OVERLAY MAP OF MODULE: ASM_TEST (ASM_TEST)


    FUNCTION/MODULE BIT_GROUP DATA_GROUP
    --> CALLED FUNCTION/MODULE START STOP START STOP
    ====================================================
    ?C_C51STARTUP ----- ----- ----- -----
    +--> ?PR?MAIN?TEST

    MAIN/TEST ----- ----- ----- -----



    PUBLIC SYMBOLS OF MODULE: ASM_TEST (ASM_TEST)


    VALUE CLASS TYPE PUBLIC SYMBOL NAME
    =================================================
    00000000H NUMBER --- ?C?CODESEG
    00000000H NUMBER --- ?C?XDATASEG
    01000003H CODE --- ?C_START
    01000000H CODE --- ?C_STARTUP
    LX51 LINKER/LOCATER V3.56 07/15/2003 18:20:43 PAGE 2


    01000013H CODE --- ASMTEST
    01000003H CODE --- main



    IGNORED SEGMENTS:
    ASMTEST

    --------------------

    Does the location of ASMTEST func create the prob ? Why is it listed as "ignored segments" ?

    Thanks
    James

Reply
  • Actually what I wanted to get done is the return from the asm function should result in the next line of the C code executing. Thats why I put a dummy "i= integer" statement.
    But instead of returning here, it comes to NOP in disassembly.

    Here is a reproduction of part of the MAP file



    LX51 LINKER/LOCATER V3.56 07/15/2003 18:20:43 PAGE 1


    LX51 LINKER/LOCATER V3.56, INVOKED BY:
    C:\KEIL\C51\BIN\LX51.EXE ASM_TEST.OBJ, STARTUP.OBJ, TEST.OBJ SEGMENTS (ASMTEST (LAST))


    CPU MODE: 8051 MODE
    MEMORY MODEL: SMALL


    INPUT MODULES INCLUDED:
    ASM_TEST.OBJ (ASM_TEST)
    COMMENT TYPE 1: A51 / ASM51 Assembler
    STARTUP.OBJ (?C_STARTUP)
    COMMENT TYPE 1: A51 / ASM51 Assembler
    TEST.OBJ (TEST)
    COMMENT TYPE 1: C51 Compiler


    ACTIVE MEMORY CLASSES OF MODULE: ASM_TEST (ASM_TEST)

    BASE START END USED MEMORY CLASS
    ==========================================================
    C:000000H C:000000H C:00FFFFH 000019H CODE
    I:000000H I:000000H I:0000FFH 000001H IDATA
    I:000000H I:000000H I:00007FH 000008H DATA


    MEMORY MAP OF MODULE: ASM_TEST (ASM_TEST)


    START STOP LENGTH ALIGN RELOC MEMORY CLASS SEGMENT NAME
    =========================================================================

    * * * * * * * * * * * D A T A M E M O R Y * * * * * * * * * * * * *
    000000H 000007H 000008H --- AT.. DATA "REG BANK 0"
    000008H 000008H 000001H BYTE UNIT IDATA ?STACK

    * * * * * * * * * * * C O D E M E M O R Y * * * * * * * * * * * * *
    000000H 000002H 000003H --- OFFS.. CODE ?CO??C_STARTUP?0
    000003H 000006H 000004H BYTE INBLOCK CODE ?PR?MAIN?TEST
    000007H 000012H 00000CH BYTE UNIT CODE ?C_C51STARTUP
    000013H 000018H 000006H BYTE UNIT CODE ASMSEG



    OVERLAY MAP OF MODULE: ASM_TEST (ASM_TEST)


    FUNCTION/MODULE BIT_GROUP DATA_GROUP
    --> CALLED FUNCTION/MODULE START STOP START STOP
    ====================================================
    ?C_C51STARTUP ----- ----- ----- -----
    +--> ?PR?MAIN?TEST

    MAIN/TEST ----- ----- ----- -----



    PUBLIC SYMBOLS OF MODULE: ASM_TEST (ASM_TEST)


    VALUE CLASS TYPE PUBLIC SYMBOL NAME
    =================================================
    00000000H NUMBER --- ?C?CODESEG
    00000000H NUMBER --- ?C?XDATASEG
    01000003H CODE --- ?C_START
    01000000H CODE --- ?C_STARTUP
    LX51 LINKER/LOCATER V3.56 07/15/2003 18:20:43 PAGE 2


    01000013H CODE --- ASMTEST
    01000003H CODE --- main



    IGNORED SEGMENTS:
    ASMTEST

    --------------------

    Does the location of ASMTEST func create the prob ? Why is it listed as "ignored segments" ?

    Thanks
    James

Children
  • did you create a dummy function (code the same in C) and use the generated assembler as a template. The Keil handling of segments etc is complicated enough that any other method is bound to give you headaches.

    Actually what I wanted to get done is the return from the asm function should result in the next line of the C code executing.

    What are you babbeling about. The opcodes after a RET does NOT get executed.

    Erik

  • He's not babbling, or 'babbeling' for that matter.

    If you bothered to look at the code fragment he's posted you'd understand what he means.

    Stefan

  • If you bothered to look at the code fragment he's posted you'd understand what he means

    it comes to NOP in disassembly
    Of course it does, if the asm code happens to be linked last. There is NO statement as to where it goes in execution.

    Erik

  • Have you considered taking a look at the following example program?

    http://www.keil.com/download/docs/c2asm2c.zip.asp

    This is a C-ASM-C example project that should get you started.

    Jon

  • Yes, I've looked at the sample. And I tried to do something similar on my own. The problem of control not returning to the next statement after the call to asmtest() function remains.
    Here is the full code sample I am using :
    ------------------------------------------
    //test.c
    #pragma SMALL

    extern void asmtest(void);

    void main()
    {
    char i = 2;

    asmtest(); //asm func

    i++;
    }
    ----------------------------------------
    ;asm.a51
    NAME asm

    ?PR?asmtest?asm SEGMENT CODE

    PUBLIC asmtest
    EXTRN CODE (func)

    RSEG ?PR?asmtest?asm
    asmtest:
    USING 0
    LCALL func
    RET
    END

    ---------------------------------------
    //func.c
    void func()
    {
    }
    ----------------------------------------

    On executing the code above in UV2, the call to asmtest function in assembly works out correct, the call to func() a C module also works, but then the RET from the asm module doesn't give control back to the calling main() function as expected. I get series of NOP in disassembly. Any real suggestions nerds ?

    thanks
    J

  • Your program does exactly what you wrote :-)

    It calls asmtest() inc i to 3 and the leaves to the OS.
    Your problem is: you don't have any OS

    what about this:

    void main()
    {
       char i = 2;
    
       while (1)
       {
          asmtest(); //asm func
          i++;
       }
    }
    
    

  • The i++ is not executed. When control is supposed to return back to that statement, I get NOP.

    Meanwhile the c2asm2c sample seems to be in an infinite loop, wherein the main() keeps repeating. after the calls from C to asm & asm to C are over.

    J

  • Hmmm,

    It works correctly on my compiler. It even executes the i++.

    I'm using the V7.06a tools with the uVision2 debugger.

    Jon