Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to program asm in mikroC

Status
Not open for further replies.

coffeeCupPepsi

Junior Member level 2
Joined
May 30, 2010
Messages
24
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Location
Australia
Activity points
1,448
how can i use general register in mikroC?
this works clrf portb;
but this does not clrf 6;
this is not a problem for special registers as they have names, but how can i use a general register?? for example clrf 0x21; ??
 

Hai,

In C registers are smae as variables like int a,b,etc..
clrf portb = portb=0;
 

sure... but that's not what i'm asking.
i want to know how con i access general registers in mikroC
in pure assembly i can say bsf 06, 5;
but in mikroc i can't... so, how do i refer to a general register if all i know is it's address
 

hai,

If you know the general register name then why you access it's address.
 

hai,

If you know the general register name then why you access it's address.

@coffeeCupPepsi:
Its rite coffeeCupPepsi.... I have also posted reply for your doubts in some other thread where you have posted the same doubts. Also this forum is created to clear doubts or to get any information you require & not to tease any body. DO keep manners.
Regards,
Jerin.
 

@Jerin
I'm sorry if i have offended you, it was not aimed at you. But i feel the previous poster was intentionally leaving un-helpful comments, maybe he thinks it's funny, but i agree with you %100, this site is for asking questions and getting answers, the previous poster was violating this, not me.
again I'm sorry you took offense to this, I agree my comment was poor taste.
 

@Jerin
I'm sorry if i have offended you, it was not aimed at you. But i feel the previous poster was intentionally leaving un-helpful comments, maybe he thinks it's funny, but i agree with you %100, this site is for asking questions and getting answers, the previous poster was violating this, not me.
again I'm sorry you took offense to this, I agree my comment was poor taste.

If it wasn't aimed @ me then also it is bad to post such comments here in the forum.We 3 are not the only members in this site. There are over 3m. So try to keep control over you posts k?
I'm not the person you gotta apologize. Its to ansarmytheen. I have posted reply to you doubt in another thread. Please do see that & don;t repeat it again k? Be a good boy.
Regards,
Jerin. ;-)
 

Going back to the original question:

Sending command to act on a number is not a good idea, it can be confusing to read and is you juggle addresses around the numbers may become invalid.
A better solution is to give it a name, for example "#define MyVariable 0x21;" then refer to it as MyVariable afterward. You can then include it in C or asm statements by using "MyVariable = 0", "*MyVariable = 0" or wrap it in an asm statement like "asm clrf MyVariable".

An even better solution, if the address doesn't need to be fixed is simply to use "unsigned char MyVariable;", you can still use it as mentioned above but the compiler will allocate the address for you.

Brian.
 

Dear brian,
Actually the doubt still persists....
can i use general register in mikroC?
Also can we code in assembly language in any other variants of MikroC?
Regards,
Jerin. ;-)
 

@Brian
sure, I'm aware of all that.. i do know about defines etc... but even using defines or EQU, mikroC simply doesn't like to be passed an address..
#define count 0x21
or
count EQU 0x21

clrf count; // doesnt work!
 

@Brian
sure, I'm aware of all that.. i do know about defines etc... but even using defines or EQU, mikroC simply doesn't like to be passed an address..
#define count 0x21
or
count EQU 0x21

clrf count; // doesnt work!

I had posted you later that assembly instructions won't be executed in a C compiler. MikroC is a C compiler & only instructions in C could be written here. If you have to write code in assembly language, use this procedure:
void main()
{
asm
{
//code in asaembly language
}
}
Do try with that. It may help you. For more details refer the data sheet for MicroC in the site of microelectronica.
Regards,
Jerin. ;-)
 

Hi,
Refer to: https://www.edaboard.com/threads/192870/#post809576

Code:
asm {
//.....block of assembly instructions
}
C comments (both single-line and multi-line) are allowed in embedded assembly code. Assembly-style comments starting with semicolon are not allowed.

If you plan to use a certain C variable in embedded assembly only, be sure to at least initialize it in C code; otherwise, linker will issue an error. This does not apply to predefined globals such as PORTB.

For example, the following code will not be compiled, as linker won’t be able to recognize variable myvar:
Code:
unsigned myvar;
void main() {
  asm {
    MOVLW 10  // just a test
    MOVLW test_main_global_myvar_1
  }
}
Adding the following line (or similar) above asm block would let linker know that variable is used:
Code:
myvar := 0;

Hope this helps.
Tahmid.
 

how can i use general register in mikroC?
this works clrf portb;
but this does not clrf 6;
this is not a problem for special registers as they have names, but how can i use a general register?? for example clrf 0x21; ??

From MIKROELEKTRONIKA support forum :

Q: How can I properly access variables that are being declared in mikroC, from within the ASM block?

A: As you know, mikroC allows the user to mix assembly with C code. This is being done by using the asm statement (or block) within any function.
This makes critical pieces of code (hotspots) as fast and controllable as they can be. However, the instructions within the asm block are not
processed by the compiler in any way. This leads to 2 potential problems:

P1.If you intend to use a variable that was previously declared through C and you don't use it outside the asm{}, the compiler won't mark this
variable as being used and will purge it from code, making it non-existent (and therefore unrecognizable) for the Linker;

P2. The linker does NOT set the memory bank for the variables within the asm block. So, if the variable being used within the asm block is in
another RAM bank than the one that was set before entering the asm block, your code will 'miss' the RAM bank, and consequently the variable itself.

There are several ways to cope with these issues:

S1. The easiest way to cope with this is to put a dummy variable assignment just before the asm code, like this:

Code:
...
tictac = tictac; // this line will be removed by the compiler,
// but the linker will get information that the
// global variable tictac has been used and
// will allocate memory for it
asm {
MOVF _tictac,W
}
...


S2. Issue No.2 can be approached in several possible ways:
S2a) Perform variable init just before the asm block:

Code:
...
tictac = 0;
asm ADDWF _tictac,W ;
...

This will set the correct bank (if necessary) for the variable being used in the asm{}. The drawback of this is that you cannot do this if your variable is to remain unchanged. In this case, you must

S2b) Force the variable dummy init:

Code:
...
unsigned short tictac;
...
tictac = (unsigned short)tictac;

asm ADDWF _tictac,W ;
...

This allways works, but produces some extra code (for the dummy init) before the asm block; nevertheless, this is the method is most often used at mikroE, since all the changes that occur during the code change are being handled by the compiler and linker.
If you want to have things 'under a strong hand', you will like the following solution:

S2c) Setting up the bank manually. This lets you 'sleep like a baby', but might generate too much extra code (for banking) if you have to access many variables from within the asm block:

Code:
unsigned short tictac; // suppose tictac is in bank1
...
asm {
BCF STATUS,RP1
BSF STATUS,RP0
MOVF _tictac, W
...
}
...

This solution also requires some extra work, since you have first to do the compile, then take a look where (in which bank) has your variable been placed by the linker, and set the bank accordingly.
And, if you change your code (add /remove some variables) the linker might re-position your variable and you'll have to do the banking 'detective work' again. In this case you could declare your
variable(s)of choice with the absolute directive (and put them all in the same bank, for instance), and spare yourself the troubles after each code change;


- Global variables are translated as _<C_var_name>
- Static variables are translated as <src_file_name>_<function_name>_<C_var_name>

void main()
{
unsigned char var = 0xAA;
PORTB = 0x00; //All pins declared as outputs
do
{
PORTB = var;
Delay_ms(800);
asm
{
RLF main_var_L0, 1
}
}
while(1);
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top