| Details | Message |
|---|
Read-Only Author ChiewHuat Sew Posted 13-Dec-2000 01:34 GMT Toolset C51 |  Combining C and assembly codes ChiewHuat Sew Would appreciate if someone can tell/show me how to combine C and assembly code in the Uvision 2?
|
|
Read-Only Author Keil Support Posted 13-Dec-2000 02:51 GMT Toolset C51 |  RE: Combining C and assembly codes Keil Support Does this help?
http://www.keil.com/support/docs/1671.htm
Keil Support |
|
Read-Only Author John Cleary Posted 15-Dec-2000 19:50 GMT Toolset C51 |  RE: Combining C and assembly codes John Cleary The other way is to use #pragma asm (assembly code and assembly style comments) #pragma endasm
But, there is a very good reason why not to ever do this. I have a Nohau emulator, and the emulator started treating all the code in the file that used this as mixed mode (forced line by line assembly). I quickly pulled the pragmas out and took the asm code to another file as a set of procedures.
I still wish that I could include these procedures in-line some how, but there is not a in_line command/pragma/whatever, and there would have to be a list of things to do so that one does not trash the registers of the code that gets this function in-line. Sean
|
|
Read-Only Author Andrew Neil Posted 15-Dec-2000 23:41 GMT Toolset C51 |  RE: Combining C and assembly codes Andrew Neil Well, I suppose it would do that, wouldn't it!
As noted elsewhere in this thread, when you use #pragma asm ... #pragma endasm in your 'C' source you also have to use the SRC directive, and then pass the resulting assembly source to the Assembler. Thus it is not the Compiler which generates the object code, but the Assembler. Therefore it is the Assembler which inserts the debug info into the object file, and not the Compiler. Therefore the debug info (including line numbers) will refer to the Assembly source, and not to the 'C' source!
|
|
Read-Only Author chiew_huat sew Posted 18-Dec-2000 06:11 GMT Toolset C51 |  RE: C and assembly codes chiew_huat sew thank for ur help.
pls check if what i,am doing is right.
example: mov DPTR,#IO_PORT0 mov A,#55h movx @dptr,A
can it be done as in C:
extern xdata unsigned char IO_PORT=0x9800; void main() {unsigned charx; x&=0x55; }
can it be write in this way?? if not how should i write?hope that u can show me. pls also should i use extern? xdata or idata? What is the main different?? pls may i know when should i use XBYTE.
|
|
Read-Only Author Mark Odell Posted 18-Dec-2000 13:33 GMT Toolset C51 |  RE: C and assembly codes Mark Odell Problem: You want to move 0x55 to XDATA address IO_PORT at 0x9800.
Solution:
// Do not forget volatile.
volatile unsigned char IO_PORT _at_ 0x9800;
void main(void)
{
unsigned char charVal;
// Write.
IO_PORT = 0x55;
// Read.
charVal = IO_PORT;
// Read again.
if (IO_PORT & 0x80)
{
// High bit set.
}
}
P.S. You cannot extern a C variable and assign a value to it. Only use extern in other C files that need to use IO_PORT. Never use XBYTE, just create a typed pointer as in unsigned char xdata *ptr. Now ptr can only point into xdata.
- Mark |
|
Read-Only Author Andrew Neil Posted 19-Dec-2000 09:32 GMT Toolset C51 |  RE: C and assembly codes Andrew Neil Never use XBYTE
May I ask why?
|
|
Read-Only Author Mark Odell Posted 19-Dec-2000 12:58 GMT Toolset C51 |  RE: C and assembly codes Mark Odell Just because it's a macro and I don't like macros when the implementation provides a clean way to do the same thing. Every thing I say is of course an opinion, take it with a grain of salt.
Regards.
- Mark |
|
Read-Only Author chiew huat sew Posted 29-Dec-2000 14:44 GMT Toolset C51 |  RE: C and assembly codes chiew huat sew when should the word extern. when xdata should or should not be use. is xdata does the same as xbyte. pls, how can i increase DPTR when i want to write it in C? Also how can i just increase DPL? And how can i write the push and pop of assembly to. pls show me with some example. Is there any side where i can find out more about using c to to control 8051. Thank you. |
|
Read-Only Author Mark Odell Posted 29-Dec-2000 22:30 GMT Toolset C51 |  RE: C and assembly codes Mark Odell extern is only need on variables when you need to define them in one C file but use them in at least one other C file. The variables are extern'd in an include (.h) file typically which is included by all C files that require that variable.
You can access DPTR via the sfr keyword extension. Read you C51 manual. I don't advise touching the DPTR however since you may clobber the compiler's use of it. Likewise you can access ports on the 8051 like this:
sfr port0 = 0x80;
sbit portBit0_0 = port0 ^ 0;
sbit portBit0_1 = port0 ^ 1;
...
sbit portBit0_7 = port0 ^ 7;
port0 = 0xFF; // Set all pins high
portBit0_1 = 0; // Set Port 0.1 low
xdata is not the same as XBYTE. To declare a variable that will exist in xdata simple do this:
char xdata myVariable;
Likewise you can place variables in data or idata (or pdata, bdata, etc) in a similar manner as in:
char idata someVar;
int data otherVar;
To locate a variable at a particular address you use the _at_ directive like this:
char xdata aVar _at_ 0x8000;
int code bVar _at_ 0xFFFE;
Read Kerrnigan & Ritchie's "The C Programming Language, 2nd Ed." and then read your C51 manual. |
|
Read-Only Author James Lee Posted 18-Aug-2003 12:01 GMT Toolset C51 |  RE: C and assembly codes James Lee Hi Mark/Andrew,
Read through your advice to others. Splendid! Maybe either of you can assist me too. I came across this forum when I am seeking for some quick guide of sharing code of different files of C and .a51.
Calling each other from C to get the function written in .a51 or vice-versa is completed. Went through c2asm2c.zip and it worked perfectly. However, I encounter some trouble when I need to pass on some variables that need to be manipulated from .a51 to .c My case:-
Main code is in .a51. Requires to call a function that adds 20 slightly different 8bit data bytes and then take its average (i.e. by division).
Writing the function above in .a51 is a pain due to unavailable 16bit-addition in 8051 done under Keil uV2.
My solution thought:- is to write the addition function in C so addition can be done much more easily.
Problem:- how do I pass the variables/registers defined in .a51 to the C function to be called from .a51 environment? The C function failed to recognize the similar declared register under .a51 before. Do I need to declare them under a new name OR use the similar name of data registers OR can use some global declaration of data registers. But how?
Appreciate some really cool advice if available.
Cheers, James |
|
Read-Only Author Andrew Neil Posted 13-Dec-2000 08:42 GMT Toolset C51 |  RE: Combining C and assembly codes Andrew Neil Do you mean how to put inline assembler within a 'C' source file, or how to build a uVision Project comprising both 'C' and Assembler source files?
Or both? |
|
Read-Only Author chiew huat sew Posted 14-Dec-2000 04:16 GMT Toolset C51 |  RE: Combining C and assembly codes chiew huat sew pls can have some example of the combination of the C and 8051 assemable language. thank u |
|
Read-Only Author Mark Odell Posted 13-Dec-2000 13:04 GMT Toolset C51 |  RE: Combining C and assembly codes Mark Odell Do you mean you want uVision to display both the C source code and ASM at the same time as you debug?
- Mark |
|
Read-Only Author chiew huat sew Posted 14-Dec-2000 04:43 GMT Toolset C51 |  RE: Combining C and assembly codes chiew huat sew yes thank u. what must be done with i want to combine both language. must i save both language in different file name?? If not, what should i include in the program. thank for your reply. thank u. |
|
Read-Only Author Andrew Neil Posted 14-Dec-2000 09:21 GMT Toolset C51 |  RE: Combining C and assembly codes Andrew Neil You still haven't said exactly what you want to do:
1. In your 'C' source file, include a little bit of assembler for some "tricky" task;
2. Make a project which contains both 'C' and assembler source files;
3. In the listing file, view the assembler code generated by the compiler for your 'C' source;
4. While debugging, view both 'C' source and corresponding assembler.
So here we go:
1. To include some assembler within a 'C' source file, use the #pragma asm - as described on p12 of the C51 User's Guide [1].
Note that you also need to specify the SRC directive to make the Compiler emit assembler source, and then pass that to the assembler to generate the object! (in uVision2, just check the 'Generate Assembler SRC File' and 'Assemble SRC File' options in the file properties for the 'C' source)
2. To make a project comprising both 'C' and assembler files: just add the sources to your Project. uVision will ensure that the appropriate translator (compiler or assembler) is used to generate the object, and then the linker just links all the objects. See the C51 User's Guide p141 for details of interfacing 'C' and Assembler. See also the uVision User's Guide [2]
3. View both source & assembler in the compiler listfile: use the CODE directive - see User's Guide p15. Or check 'Assembly Code' in the uVision 'Listing' options for your Project. (Unfortunately this gives the 'C' and assembler in separate parts of the listing - Keil C51 does not produce an "interleaved" C+Assembler listing).
4. View both source & assembler while debugging: With uVision in Debug mode (Debug/Start Debug Session), use View/Disassembly window. In this case you do get "interleaved" C+Assembler Note that you must have enable Debug Info generation for this to work (Project's 'Output' options).
References: [1] C51 User's Guide 03.2000
[2] uVision User's Guide 06.2000, "Getting Started and Creating Applications"
All the manuals are on the free CD in PDF format.
|
|
Read-Only Author Aasif Mansoor Posted 24-Jul-2003 17:54 GMT Toolset None |  RE: Combining C and assembly codes Aasif Mansoor How will acess a code in assembly language through a simple progrse in C language?
Please mail me the answer as soon as posssible.
Aasif_mansoor1@yahoo.com |
|
Read-Only Author Andrew Neil Posted 10-Aug-2003 22:32 GMT Toolset None |  RE: Combining C and assembly codes Andrew Neil "How will acess a code in assembly language through a simple progrse in C language?"
Sorry, don't understand your question.
I think this has all been thoroughly covered in the Manuals and previous discussions? If not, then precisely which point is confusing you? |
|