We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
In ASM51, the following code is every useful:
-------- file1.asm --------- public MAX MAX EQU 10 -------- file2.asm --------- extrn number(MAX) MOV A,#MAX
-------- file2.C ----------- extern code MAX; #define MAX_NUMBER ((unsigned char)&MAX)
The usual way to do this in C would be to supply a header file with some definitions: config.h extern U8 const myVal; and tell users of the library that they need to create an object file that defines values for those constants. config.c: U8 const myVal = 99; You then link config.o with the rest of your library. Typically, use of myVal will generate code that loads myVal from some address (determined by the linker) -- something like (at a guess) ; small model MOV R0,#myVal MOV A,@R0 ; large model MOV DPTR,#myVal MOVX A,@DPTR This code will be larger than code that loads a value as an immediate value. That is, the value is included literally in the instruction, not by reference. MOV A,#99 If this difference is important to you, then life will be a little more painful. When the assembler runs, it actually generates code like this: 0100: MOV A,#00 along with an entry in the object file that says "label MAX is at address 101". The linker then replaces the byte at address 101 with the value defined for MAX. There is no way to write C code with a "placeholder" value such as the EXTERN NUMBER in the assembler. (Such syntax would have to look something like: U16 myMax = ???; where "???" is the value to be replaced by the linker. So, there's no way to write C to get that "to be determined" integer. There is a way to cheat, though. It's easy to write C code that generates addresses that get patched up by the linker. And you can cast an address. So, you could write C thus: extern U8 MAX; // type doesn't really matter U16 myMax = (U16)&MAX; Now, all you have to do is tell your linker the address of MAX. It will find all references to the address-of-MAX in the object files and fix them up, thus replacing the value in line, as desired. I find this technique "painful" because on the one hand, it's not really obvious what you're doing. It obscures the real type and purpose of MAX. And every linker has its own idiosyncratic way of defining symbols, so it's a really non-portable technique. Unless the few extra bytes of code are really going to cripple your application, you might want to just use the first method. Yet another method would be to distribute source code (obfuscated, perhaps, if piracy is a concern), put the definitions in a common header file, and have the user recompile the whole works.
Hi, I have the following line in my assembly code and I want to link with C program. How to declare in C? EXTRN NUMBER (RS485_TASK, RECV_END) Thanks in advance.