Skip to main content
Visitor II
November 6, 2003
Question

Assembler in Cosmic C compiler

  • November 6, 2003
  • 8 replies
  • 1792 views
Posted on November 06, 2003 at 09:17

Assembler in Cosmic C compiler

    This topic has been closed for replies.

    8 replies

    Visitor II
    June 18, 2003
    Posted on June 18, 2003 at 08:29

    Hello everyone,

    I want to write a small part of the code of my program in assembler, the rest is written in C. Doing this is no problem in Cosmic, but...

    The assembler part writes data into a variable that is used in the C code.

    How can i manage that C an assembler use the same memory place for this variable?

    Thanks...
    Visitor II
    June 18, 2003
    Posted on June 18, 2003 at 10:45

    You can do this by two methods:

    1)Use absolute addressing for declaring variables

    volatile unsigned char i @0x80;

    Above instruction will declare i at memory lacation 0x80 and in your assembly you can use this address

    2) use _asm function

    _asm(''SRA A\n LD $80,a\n'',j);

    This instruction will load variable j in A and shift it right, next it will load the result in A at 0x80

    Hope this helps

    Regards,

    PraveenG

    [ This message was edited by: praveenG on 18-06-2003 15:15 ]
    Visitor II
    June 18, 2003
    Posted on June 18, 2003 at 12:04

    The correct way would be:

    unsigned char data; // c variable

    void somefunc(void)

    {

    #asm

    ld a, #10

    ld _data, a

    #endasm

    }

    If the variables are declared in another file then use the XREF keyword,eg.

    #pragma asm

    xref _data

    #pragma endasm

    Regards

    Spen
    Visitor II
    June 18, 2003
    Posted on June 18, 2003 at 12:04

    Hi,

    I've tried this :

    int waarde;

    void main(){

    #asm

    LD A, #100

    LD _waarde,A

    #endasm

    schrijfPWM(3,waarde);

    }

    but it didn't work, anyone an idea what i did wrong??? When i write ex. '' waarde = 255;'' it works fine, so ''schrijfPWM()'' works well.

    thanks.

    [ This message was edited by: joble on 18-06-2003 15:51 ]
    Visitor II
    June 18, 2003
    Posted on June 18, 2003 at 12:41

    Hi again,

    Finally it works!

    The variable should be a char (like in the example sjo gave) instead of an int. I do not understand why, but it seems to be that way...

    Thanks both for your help!
    Visitor II
    June 18, 2003
    Posted on June 18, 2003 at 13:39

    If you need to use an int, then remenber you have to work on both chars, eg.

    int data;

    void somefunc(void)

    {

    #asm

    ld a, #10

    ld _data, a ; msb of int

    clr _data+1 ; lsb of int

    #endasm

    }

    Regards

    Spen
    Visitor II
    November 6, 2003
    Posted on November 06, 2003 at 07:57

    Quote:

    How about declaring the variable ''data'' in somefunc() as a local variable?

    I tried. But it didn't work and showed _data undefined.

    I can't find the helpful information in documentation.

    Thanks a million!

    slin

    Visitor II
    November 6, 2003
    Posted on November 06, 2003 at 09:17

    This is not possible on local variables because of the way the compiler treats locals, eg.

    Depending on memory model, options etc a variable declared locally such as:

    unsigned char var

    is actually internal as _main$L-1. (using memory stack)

    Have a look inside the listing file to see what I mean.

    Regards

    sjo