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

8bit add with carry test macro

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







8bit add with carry test macro
PostPosted: Mon Jul 12, 2004 7:17 am     Reply with quote

I am thinking about to write a define/macro to add two bytes, test carry and if true set result to 255.

#define add8x8(A1,A2,RESULT) ??

I tried but defines does not like if-then.
Maybe a simple function would be better, but I dont know and I like to experiment.

Can someone help me?
Guest








PostPosted: Mon Jul 12, 2004 7:57 am     Reply with quote

I would use this:
#define _PCH
#ifndef _PCH
#byte STATUS = 0x03
#else
#byte STATUS = 0x0FD8
#endif
#bit CARRY = STATUS.0

char Add8_8(char in1, char in2)
{
char result;

CARRY = 0;
result = in1 + in2;
if (CARRY) result = 0xFF;
}
Guest








PostPosted: Mon Jul 12, 2004 7:57 am     Reply with quote

And of course
return result;
Smile
Futureatwork
Guest







PostPosted: Mon Jul 12, 2004 10:11 am     Reply with quote

And what about the carry flag behavior, does it clear automatically after a successful addition or I have to clear it myself like showed above?
ckielstra



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

View user's profile Send private message

PostPosted: Mon Jul 12, 2004 10:44 am     Reply with quote

The carry flag is set according to the result of the last computation, so you don't have to clear it manually as in the example above.

Also I wouldn't use the define _PCH which you would have to set yourself, but use the define __PCH__ which is automatically defined by the compiler.

And because this is such a nice short function I would make it inline so you can save two goto instructions.

New example code:
Code:
#ifndef __PCH__
#byte STATUS = 0x03
#else
#byte STATUS = 0x0FD8
#endif
#bit CARRY = STATUS.0

#inline
int8 Add8_8(int8 in1, int8 in2)
{
  int8 result;

  result = in1 + in2;
  if (CARRY)
     result = 0xFF;

  return result;
}
Futureatwork
Guest







PostPosted: Mon Jul 12, 2004 11:19 am     Reply with quote

Does MULLW affect the same carry flag bit?

I will use this for 8bit*8bit<=255 testing.
Guest








PostPosted: Mon Jul 12, 2004 12:10 pm     Reply with quote

8X8 multiply is another story. If you use the built-in HW multiplyer then you will always get the result as a 16-bit register pair. You should use the lower half and can test the upper byte for zero Smile

P.S.: The clear carry before the addition as suggested by myself is for "safety first" purposes. You didn't tell what PIC you are using and some of the 18F6X20 series makes really strange things...
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