I am writing simulator to microcontroller msp430. I cant understand when i should set carry bit. For example in add
instruction:
1+0x7FFF
setting carry bit or 1+0xFFFF
?
-
117th bit not 16th 0x7FFF+1 means the N bit is set but not the carry. 0xFFFF+1 sets the carry bit as does 0x8000 + 0x8000. the carry bit is the carry out of the msbit. the real question is what do you do on a subtract, you invert the second operand and carry in a 1, but do you invert the carry out? some architectures you do some you dont. easiest just to get a $10 msp430 and try it. V flag is typically set if msbit carry in and carry out differ. and n is just bit 15. – old_timer Jan 26 '16 at 00:45
-
1oh right (looking at my simulator) if it is a byte operation then 0xFF+1, bit 9 out of the alu basically is the carry bit, bit 7 is the n bit, etc. if it is a word operation then it is like the above 0xFFFF+1 is 0x0000 with the carry bit set same as 0x8000+0x8000. – old_timer Jan 26 '16 at 00:47
2 Answers
There a two different instructions that implement <
, JL (jump if less) and JLO (jump if lower).
The documentation of JL says that it
allows comparison of signed integers.
The documentation of JLO says that it
is used for the comparison of unsigned numbers.
JLO is the same as JNC, therefore, C is unsigned carry.

- 173,858
- 17
- 217
- 259
For the ADD
instruction, the carry bit is set on unsigned overflow.
You can deduce that from the examples in TI documents. In particular, the second example in the documentation of ADD
instruction (page 3-22) says that carry occurs on ADD.B
if the result is greater than 0xff
(and for ADD
and ADDA
the limits are 0xffff
and 0xfffff
respectively - 8, 16 and 20 bits):
ADD.B #10,R5 ; Add 10 to Lowbyte of R5
JC TONI ; Carry occurred, if (R5) ≥ 246 [0Ah+0F6h]
...... ; No carry
The fact that there is a NEGATIVE bit in the msp430 status register in addition to the carry bit confirms this.
There are at least a couple of existing open-source MSP430 emulators, namely mspsim
and Avrora. I suggest to use them as reference implementations.

- 8,136
- 3
- 28
- 52
-
That makes sense. I thougth that if we have negative bit then range for signed for 16 bit number is -32768 to 32767, and when we cross this range then we set carry bit, but this is wrong. – Jan 28 '16 at 18:47