247 Views 8 Replies Last post: Mar 3, 2010 7:04 AM by andrea mazzolani RSS
andrea mazzolani Newbie 6 posts since
Dec 27, 2009
Currently Being Moderated

Mar 1, 2010 6:58 AM

HPEW_Mix  asm and c source code -Use a ASM variable in a C function

hello at all,

i need to modify an old program write in asm, I'm able to call a C-function from the ASM main program, with the following declaration:

 

.glb

_ExampleCsubroutine

 

 

and in c source I declare for example:

void ExampleCsubroutine(void);

 

...within this all it'ok but, if, in a C subroutin,  I want work on a variable declared in asm (.inc file) how I can do ??

I have try to find this in information in other post without result..

 

in advance many thanks

FrankL Apprentice 1,100 posts since
Mar 16, 2009
Currently Being Moderated
Mar 1, 2010 7:36 AM in response to: andrea mazzolani
Re: HPEW_Mix  asm and c source code -Use a ASM variable in a C function

For which processor? Using which compiler?

mbarclay Newbie 8 posts since
Feb 18, 2009
Currently Being Moderated
Mar 1, 2010 12:18 PM in response to: andrea mazzolani
Re: HPEW_Mix  asm and c source code -Use a ASM variable in a C function

Regardless of which assembler and compiler you use, the general idea is like this:

 

In the asm file, declare the variable of the appropriate size using a name that has an underscore before it (e.g. _foo).

You must also declare it to be globally visible, like this:

        .glb        _foo

In the C compilation unit  (perhaps in a header file), you have to declare a variable (with the "extern" qualifier) of the type you are accessing, then reference it in your code. In both cases you do not use the underscore.

 

extern unsigned in foo;

...

a = foo * 2;

...

 

That should do it.

FrankL Apprentice 1,100 posts since
Mar 16, 2009
Currently Being Moderated
Mar 2, 2010 1:46 AM in response to: andrea mazzolani
Re: HPEW_Mix  asm and c source code -Use a ASM variable in a C function

Sorry, but why don't you define the variable in C and reference it from your assembler file?

This is easier, and it is much better for debugging.

If you define a variable in assembler you cannot see it in the debugger. Strictly speaking, in assembler you do not define a variable but you define an assembler symbol and reserve some memory space for at the address of this symbol.

For example, you will not be able to see this variable in the variable watch window in the debugger. The debugger does not know anything about the variable type.

 

The linker error may come because you do not have the underscore at the beginning of the assembler symbol. In the assembler file you have the underscore in the .glb instruction, but not in the next line.

 

And a small question, why write assembler from scratch anyway? I would prefer to write a routine in C, tell the compiler to produce an assembler source file, and then if I think it is worth the time modify the compiler generated assembler source. Then you can see how the compiler defines a variable (it adds several additional instructions as debug information).

 

To mbarkley: It is important which compiler is used as IAR compiler don't add a leading underscore to the variable name.

FrankL Apprentice 1,100 posts since
Mar 16, 2009
Currently Being Moderated
Mar 3, 2010 1:17 AM in response to: andrea mazzolani
Re: HPEW_Mix  asm and c source code -Use a ASM variable in a C function

You can assign an address to a C variable using #pragma ADDRESS, but this will not help. A variable with an associated #pragma ADDRESS is not treated as variable regarding the debug information, so you cannot see it in the debugger. And a variable defined with #pragma ADDRESS doies not reserve memory. It is simply a reference to a variable.

Some more thoughts about your software:

- You cannot get access to your assembler symbol from C because it most probably has no leading '_'. Variable names in C have on assembler level always a leading '_'.

- You have to modify your assembler code anyway as you have to add the '_' to your assembler symbol. So you can also use a variable name defined by a C variable.

- I hope the code initialising the assembler pointer also uses the assembler symbol name. Then the replacement is no problem.

- If the assembler symbol has been defined with .blkb it most probably does not have a fixed address, but is also relocate in a section defined with .SECTION command.

More Like This

  • Retrieving data ...

Bookmarked By (0)