Forum: µC & Digital Electronics atmega8: call c-code out of asm-code?

von kellerkind (Guest)

Rate this post
0 useful
not useful
hi, is there anybody out there who can explain, how i can call c-code 
from asm-code?

for tests i wrote some little code for my atmega8:

asm_code in atmel-avr-assembler:

  .include "m8def.inc"
  .include "test_c.c"
  .def temp = r16

  ldi temp, LOW(RAMEND)
  out SPL, temp
  ldi temp, HIGH(RAMEND)
  out SPH, temp


  RCALL test_c


c_code (external file named test_c.c):

  #include <avr/io.h>
  #include <stdio.h>

  int main (void) {

   DDRB  = 0xff;
   PORTB = 0x03;
   PORTB = 0x07;
   while(1) {
     /* "empty*/

   return 0;

if i try to build the asm-section, i get the following errors:

E:\elektronik\atmega8\c aus asm\test.c(1): error: Cannot find include 
file: avr/io.h

E:\elektronik\atmega8\c aus asm\test.c(2): error: Cannot find include 
file: stdio.h

E:\elektronik\atmega8\c aus asm\test.c(4): error: syntax error, 
unexpected FUNCTION

what is wrong? do i have to compile the test.c first? but then i get 
these files:


do i have to include one of them??

thanks for help!!!

von Stefan_KM (Guest)

Rate this post
0 useful
not useful
Which compiler do you use?

von Karl Heinz Buchegger (Guest)

Rate this post
0 useful
not useful
1) Why would you want to do so

2) Looking at your code, I can see a lot of misinformation and

3) Start with either programming in assembler or programming in C.
   Calling assembler from C is something, that is rarely needed.
   But calling a C function from assembler is something you will not
   want to do.

4) you cannot include a C-code file into assembler. The assembler
   translates assembler code. Translating C-code files is the job
   of the compiler, not the assembler. The output of both independent
   jobs is then linked together to form the entire executable.

5) One calls functions, not files.
   Your function is named 'main', not 'test_c'

6) If you want to mix assembler with C, you have to play by the
   rules. The rules are made by the compiler, not by yourself.
   So you have to take care about things like register usage and
   stack calling conventions.

7) If, as a C programmer, you feel the need to do something in
   assembler, step back and look again.
   While there are sometimes things, that should be done in assembler
   for speed reasons, those things are seldome necessary but often
   show a lack of C or algorithmic knowledge. Those reasons often
   circle around pushing/poping registers in ISR-functions. Other
   then that, there seldom is a need for writing something in assembler
   when writing C programs.
   But when you do, it is most of the time: C calls assembler and
   not the other way round.

8) If you feel the need, that you have to do things before main starts,
   (in avr-gcc) there are documented functions you can inherit, which
   are called during the C-runtime startup process and which are the
   correct places you can hook into the startup process.


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.