Glad to see that "assembly questions" still appear to attract a lot of interest. I too sometimes have problems with conditionals. Probably something to do with latent dyslexia (I really must get round to joining the DNA - National Dyslexia Association ;) (If you dont get the joke you are probably a sufferer too!) My solution was to create and use the following table:
Conditional branching (bcc)
---------------------------

CMP Dx,Dy

X = branch taken, N Z C = flags set


Dx/Dy | 1/0 | 1/1 | -1/0 | 0/1 | 1/-1 | -2/-1 |
Flags | NC | Z | C | ---- | N | ---- |
+ - - - + - - - + - - - + - - - + - - - + - - - +
U bhi | | | | X | X | X |
+ + + + + + +
U bls | X | X | X | | | |
+ + + + + + +
U bcc | | X | | X | X | X |
+ + + + + + +
U bcs | X | | X | | | |
+ + + + + + +
bne | X | | X | X | X | X |
+ + + + + + +
beq | | X | | | | |
+ + + + + + +
S bpl | | X | X | X | | X |
+ + + + + + +
S bmi | X | | | | X | |
+ + + + + + +
S bge | | X | X | X | | X |
+ + + + + + +
S blt | X | | | | X | |
+ + + + + + +
S bgt | | | X | X | | X |
+ + + + + + +
S ble | X | X | | | X | |
+ + + + + + +

(Please let me know if you discover any mistakes!)

I must admit I'm a bit confused by your table, along with the fact that it nowhere deals with the V (oVerflow) flag - important with signed comparisons; eg as CMP is effectively SUB without setting the result, using 8 bits:

-127 - +2 = +127

so should this branch be taken:

MOVE.B #-127,D2
MOVE.B #2,D1
CMP.B D1,D2
BMI.S d1_less

Looking at the 1/-1 column (closest, as I've got 2/-127), I see that N is set and so branch should be taken. However, N is in fact clear - N,Z are clear and C,V are set: using 2's complement and adding (I learnt MC programming on a 6502) you can see this:

-127 = 1000 0001 -> 1000 0001
+2 = 0000 0010 -> 1111 1101 [1's complement]
1 [extra [initial] 1 for 2's complement]
-----------
1 0111 1111

C = 1 : Obvious
Z = 0 : Obvious
N = 0 : Obvious (bit 7 is 0)
V = 1 : we've exceeded -128

And so branch should NOT be taken. (AFAIK bpl & bmi really only make sense when used in conjunction with TST, which is effectively a CMP.s #0,<dest>?)

Extending your table to include V settings is a bit awkward (homework). I use the following table:

Bcc |Typ| Flags | Meaning
---------+---+----------------+------------------------------------------
RA/T | | 1 | Always (ie GOTO)
DBT | * | 1 | Never (exec loop exactly once & dec reg)
F | # | 0 | Never (replaced by the more useful BSR)
DBRA/DBF | * | 0 | Always (until reg == -1)
---------+---+----------------+------------------------------------------
HI | u | !(C | Z) | D1 > D2 (unsigned, eg 0..255)
LS | u | C | Z | D1 <= D2 (unsigned)
HS/CC | u | !C | D1 >= D2 (unsigned)
LO/CS | u | C | D1 < D2 (unsigned)
---------+---+----------------+------------------------------------------
NE | b | !Z | D1 != D2 (also, result was non-zero)
EQ | b | Z | D1 == D2 (also, result was zero)
PL | | !N | result was top bit clear (TST >= 0)
MI | | N | result was top bit set (TST < 0)
---------+---+----------------+------------------------------------------
VC | s | !V | no overflow - sign (N) correct
VS | s | V | overflow - sign (N) [poss] wrong
---------+---+----------------+------------------------------------------
GE | s | !(N ^ V) | D1 >= D2 (signed, eg -128..127)
LT | s | N ^ V | D1 < D2 (signed)
GT | s | !(Z | (N ^ V)) | D1 > D2 (signed)
LE | s | Z | (N ^ V) | D1 <= D2 (signed)
-------------------------------------------------------------------------

comparison meanings on: CMP.s D1, D2

Typ: u = unsigned
s = signed
b = both
* = DBcc versions included for completness (as they invert "sense").
# = BF would never branch and so is replaced (in the opcodes) by BSR

Flags are given using C notation relative to their ids
(! - Not, | - Or, ^ - Eor).



Reply via email to