View previous topic :: View next topic |
Author |
Message |
Futureatwork Guest
|
8bit add with carry test macro |
Posted: Mon Jul 12, 2004 7:17 am |
|
|
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
|
|
Posted: Mon Jul 12, 2004 7:57 am |
|
|
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
|
|
Posted: Mon Jul 12, 2004 7:57 am |
|
|
And of course
return result;
|
|
|
Futureatwork Guest
|
|
Posted: Mon Jul 12, 2004 10:11 am |
|
|
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
|
|
Posted: Mon Jul 12, 2004 10:44 am |
|
|
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
|
|
Posted: Mon Jul 12, 2004 11:19 am |
|
|
Does MULLW affect the same carry flag bit?
I will use this for 8bit*8bit<=255 testing. |
|
|
Guest
|
|
Posted: Mon Jul 12, 2004 12:10 pm |
|
|
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
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... |
|
|
|