Mix Assembler and C?

Hi,

I managed to get my own C program running on MIOS, based on the sdcc_skeleton files. Now I want to integrate TKs random function into my program:

;; --------------------------------------------------------------------------

;;  This function generates a new random number

;;  OUT: new random number in SEQ_RANDOM_SEED_[LH]

;; --------------------------------------------------------------------------

SEQ_CORE_GenRandomNumber

  SET_BSR  SEQ_BASE

  movf  SEQ_RANDOM_SEED_L, W, BANKED

  mulwf  SEQ_RANDOM_SEED_H, BANKED

  movf  TMR0L, W

  addwf  PRODL, W

  movwf  SEQ_RANDOM_SEED_L, BANKED

  movlw  0x69

  addwfc  PRODH, W

  movwf  SEQ_RANDOM_SEED_H, BANKED

  return

How do I define this as an assembler function that I can reference in C? I could not find a mixed C/Assembler example project.

Many thanks, ALEXander.

Hi Alexander,

did you really search for examples?

Here you will find a lot of examples for accessing assembler code from C with a wrapper:

http://svnmios.midibox.org/listing.php?repname=svn.mios&path=%2Ftrunk%2Fmodules%2F

This example is a bit more simple, only one file which references (and contains) the assembly code:

http://svnmios.midibox.org/listing.php?repname=svn.mios&path=%2Ftrunk%2Fapps%2Fexamples%2Fsm_c_example1%2F

The mclock.c file of this application demonstrates, how to use inline assembly code:

http://svnmios.midibox.org/listing.php?repname=svn.mios&path=%2Ftrunk%2Fapps%2Fclockbox%2Fenhanced%2F

Best Regards, Thorsten.

Thanks, I only looked in the base package and did not find an app where I could really understand what was going on.

Don’t forget the wiki - there is a whole section devoted to this subject at http://www.midibox.org/dokuwiki/how_to_mix_c_and_asm. At the moment, it’s very bare, although it links to a very informative forum post. Perhaps you’ll flesh it out a little when you finish your work? :slight_smile:

OK, I got it working. I generated a simple application sceleton which uses a c and an asm part, and links them both together. so far, only one byte is supported for parameters or return values. anybody care? I will extend it soon.

Best, ALEXander.

BTW: is SET_BSR “Set the Bank Select Register”? Took me a while to figure that one out. But now it works.

yes, SET_BSR is a macro which simplifies the usage of movlb, see also:

http://svnmios.midibox.org/filedetails.php?repname=svn.mios&path=%2Ftrunk%2Finclude%2Fasm%2Fmacros.h

Best Regards, Thorsten.

I generated a simple application sceleton which uses a c and an asm part, and links them both together.

Would be cool if you could show the various ways of doing it… Functions, variables, inline, included, via modules, etc