hi, I'm writting code (8051) that has both C and assambly in the same file. for example: void main() { long temp; #pragma asm mov a, #0; //assambly #pragma endasm temp = a*1000; //C } problems: 1. a (acc) is not recognizable in the C part.what is the BEST thing to do? 2. if I'll continue this proram and add another asm part will the last value of a,for example zero, and other reg/ptr will be saved?
1. a (acc) is not recognizable in the C part. what is the BEST thing to do? 'C' uses the CPU registers (as opposed to the SFRs) for its own purposes; It has no idea that you've left a wanted value in A, so you can't rely on being able to use it! You'd need your assembler to copy the value to a 'C' variable. 2. if I'll continue this proram and add another asm part will the last value of a,for example zero, and other reg/ptr will be saved? No. #pragma asm ... endasm simply pastes assembler source directly into the assembler source generated by the compiler. See p12 in the C51 User's Guide: "This source text can be thought of as in-line assembly. However, it is output to the source file generated only when using the SRC directive. The source text is not assembled and output to the object file." If you want values saved, you'll have to do it yourself! Note that #pragma asm ... endasm requires the SRC directive, so you can see exactly what's happening by examining the assembly source generated by the compiler! BTW, your code sample would look better within <pre> and </pre> HTML Tags. (which also gives a pale background tint on my system). See "Tips for Posting Messages" http://www.keil.com/forum/tips.asp Check formatting using the 'Preview' button before posting.
Do you really need the assembly? With its optimisations & processor-specific extensions, C51 can produce some pretty tight code. You can examine the code generated by the compiler by specifying the CODE directive (or checking 'Assembly Code' in the Compiler section on the 'Listing' tab in uVision Project Options) Your 'C' structure can affect efficiency; eg,
for( idx = X; idx != 0; idx-- )
Also consider writing anything that has to be very tight in assembler, then link it to the 'c' routines. This is easy to do. The manual contains examples of 'c' calling asm and asm calling 'c'. In our projects this has proved much easier to maintain and debug.
1. a (acc) is not recognizable in the C part.what is the BEST thing to do? Well, your C code does not "know" about the registers of the 8051 unless you TELL it. The accumulator is an SFR and C51 provides a way to DECLARE it.
sfr ACC = 0xE0;
void main() { long temp; #pragma asm mov a, #0; //assambly #pragma endasm temp = ACC*1000; //C }