Dear friends, Plz help me in solving this bad problem: PROBLEM DESCRIPTION: I have a number defined in C and I DONT want to alocate memory in 8051 for this number : #define myNumber 0x1234 All is well, when i work with myNumber in C for example: HiByte=myNumber/256; will translate as expected: MOV HiByte,#012H ; No memory alocated for myNumber and stil, C compiler is capable to make calculus based on it. Happy with that, now i try tu use myNumber (remember defined in C) in ASM. And that is the problem: I'm unnable to found a method to see myNumberin ASM. Shortly: Seems to me that A51 compiler is unable to see a numerical constant declared in C51 with #define directive (but mybe i missing something out, and that's why I ask for your expertize, Dear friends) An example follows:
//---------file main.c---------------- //I wish to see this number both in C and ASM //PLZ NOTE: I dont want to reserve memory for myNumber //i only wish to use it's High Byte in a data memory location #define myNumber 0x1234 //function defined in ASM which use myNumber extern unsigned char GetHiByte(void); //reserve memory location unsigned char HiByte; void main (void) { HiByte=myNumber/256;//all is well in C HiByte=GetHiByte();//dont work in ASM, myNumber is not seen //and is initialized to zero } ;-------file func.a51----------------- ?PR?GetHiByte?FUNC? segment code EXTRN NUMBER (myNumber) public GetHiByte rseg ?PR?GetHiByte?FUNC? using 0 GetHiByte: mov r7,#HIGH(myNumber) ret end
Your problem is that you don't understand how #define actually works. You should put that #define into a separate file, and #include that both in the C and in the assembler source file.
"Your problem is that you don't understand how #define actually works." To gain such understanding, read-up on the preprocessor in your 'C' textbook. You could also try a 'Search' on this forum for "preprocessor" (or maybe "pre-processor").
Thank you verry much, Hans-Bernhard Broeker and Andrew Neil My problem is SOLVED!!!! I found the search in forum more usefull then the C51 manual because Chapter 4 lacks in information on haw preprocessor works. The corected code follows:
//file main.c------------- /************************* Hans-Bernhard Broeker and Andrew Neil solution **************************/ //myNuber must be seen both by ASM and C //so is defined in a comon include file #include <comon.h> //function defined in ASM which use myNumber extern unsigned char GetHiByte(void); //reserve memory location unsigned char HiByte; void main (void) { HiByte=myNumber/256; HiByte=GetHiByte(); } //file comon.h------------- #ifndef _COMON_H_ #define _COMON_H_ //I wish to see this number both in ASM and C //PLZ NOTE: I dont want to reserve memory for myNumber //i only wish to use it's High Byte in a data memory location #define myNumber 0x1234 #endif ;file func.a51-------------- ;myNuber must be seen both by ASM and C ;so is defined in a comon include file $include (comon.h) ?PR?GetHiByte?FUNC? segment code public GetHiByte rseg ?PR?GetHiByte?FUNC? using 0 GetHiByte: mov r7,#HIGH(myNumber) ; Perfectly now!! ;translated as ;MOV R7,#012H ret end
"the C51 manual ... Chapter 4 lacks in information on haw preprocessor works." That's because it's a Compiler Manual - not a 'C' textbook! It is not the job of a Compiler Manual to tech you how to write programs in 'C' - that is the job of a textbook and, preferably, a 'C' programming class. See the Preface in the front of the printed C51 Manual (same as the PDF version): "This manual describes how to use the Cx51 Optimizing C Compilers to compile C programs for your target 8051 environment ... This manual assumes that you are familiar with the Windows operating system, know how to program 8051 processors, and have a working knowledge of the C programming language." (my emphasis) Also: "If you have questions about programming in C, or if you would like more information about the C programming language, refer to "Books About the C Language" on page 16."
You are right Andy Neil, Thanks again!!