How Does Assembler Know What Registers Are Safe To Overwrite Mixed Code And Assembly
Mixing up Assembly and C...
hi,
I am using a C30 compiler and was hoping to mix some C and associates code. I have a C variable say "i" and i want to apply the value stored in i somewhere in my assembly code.
I am using asm volatile( "bra 0x200" ). I desire to store the 0x200 in "i" and use inline associates to betoken to i and look for a literal value. How exercise i do this....?
I take read the section in C30 user guide regarding mixing upwardly of C and ASM code.... but it didn't help. Can anyone provide some sample code or something of this sort.
Thanks,
Asim
RE: Mixing upwardly Associates and C... 2008/03/11 01:21:00 (permalink)
From your question I am not quite sure what you want.
You are talking about a literal value but want to have information technology in a variable 'i'
Second using inline branches you have to be very carefull. A branch is relative then coding an inline branch you never know where it will cease up unless you also use labels in your inline assembly.
Using inline assembly means reading reading and reading the applicable department in the C30 user manual. Second, trying and look at the generated code and be prepared for surprises.
Suppose you want to load the content of variable i in a specific register, you can code:
__asm__ volatile ("mov %0, w0" : : "g"(i));
All parameters behind the ':' have an index. The first is %0, the second is %1 and so on. Since this instruction uses one parameter you but run across %0.
Behind the start ':' are the output parameters, behind the second ':' the input. Since this education uses an input, you want to read a variable and place the content in a annals the 'C' variable is backside the second ':'.
AVIX the PIC32 & dsPIC/PIC24 RTOS with:
- Zero Latency Interrupts
- The all-time operation!
- Integrated Power Management
Download here: http://www.avix-rt.com/
RE: Mixing up Assembly and C... 2008/03/eleven 08:16:09 (permalink)
If you write your inline assembly properly then in that location volition be few surprises.
Never utilize branches in inline code.
If you used fixed registers, equally Leon did, make sure yous clobber them or better nevertheless don't apply fixed registers.
Writing good inline lawmaking is all about communicating side-furnishings to the compiler, if this is non possible information technology is ameliorate to write a separate stand-alone assembler function.
Regards
Calum
RE: Mixing upwards Assembly and C... 2008/03/12 05:10:26 (permalink)
run across guys,
I want to strength the command to go to a specific address in the program memory. I am fully enlightened of the hazards of doing this. The address to which the branch must be made is stored in some C variable "i". Say, at run-time i get i = 0x2000. Now i want to utilise the branch instruction "bra" to take me to the 0x2000 address. then i would want to write "bra 0x2000", merely then this address may change (thats why its put in a variable). And then everytime I want to branch out to the accost whose value is stored in a variable. thats all.
I thought of moving the value to W0 and then branching from there, merely then cant i do it directly. What was that thing most ii ":" (colons) and behind a colon...?!? input and output operands?!? tin can you please intermission it down for me?
RE: Mixing up Assembly and C... 2008/03/12 05:30:36 (permalink)
I want to force the control to go to a specific address in the plan memory. I am fully aware of the hazards of doing this. The address to which the co-operative must be fabricated is stored in some C variable "i". Say, at run-time i go i = 0x2000. Now i want to use the branch pedagogy "bra" to take me to the 0x2000 address. then i would desire to write "bra 0x2000", but and so this accost may change (thats why its put in a variable). So everytime I want to branch out to the address whose value is stored in a variable.
This sounds similar you're getting hung upwards in a supposed implementation and forgetting nigh the higher level trouble you're trying to solve. Where are you getting these execution addresses from in the kickoff place? Tin can you adjust for these execution points to exist subroutines instead of jump points? If and so, you tin can use ordinary C to practice this by calling a function thru a arrow.
What do you lot expect to happen to the stack when y'all go to the computed target accost? How will the stack data of the subroutine containing the goto kludge be cleaned? Unless you are using stack frames and write assembler code to clean the stack, y'all're nigh likely just going to jump back into the subroutine that contained the goto kludge because simply information technology knows how to make clean its stuff off the stack. However how are these dispatch routines going to know the address of a appropriate place in the original subroutine? And even if they do discover that somehow, jumping into the middle of a C subroutine could and probably would take all kinds of unexpected consequences. What a mess! Calling the acceleration routines thru a arrow does all this cleanly and gives them a way to get back to the calling routine cleanly. It seems you oasis't thought this thru and don't empathize how the stack works.
If a few cycles or a footling stack space really matter, then do this acceleration table lawmaking in assembler in its own module. Mixing ASM30 and C30 by module instead of adding inline assembler to C is a lot cleaner and easier to maintain. The C subroutine linkage conventions are well documented in the manual and pretty easy to follow in assembler. I always turn off stack frame pointer usage, which makes the subroutine interface even easier and faster.
RE: Mixing upwardly Associates and C... 2008/03/12 06:05:29 (permalink)
ok what is "kludge" ? look... i am writing code to reprogram the command from inside the code... so once i've reprogrammed using RTSP (run time self programming) i desire to shift execution to the new lawmaking. The new code is stored within the data memory and so written into the program retentiveness.
is its like this:
writeProgramMem(start_addr)
{
//finishes writing some block of the program memory starting at start_addr
//sets some Finish flag
}
master()
{
long address = 0x2000;
writeProgramMem(address);
//after this my program memory has the new code. so i need to go there and showtime executing
if Finish is True
{
uncoditional bra to accost;
//once control transfers hither... information technology keeps executing the new code. So there is no demand to return to this point
//ever over again... then i dont care about saving annihilation right now. I will re-initialize the state of the whole //processor in the new code.
}
else telephone call some errorHandler
}
So that is the pseudo code up to a higher place. Now tell me, why practise i care about the stack. I am making an unconditional bound... never comin back... i dont demand to worry about the return address.
RE: Mixing upward Associates and C... 2008/03/12 06:59:fifteen (permalink)
But if all you lot want is go to a specific accost (given this is what you really want), use the desired address, bandage information technology to a part pointer and make the call from C.
AVIX the PIC32 & dsPIC/PIC24 RTOS with:
- Zero Latency Interrupts
- The best operation!
- Integrated Power Management
Download hither: http://www.avix-rt.com/
RE: Mixing upwardly Assembly and C... 2008/03/12 07:17:09 (permalink)
Thats what I am doing already... i just wanted to experiment with inline assembly. The section on mixing C variables seemed to work with everything except bra instruction. And then i was simply hoping someone could explicate that section with respect to "BRA" didactics in particular.
thats all... :)
RE: Mixing up Assembly and C... 2008/03/12 08:23:47 (permalink)
But if all you want is go to a specific accost (given this is what you really desire), utilize the desired address, cast it to a part arrow and make the call from C.
As long equally you consider the stack, since yous're using a CALL to practise a GOTO. Every time you do this yous will exit some orphan area on the stack unless you clean it somehow. The easiest manner to practise this is to make the downloaded procedure a subroutine and then that it returns to the routine that does the pointer phone call.
If it's a ane time acceleration and execution is not intended to become back to the routine that does the dispatch until reset, and then the chosen routine could explicitly reset the the stack to empty.
RE: Mixing up Assembly and C... 2008/03/12 08:27:33 (permalink)
Thats what I am doing already... i just wanted to experiment with inline assembly. The section on mixing C variables seemed to work with everything except bra educational activity.
Performing unnatural acts in assembler within compiler-managed code is a bad idea. As I said, it's a lot safer to put the unusual system diddling in its ain assembler module where there are no compiler assumptions to break and y'all know exactly what'south going on. If you're non up to that, stick with regular C within the linguistic communication.
How Does Assembler Know What Registers Are Safe To Overwrite Mixed Code And Assembly,
Source: https://www.microchip.com/forums/m321267.aspx
Posted by: claytonfittleand.blogspot.com

0 Response to "How Does Assembler Know What Registers Are Safe To Overwrite Mixed Code And Assembly"
Post a Comment