CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to use Assembly in CCS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
amit78



Joined: 17 Jul 2009
Posts: 22
Location: Kolkata, India

View user's profile Send private message

How to use Assembly in CCS
PostPosted: Wed Jun 08, 2022 11:05 pm     Reply with quote

Dear All,

In PICMicro Datasheet some times we found some Assembly code snippets. I need to know how should we use those codes in our CCS Program.

For example, currently I am using PIC18F97J60. In the datasheet @page 160, I found one set of assembly code. I like to use that code in my C Code. I went to CCS Help but, may be I am missing something. I need little guidance.
Code:

#asm
    CLRF PORTF     ; Initialize PORTF by
                   ; clearing output
                   ; data latches
    CLRF LATF      ; Alternate method
                   ; to clear output data latches
    MOVLW 07h      ;
    MOVWF CMCON    ; Turn off comparators
    MOVLW 0Fh      ;
    MOVWF ADCON1   ; Set PORTF as digital I/O
    MOVLW 0CEh     ; Value used to initialize data direction
    MOVWF TRISF    ; Set RF3:RF1 as inputs RF5:RF4 as outputs
                   ; RF7:RF6 as inputs
#endasm


Need to know how it should work.

Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 1:12 am     Reply with quote

For your information, the compiler automatically puts the following code at
the start of main():
Code:

00004:  CLRF   TBLPTRU
00006:  BCF    RCON.IPEN
00008:  MOVF   ADCON1,W
0000A:  ANDLW  C0
0000C:  MOVWF  ADCON1
0000E:  MOVLW  07
00010:  MOVWF  CMCON


You can get the other two lines you want by doing this:
Code:
#include <18F97J60.h>
#use delay(clock=4M)

#byte LATF = getenv("SFR:LATF");

//================================
void main()
{
LATF=0;
set_tris_f(0xCE);

while(TRUE);
}


Then you get this:
Code:

.... void main()
00004:  CLRF   TBLPTRU
00006:  BCF    RCON.IPEN
00008:  MOVF   ADCON1,W
0000A:  ANDLW  C0
0000C:  MOVWF  ADCON1
0000E:  MOVLW  07
00010:  MOVWF  CMCON
.... LATF=0;
00012:  CLRF   LATF
.... set_tris_f(0xCE);
00014:  MOVLW  CE
00016:  MOVWF  TRISF
amit78



Joined: 17 Jul 2009
Posts: 22
Location: Kolkata, India

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 2:25 am     Reply with quote

Thank you So Much for your Prompt Reply.

I will check it.
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 5:30 am     Reply with quote

Both the compiler's built-in help and the manual have entries. Search for #asm. If you really need to include assembly instructions, they're inserted with a starting #asm and ended with #endasm.

Between the manual, help and searching here, you'll be able to figure things out very quickly.
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 2:25 pm     Reply with quote

Hi,

One question I would ask is "why are you doing this"? CCS has built-in high level functions to do the simple initialization/configuration stuff you want/need to do! This is one of the huge benefits of CCS C. In 10+ years of PIC coding with CCS C, I've only used .asm 1 time, and that was to ensure the fastest possible control of some digital I/O with a particularly time-fussy LED driver chip. I'd be really shocked if you *truly* need to use .asm in your projects. Basically, if you are going to use CCS C then use CCS C, which means using the built-in functions and C syntax to do what you need to do!

Jack
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 2:41 pm     Reply with quote

Agree with Jack ... WHY is ASM needed ?

I've actually only needed ASM TWICE in 25 years, both were custom very time sensitive interfaces to client's hardware.

curious to know...

Jay
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 5:15 pm     Reply with quote

I designed several real time control systems for large industrial equipment
and, with careful coding and time slicing, did not need ASM at all. I used ASM a
couple of times in earlier versions of CCS to lock/unlock registers but not any
more since they got that fixed.
_________________
Google and Forum Search are some of your best tools!!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 09, 2022 6:04 pm     Reply with quote

temtronic,
He wanted ASM because he wanted the init code published in the PIC data
sheet, and he didn't know that CCS automatically puts it at the start of main.
Now he knows.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Jun 10, 2022 9:32 pm     Reply with quote

Thanks PCM Programmer for so brilliant answer (one more time).
The CCS compiler has many years of maturation through which it has achieved good code optimization
compared to earlier versions.
It is always a temptation to use Assembler to solve critical situations, but it is often solved by modifying an
algorithm or optimize some hardware issue.

Best wishes
_________________
Humber
Ttelmah



Joined: 11 Mar 2010
Posts: 19509

View user's profile Send private message

PostPosted: Sat Jun 11, 2022 1:22 am     Reply with quote

Yes,
Even this has become less and less necessary.

Historically when the V5 compilers started some code got larger than it
had been on the V4 compilers.
I recently had cause to recompile some old V4 code, and was impressed
to see that now the result was nearly 200 bytes smaller than the V4
version.
On performing basic tasks, with a bit of care in how things are defined
and done it is possible now to pretty much do every assembler operation
in C with the same result, by doing direct accesses as PCM shows.
I've posted things like replacements for the flash memory access routines
(where several compiler versions had problems), that are direct translations
of the assembler posted in the data sheets, into C, and are simple, and
result in exactly the same assembler.

It really has become almost totally unneeded now. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group