Accessing global array in assembly language

Hi All,

I am working on an embedded system based on C166 micro controller, in which I have to access a global array (defined in C file) in assembly language.

I have 2 fies as following:

TEST1.C

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

int state_table [256]; .... .... // Some Code here .....

CALL.asm

---------------- ; ; Code in assembly language ; ;

I want to access state_table array in CALL.asm file.

Can anyone tell me how can I do this?

I tried to search this on other groups, but could not find answer. So I am posting my query here. If this is not the right group, then please let me know where can I post this question?

Thanks in advance for the help.

Reply to
Bhavik
Loading thread data ...

To expand on this a bit:

Every C compiler has its own way of mapping data from 'C' to an object file. There are no requirements on the compiler as to how it does this, and different compilers will do it differently. So if you want to use assembly to interact with data that's declared in C (or visa-versa), you have to know the convention that your particular compiler uses. Many, but by no means every, compiler uses the convention of prepending the C name with an underscore, so your state_table would become _state_table in assembly.

Most C compilers will emit assembly code as an intermediate step, and you can give the compiler a flag to retain the assembly file so that you can look at it. If this is the case with your compiler then just do what David said: compile to assembly, then look for the string 'state_table' in the assembly code to see what the compiler is doing with it.

If your tool chain does _not_ emit assembly then you have to dig a little harder; just check back here if that's the case and (hopefully) someone will help you out.

--
Tim Wescott
Control systems and communications consulting
http://www.wescottdesign.com

Need to learn how to apply control theory in your embedded system?
"Applied Control Theory for Embedded Systems" by Tim Wescott
Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
Reply to
Tim Wescott

The other poster's suggestion of "stealing" the compiler output as an example is probably the easiest route.

The elements of doing this are usually:

a)There is come development tool convention of mapping between C name and linker symbol name, i.e. "x" is really "_x" as known to the linker.

b)There is normally some statement in assembly-language to make the symbol public.

Using compiler output as an example will typically answer all of your questions.

--
David T. Ashley              (dta@e3ft.com)
http://www.e3ft.com          (Consulting Home Page)
http://www.dtashley.com      (Personal Home Page)
http://gpl.e3ft.com          (GPL Publications and Projects)
Reply to
David T. Ashley

Without knowing your specific compiler, I can't give an exact answer. But you might look at some of the output listing files from your compiler, & see how it does it. Try writing the action you want in C, & let let the compiler translate it. Then "steal" the compiler's output, & use as a template.

Reply to
David R Brooks

  1. Only punks use the global arrays.

  1. Only fops access the global arrays from assembler.

  2. Only retards are asking the clueless questions in the newsgroups instead of reading the manuals.

VLV

Reply to
Vladimir Vassilevsky

Only clueless impolite imbeciles take this sort of attitude to a new user.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

1 and 2 lead to the suggestion of passing the address of the array via a function call. This avoids any tricks the compiler may / or may not do. Calling ASM functions from C is in the Compiler Manual.
Reply to
Neil

That's true, but it may also avoid many of the tricks the compiler or assembler programmer may or may not do. Especially on small micros, having the data at a (link-time) known constant address can let you make much better code than if the base address is a variable in a register.

That's why number 1 above is complete nonsense in the world of small microcontrollers, and number 2 only makes sense if it is generalised to "only fops use assembly in a C program unless it is absolutely necessary".

Reply to
David Brown

The poster did not leave enough information to guess the best way. A similar question was asked in a different group. I n that case the Poster waned to shift left a 10 byte array with a PIC. in that case ASM was smaller and faster.

Reply to
Neil

Yes - using absolute addresses makes a lot of sense for small micros (such as the PIC), but is of no benefit for larger cpus. That's part of the fun of embedded development - there are no answers that apply in all circumstances.

In circumstances like that, using assembly is *sometimes* the correct way to do it. It depends on balancing your requirements - smaller and faster code, against the readability and maintainability of keeping the whole program in C. If the 10 byte shift is in a critical routine, assembly is the way to go - if it is in initialisation code, who cares if it uses a few tens of microseconds more than it has to?

If the OP would give some useful information, such as his target and what he wants to do, then we'd be able to give more concrete suggestions.

Reply to
David Brown

You can access this table, provided that you have a PRAGMA that tells the compiler to put this table in a pre-defined address in the microcontroller RAM area.

Reply to
dk

[...]

Other posters have given the complete answer, which is that it depends on your specific compiler. But for *most* compilers, it works like this: the compiler will define a symbol with an underscore followed by the array's name (for some reason, identifiers in C are usually given a leading underscore in the object file). This symbol's value will be the address of the start of the array. That is, the compiler emits the equivalent of:

.globl _state_table ; declare the _state_table symbol global _state_table: ; the symbol itself .dw 14, 32, 18, ... ; the array elements

Your assembler mnemonics will vary, but you get the idea...

--
   Wim Lewis , Seattle, WA, USA. PGP keyID 27F772C1
Reply to
Wim Lewis

It depens on the type of compiler you use. But the generic idea will be , C variables will be treated with a underscore before them in the assembly. Some compilers allow double underscores for certain variables based on their type of declaration. You can also switch to C-Assembly Mixed mode of your compiler to come to know the equivalent assembly naming and calling conventions. Calling conventions also vary from compiler to compiler. Some compilers use specific registers for passing parameters, some have limit on passing the parameters which inturn should be considered as an important point while optimisation. (Avoid using more local variables as the processor might have to internally use more registers for that function/method). They also have a specific register or set of registers in which they will return the values.

Karthik Balaguru

Reply to
karthikbalaguru

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.