Lecture05 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Five: Conditional Branch Instructions • Condition Codes • Compare Instructions • Conditional Branches • Implementation Examples General Classes of Assembly Language Instructions • Data Movement • Data Operation – Arithmetic – Logical – Shift • Program Control – Unconditional – Conditional 2 Condition Code Register (CCR) H N Z V C CCR status bits (bits 5 and 3-0 of CCR) • H: Half carry—previous result produced a carry out • N: • Z: • V: • C: of the least significant nibble of A (A bit 3); used for BCD arithmetic. Negative—previous result was negative. Zero—previous result was zero. Overflow—previous result produced an overflow. Carry—previous result produced a carry out of the most significant bit (MSB, as determined by the operation size) S12CPUV2 Reference Manual, pp. 25-27 Text, p. 53 3 Condition Code Settings Notation for instruction effect Changed Never • -: Unchanged (by the instruction) • 0: Cleared Changed Always • 1: Set • Changed Sometimes D or o: Set/cleared (according to result) • i: Cleared or remains set • h: Set or remains cleared • ?: Undefined or undetermined Getting condition codes set • Previous “regular” program instruction • Compare instruction—affects only CCR • S12CPUV2 Reference Manual, p. 89 • Text, p. 129. 4 HCS12 Compare Instructions S12CPUV2 Reference Manual, pp. 62 5 CMPA S12CPUV2 Reference Manual, pp. 152 6 Conditional Branches, BCond Label S12CPUV2 Reference Manual, pp. 75 7 Conditional Branch Instructions BCond Label • Branch target encoded as offset from current program counter (PC) • Cond : Condition Code • Cond true: Next instruction executed is at Label, (i.e., PC + offset) • Cond false: Next instruction executed is next instruction in program 8 Branch on Single CCR Bit • C: Carry – BCC: Branch on carry clear – BCS: Branch on carry set • N: Negative (i.e., MSB) – BPL: Branch on plus, (i.e., not negative) – BMI: Branch on minus, (i.e., negative) • V: Overflow – BVC: Branch on overflow clear – BVS: Branch on overflow set • Z: Zero – BNE: Branch on not equal, (i.e., Z clear) – BEQ: Branch on equal, (i.e., Z set) 9 Branch on Number Comparison • Signed comparison – – – – BGT: BGE: BLE: BLT: Branch on greater than Branch on greater than or equal Branch on less than or equal Branch on less than • Unsigned comparison – – – – BHI: BHS: BLS: BLO: Branch on higher than Branch on higher than or same Branch on lower than or same Branch on lower than • Both signed or unsigned comparison – BNE: Branch on not equal – BEQ: Branch on equal 10 IF Statement Control Flow Graph IF (Predicate) { Then } Predicate ? T Then F Predicate ? Branch ¬ Predicate Then LABEL 11 Example: IF int X,Y; X = X - Y; if (X < 0) { X = -X; } Memory Variable: X Control Flow Graph ; Register usage ; A:X ; B:Y sba If: bge Then: nega EndIf: . . . EndIf X<0? T F X=-X 12 IF-THEN-ELSE Statement Control Flow Graph IF (Predicate) { Then } ELSE { Else } Predicate ? T Then Predicate ? F Branch ¬ Predicate Else Then Branch Done LABEL Else Done 13 Example: IF-THEN-ELSE int X, Y; if (X = = Y) { X = X * 2; } else { Y = Y - X; } Memory Variables: X, Y ; Register usage ; A:X ; B:Y If: Control Flow Graph X=Y? T F X=X*2 Y=Y-X cba bne Then: asla jmp Else: exg sba exg EndIf: . . . Else EndIF a,b a,b 14 ...
View Full Document

Ask a homework question - tutors are online