combination of C and assembly

hi I am new to embedded , how do i combine C and Assembly lang in real time application in embedded applications

tanks

Reply to
vinod
Loading thread data ...

Usually this is done by calling assembly language routines from C, or embedding assembly statements in your C code.

The former is done by finding out how your compiler interfaces assembly language functions. If you're lucky this will be well documented. If you're not then compile some code to assembly and look at how the compiler does it. _Usually_ C labels like 'foobar' will get translated into the assembly by prepending an underscore, like '_foobar' -- so functions and global variables are easy to interface to. You can't count on static functions or variables to get a name thats visible to the assembly, more's the pity. _Usually_ variables are passed to the function in question on the stack, although the first few are often passed in registers. _Usually_ return values are passed in registers.

The latter is done with a special, non-standard statement, usually involving the letters 'asm'. The Gnu compilers have a particularly nice way of doing this that's actually useful and fairly safe. Other compilers have different ways, and some just don't let you do it at all. Once again, look to your compiler documentation, and plan on spending some quality time with the compiler's assembly output until you get it figured out.

--
Tim Wescott
Wescott Design Services
 Click to see the full signature
Reply to
Tim Wescott

It's as Tim says but read your compiler manual.

Reply to
cbarn24050

In the same file?

Read your compiler manual.

In separate files?

Read your compiler manual.

--
Grant Edwards                   grante             Yow!  I will invent "TIDY
                                  at               BOWL"...
 Click to see the full signature
Reply to
Grant Edwards

And hope that it's documented.

And hope that it's documented.

I _really_ appreciate it when the compiler documentation tells you what you need to know here. They very often don't, in which case you have to do some significant reverse-engineering. When they do it just makes life easy.

--
Tim Wescott
Wescott Design Services
 Click to see the full signature
Reply to
Tim Wescott

That reverse engineering is not that hard.

Just write a stub for the assembler routine in C into a separate C-file. You just need the parameter declarations and possibly some dummy references to the parameters in that C-file. Compile the C-file, disassemble it, look at the parameter passing and write your actual assembler instructions into the disassembled file and run it through the assembler and link with object codes from other C-files that are calling your function.

Depending on how complete disassembly listings your disassembler produced, you may have to add some pseudo directives into the disassembled file (check your assembler manual) to get it correctly assembled.

Paul

Reply to
Paul Keinanen

Just to be more complete, there is another option -- use assembly as the primary mode and use the C compiler only for compiling various functions you write. As long as you avoid C library calls which require initialization prior to main(), or if you can set those you do use up properly, it works fairly well.

I've done this several times before. Along with mixing a compiler from one vendor, an assembler from another, and a linker from yet another (for example, Lattice C, Microsoft MASM, Intel's LINK86 and LOC86 from their builder tools -- neither Lattice nor Microsoft provided a locating linker and the OBJ formats weren't even entirely compatible [but fixable, with yet another tool.])

And even the above doesn't capture the possibilities. The real world isn't easily couched in a few words, I guess.

Jon

Reply to
Jonathan Kirwan

exactally... even just look at the lst files

Reply to
DAC

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.