Using C and ASM

Discussion in 'Embedded Systems and Microcontrollers' started by corsair, Mar 31, 2010.

  1. corsair

    Thread Starter Member

    Mar 6, 2010
    Hi guys,

    I'm trying to figure out how to use both C and ASM in my programming. I am mainly using PIC18F4550 assembly, but there are things that are just much easier in C that I would eventually just like to code up really quick, and call it from assembly. For example, if you were helping me with my other post, I was trying to do some simple hex to bcd conversions that was just a nightmare for me. Plus, you can't divide or multiply by non-integers.

    Anyways, let's get to the point. As a small assignment, I have three .c files named bottom.c, middle.c, and top.c. I need to replace middle.c with middle.asm (I make this).

    Code ( (Unknown Language)):
    1. #include "P18F4550.h"       //local copy of version found in MCC18 directory
    4. int bottom (int dividend, int divisor);
    6. int middle(int arg1, int arg2)
    7. {
    8.     int middlelocal;
    10.     middlelocal = arg1;
    11.     middlelocal += bottom(arg1, arg2);
    13.     return middlelocal;
    14. }
    As I was starting with my middle.asm file, I kinda realized I don't use arguments in any of my function calls since I store the data all in registers.

    This is what I have so far in my middle.asm:
    Code ( (Unknown Language)):
    1. LIST P=18F4550     
    2.     #include "P18F4550.INC"
    4. extern bottom
    6. middle:
    7.         ; Define middlelocal variable (since it's an int, should I res 4 bytes?)
    8.     local middlelocal
    9.         ; Get arg2
    10.         movff   TOSU,arg2U
    11.     movff   TOSH,arg2H
    12.     movff   TOSL,arg2L
    13.     ; Get arg1
    14.     pop
    15.     movff   TOSU,arg1U
    16.     movff   TOSH,arg1H
    17.     movff   TOSL,arg1L
    18.     ; Set middlelocal = arg1?
    19.     movff   arg1L,middlelocal
    20.         ; TODO: Push variables onto stack, but not sure what to do with middlelocal
    22.         return
    23.     end
    Here are my questions:
    1) When it's just a local variable, is it best to just use "local" or should I just make a variable at the top by doing "middlelocal res 1."
    2) How do I call a C function from assembly that has arguments?
    3) How do I make a function in assembly language that uses arguments?

    I was trying to search for tutorials, but all I can find are tutorials for C or for asm.
    Last edited: Mar 31, 2010
  2. BMorse

    AAC Fanatic!

    Sep 26, 2009
    It would be easier to write the program in C, and make calls to ASM routines....
    You will just have to declare the ASM instructions in your C program...

    asm volatile("nop");


    #define Nop() asm("NOP");
    #define Reset() asm("RESET");

    B. Morse
    Last edited: Mar 31, 2010