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

wanna write code in assembly language in CCS compiler

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



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

wanna write code in assembly language in CCS compiler
PostPosted: Tue May 28, 2013 6:51 am     Reply with quote

Hi
i never write an assembly code in CCS compiler. Now i want to write assembly in ccs compiler and want to use the following code in it
Code:

NUME     EQU   0x15
QU       EQU   0x20
RMND_L   EQU   0X22
RMND_M   EQU   0X23
RMND_H   EQU   0X24
MYNUM    EQU   0XFD
MYDEN    EQU   D'10'
         ORG   0H
         MOVLW MYNUM
         MOVWF NUME
         MOVLW MYDEN
         CLRF  QU,F
D_1      INCF  QU,F
         SUBWF NUME
         BC    D_1
         ADDWF NUME
         DECF  QU,F
         MOVFF NUME,RMND_L
         MOVFF QU,NUME
         CLRF  QU
D_2      INCF  QU,F
         SUBWF NUME
         BC    D_2
         ADDWF NUME
         DECF  QU,F
         MOVFF NUME,RMND_M
         MOVFF QU,RMND_H
HERE     GOTO  HERE
         END


can anyone tell me, Is CCS allow us to write code in assembly? if yes then whats the syntax for that.
Give me a very simple example guys.
Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue May 28, 2013 7:18 am     Reply with quote

There is nothing there that couldn't be done easier in C....

For instance:
Code:

#define NYNUM 0xFD
#byte NUME=0x15
    NUME=NYNUM;


Does what the first two assembler lines do....

However, look at EX_FIR.C, and EX_GLINT.C, for examples of embedding assembler in the C code (and look at #ASM in the manual).

A few years ago, it was common to have to use assembler, but the abilities of some of the commands like #byte, have basically made it unnecessary now.

Best Wishes
temtronic



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

View user's profile Send private message

PostPosted: Tue May 28, 2013 11:59 am     Reply with quote

after staring at the code it appears to take a value from 0 to 255 and'break it up' into hundred,tens and ones digits.

ie: give 123 and you'll get back 1,2 and 3 in some variables.

If so there is code here in this forum(you'll have to search for it).

Depending on how the C code is written and compiled it may not be any faster or compact that the assembler

It'll be up to you which to use.


hth
jay
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 28, 2013 2:38 pm     Reply with quote

You are welcome to learn assembly language but unless there is a strong reason for performance optimization I would strongly discourage using it. Assembly language is much harder to follow and so debugging and maintaining your program will become more difficult.

The CCS compiler is a C compiler. It can do assembly but because of where it comes from some details are different than in the other compilers you are copying the assembly code from.
For example, in CCS the variables are automatically assigned to free RAM addresses. In other compilers you as a programmer have to do this which is more difficult and also has the disadvantage that the compiler can not do memory optimization by re-using the RAM for multiple variables when possible.

Here is your program converted to C:
Code:
#include <18F458.h>
#FUSES HS, NOWDT, PUT, NOLVP
#use delay(clock=4MHz)
#byte STATUS = getenv("SFR:STATUS")
#bit CARRY_BIT = STATUS.0

int8 NUME;
int8 QU;
int8 RMND_L;
int8 RMND_M;
int8 RMND_H;

#define  MYNUM    0XFD
#define  MYDEN    10


void main()
{
   // Calculate Least significant digit
   NUME = MYDEN;
   QU = 0;
   do {
      QU++;
      NUME -= MYDEN;
   } while (CARRY_BIT);

   // correct after overflow
   NUME += MYDEN;
   QU--;       
         
   RMND_L = NUME;

   // Calculate Second digit
   NUME = QU;
   QU = 0;
   do {
      QU++;
      NUME -= MYDEN;
   } while (CARRY_BIT);

   // correct after overflow
   NUME += MYDEN;
   QU--;
   
   RMND_M = NUME;
   
   // Store remainder as Most significant digit
   RMND_H = QU;
   
   while(1);
}


And the list file to show it is almost the same:
Code:
....................    // Calculate Least significant digit
....................    NUME = MYDEN;
0014:  MOVLW  0A
0016:  MOVWF  NUME
....................    QU = 0;
0018:  CLRF   QU
....................    do {
....................       QU++;
001A:  INCF   QU,F
....................       NUME -= MYDEN;
001C:  MOVLW  0A
001E:  SUBWF  NUME,F
....................    } while (CARRY_BIT);
0020:  BC    001A
.................... 
....................    // correct after overflow
....................    NUME += MYDEN;
0022:  MOVLW  0A
0024:  ADDWF  NUME,F
....................    QU--;       
0026:  DECF   QU,F
....................           
....................    RMND_L = NUME;
0028:  MOVFF  NUME,RMND_L
.................... 
....................    // Calculate Second digit
....................    NUME = QU;
002C:  MOVFF  QU,NUME
....................    QU = 0;
0030:  CLRF   QU
....................    do {
....................       QU++;
0032:  INCF   QU,F
....................       NUME -= MYDEN;
0034:  MOVLW  0A
0036:  SUBWF  NUME,F
....................    } while (CARRY_BIT);
0038:  BC    0032
.................... 
....................    // correct after overflow
....................    NUME += MYDEN;
003A:  MOVLW  0A
003C:  ADDWF  NUME,F
....................    QU--;
003E:  DECF   QU,F
....................     
....................    RMND_M = NUME;
0040:  MOVFF  NUME,RMND_M
....................     
....................    // Store remainder as Most significant digit
....................    RMND_H = QU;
0044:  MOVFF  QU,RMND_H
....................     
....................    while(1);
0048:  BRA    0048
The pure assembly version is a tiny bit faster because the W-register is only loaded once and outside the loop. The C-version loads the W-register two times and does it the first time inside the first loop. However, that tiny bit performance gain doesn't weight against the better maintainability.

Personally, I prefer the version from the Code Library: http://www.ccsinfo.com/forum/viewtopic.php?t=27600
Code:
void GetDigits(int16 val, int8 &hundreds, int8 &tens, int8 &units)
{
  tens = 0;
  hundreds = 0;
 
  while (val >= 100)
  {
    val -= 100;
    hundreds++;
  }
 
  while (val >= 10)
  {
    val -= 10;
    tens++;
  }

  units = val;
}

//===============================
void main ()
{
  int16 adcvalue;
  int8 hundreds,tens,units;

  adcvalue = 115;

  GetDigits(adcValue, hundreds, tens, units);
 
  while (1);
}
Same code size as the assembly version but way easier to understand and less lines to read as well.
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu May 30, 2013 2:48 am     Reply with quote

You win the thread ckielstra.

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