# How to mix assembly language in c with MPLAB c30 C compiler?

Status
Not open for further replies.

#### kinjal_ic

##### Newbie level 5
c30 asm

Hi,
My question is that how we can mix assembly language in c with MPLAB c30 C compiler. :?:

for example

Code C - [expand]1
2
3
4
#asm{
xxxxxx;
xxxxxx;
}

for example I want to place a function in my C program for eeprom writing (not dependent on any .h file), say write(int address,int data) then is it possible to pass these variables to any assembly function such as that found in dspic30f4011 data sheet, which is as under:

Code ASM - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
; Setup a pointer to data EEPROM
MOV W0,TBLPAG
; Write data value to holding latch
MOV EE_DATA,W1
TBLWTL W1,[ W0]
; Setup NVMCON for programming one word to data EEPROM
MOV #0x4004,W0
MOV W0,NVMCON
; Disable interrupts while the KEY sequence is written
PUSH SR
MOV #0x00E0,W0
IOR SR
; Write the key sequence
MOV #0x55,W0
MOV W0,NVMKEY
MOV #0xAA,W0
MOV W0,NVMKEY
; Start the write cycle
BSET NVMCON,#WR
;Re-enable interrupts, if needed
POP SR

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

Last edited by a moderator:

#### btbass

__builtin_mac

The compiler supports the simple form of inline assembly

Code:
asm ("instruction");

and also an extended form

Code:
asm("template" [ : [ "constraint"(output-operand) [ , ... ] ]
[ : [ "constraint"(input-operand) [ , ... ] ]
[ "clobber" [ , ... ] ]
]
]);

You need to read the user guide to get details on using the extended form.

There is nothing stoping you from writing the code for eeprom read/write in C.
The compiler has several 'built in' functions for common assembly operations, again, the user guide is the place to find details on all the built in functions

Built-In Function List
__builtin_btg __builtin_mpyn __builtin_tblrdl
__builtin_clr __builtin_msc __builtin_tblwth
__builtin_clr_prefetch __builtin_mulss __builtin_tblwtl
__builtin_divf __builtin_mulsu __builtin_write_NVM
__builtin_divmodsd __builtin_mulus __builtin_write_OSCCONL
__builtin_divmodud __builtin_muluu __builtin_write_OSCCONH
__builtin_divsd __builtin_nop __builtin_write_RTCWEN
__builtin_divud __builtin_psvpage
__builtin_dmaoffset __builtin_psvoffset
__builtin_fbcl __builtin_sac
__builtin_lac __builtin_sacr
__builtin_mac __builtin_sftac
__builtin_modsd __builtin_subab
__builtin_modud __builtin_tblpage

Another approach is to compile the assembly code into a library and link in when building your program.
This is a good option for commonly used functions.

Added after 3 hours 19 minutes:

I see you are starting to use the Microchip 16-bit processors.
This is quite a fast and powerful processor family with lots of varieties in the series.
I have written a small rtos for use with the dsPic30/33 and Pic24 micros.
Full source code and docs are included. Please take a look.
Maybe you could use it in one of your future projects.
It is posted on eda board here.

### kinjal_ic

Points: 2

#### D_A_V_E

##### Member level 5
_asm instruction in mplab c30

Hi Does anyone knows how to use the VDI in MPLAB.

I am using a PIC24 and i thought it would be nice to use VDI. But some how i can not use the peripherals i selected.

Yours,

Dave

Status
Not open for further replies.