|
Information exchange between .c source and .asm 4 Months, 3 Weeks ago
|
Karma: 0
|
|
Hello everyone,
I am working with the ZDS II for ZNEO version 5.0.1 and am using the Z16F2811AL.
I have always written my software in assembly and now I'm trying to start using C.
My knowledge about C is limited. None of the books I have on C give me an answer
to my question.
In .h and .c files there are definitions like this:
#define PARTICULAR_OPTION 1
I would like to know if it is possible to use the information created in the definition
statement above in .asm sources. I want to know the value of the symbol in order
to use it for conditional assembly in various .asm files, which of course are part
of the project file list.
Please advice me! Thanks.
|
|
|
|
|
|
|
Re:Information exchange between .c source and .asm 4 Months, 3 Weeks ago
|
Karma: 0
|
|
Unfortunately no, you can define options and store them in ROM and check there from both worlds. You can also make an .asm function and access it from C to query the option settings. Just put an underscore in front of the function name "_myfunc:" and use "extern char myfunc( void )" in C to access it ( like XDEF ). The char and void modifiers my be different depending on the return type and the passed parameters.
|
|
mr
(User)
Senior Boarder
Posts: 41
|
|
|
|
|
Re:Information exchange between .c source and .asm 4 Months, 3 Weeks ago
|
Karma: 0
|
|
One more thought, you can use the same variable in both environment. You'd have to enter it in the the preprocessor defines section for C and the defines section for ASM. It'd put the burden on you to keep them in sync, but that shouldn't be too bad if you create a different sub-project title for each config.
|
|
mr
(User)
Senior Boarder
Posts: 41
|
|
|
|
|
Re:Information exchange between .c source and .asm 4 Months, 3 Weeks ago
|
Karma: 0
|
|
Thanks mr for your help. I will have to give up conditional assembly based on defines made in C.
I have experimented a little and in C I can reach code and variables defined in asm, but in asm I can only reach code defined in C, not its variables.
I don't understand how you mean I should do to accomplish that. Could you please explain a bit more?
|
|
|
|
|
|
|
Re:Information exchange between .c source and .asm 4 Months, 2 Weeks ago
|
Karma: 0
|
|
You can still make conditional code in .ASM, you'd just have to duplicate the Define in the .asm project section. I don't think that'd be too bad because you can use a different configuration for each condition build type and set it up just once. The IDE will keep them seperate but maintenance can be tricky if you forget the pairing.
When I want to share a variable in .ASM and .C, I like to define it in .ASM and reference the variable from C code.
For example, I wanted to some info only available to the assembler:
.IFDEF _DEBUG
.ELSE
DEFINE ___FirmwareLen_segment,SPACE=ROM,ORG=%100064
SEGMENT ___FirmwareLen_segment
.ENDIF
.ref __low_romdata
.ref __len_data
xdef __end_of_rom
__end_of_rom: dw24 (__low_romdata + __len_data - 1)
db 0
I access it with:
#ifdef _DEBUG
#define HEAPTESTBLOCKSIZE (8191)
#else
#define HEAPTESTBLOCKSIZE (15)
#endif
unsigned int HeapStart;
unsigned int InternalRomEnd;
unsigned int InternalHeapTop;
unsigned int InternalHeapBase;
unsigned int InternalStackBase;
volatile unsigned int xstack;
extern unsigned int _end_of_rom;
void HeapSaveStartAddr(void)
{
char *ptr;
InternalRomEnd = _end_of_rom;
ptr = malloc(HEAPTESTBLOCKSIZE);
HeapStart = (int)ptr;
free(ptr);
// get current stack pointer
asm( "push hl" );
asm( "xref __heaptop" );
asm( "ld hl, __heaptop" );
asm( "ld (_xstack), hl" );
asm( "pop hl" );
InternalHeapTop = xstack;
// get current stack pointer
asm( "push hl" );
asm( "xref __heapbot" );
asm( "ld hl, __heapbot" );
asm( "ld (_xstack), hl" );
asm( "pop hl" );
InternalHeapBase = xstack;
// get current stack pointer
asm( "push hl" );
asm( "xref __stack" );
asm( "ld hl, __stack" );
asm( "ld (_xstack), hl" );
asm( "pop hl" );
InternalStackBase = xstack;
}
I use the above function to implement a run-time stack/heap crash warning for development.
|
|
mr
(User)
Senior Boarder
Posts: 41
|
|
|
|
|
Re:Information exchange between .c source and .asm 4 Months, 2 Weeks ago
|
Karma: 0
|
|
Mikael Ponten wrote:
. . but in asm I can only reach code defined in C, not its variables.
I don't understand how you mean I should do to accomplish that. Could you please explain a bit more?
I forgot the main question.
Assembly code can't see the variables directly. You can either place them in the C code explicitly with the "_At" clause or create a C function to return the values as the result from a CALL. Passing the addresses for the locations of the results to the C function and having the C function stuff those addresses is feasible too.
like:
( I forget the proper push order )
PUSH addr1 (ptr to 3 bytes)
PUSH addr2 (ptr to 1 byte)
PUSH addr3 (ptr to 2 byte)
CALL _Cfunction
void _Cfunction( void *a1, void *a2, void *a3)
{
*(int *)a1 = 123456;
*(char *)a2 = 'D';
*(short *)a3 = 443;
}
|
|
mr
(User)
Senior Boarder
Posts: 41
|
|
|
|
|