Microcontroller Fundamentals & Programming Arithmetic Instructions
2 The 68HC11 Arithmetic Instructions are : ADD, ADD with Carry, SUBTRACT, SUBTRACT with Carry, DECIMAL ADJUST Instruction, INCREMENT, DECREMENT, COMPARE, MULTIPLICATION, and DIVISION
3 Example: ADDA, ADDB, ADDD Add contents in accumulator with contents in memory. The result after addition will be placed in accumulator. The flag bits in CCR will change according to the result of the addition Memory ACC + Result Goes Back ADD Instructions xx
4 LDAA #$29ACCA = ADDA #$3A3A = Result: ACCA = $63 Binary= Hex = 6 3 The flag bits in the CCR change as follow: H = 1A “1” is carried over from bit-3 to bit-4 N = 0Answer or result is positive Z = 0The result is not zero V = 0No overflow. Answer is correct. C = 0No carry. Example: ADD Instruction
5 Example: ADCA, ADCB Add the contents in accumulator with contents in memory and with the C-bit in the CCR. ACC Result Memory C-bit ADD with Carry Instructions CCR xx
6 The flag bits in the CCR change as follow: H = 1A “1” is carried over from bit-3 to bit-4 N = 0Answer or result is positive Z = 0The result is not zero V = 0No overflow. Answer is correct. C = 0No carry. SEC ; Set Carry to “1” LDAA #$29 ; ACCA = ADCA #$3A ; $3A = C-bit = Result: ACCA = $64 Binary = Hex. = 6 4 Example: ADD with CARRY instruction
7 The flag bits in the CCR change as follow: H = XNot use. N = 0Answer or result is positive. Z = 0The result is not zero V = 0Answer is correct. C = 0No carry. LDD #$2299 ACCD = ADDD #$0800 ;$0800 = Result: ACCD = $2A99 ;Binary = Hex. = 2 A9 9 Example: ADDD (Add 16-bit values)
8 Example: SUBA, SUBB, SUBD Subtract memory contents from the accumulator. The result after subtraction placed in accumulator. Flag bits in CCR will change according to the result of subtraction Memory ACC Result _ SUBTRACT Instructions xx
9 The flag bits in the CCR change as follow: H = XNot use. N = 1Answer or result is negative Z = 0The result is not zero V = 0Answer is correct. C = 1$3A larger than $29, so a borrow is required. C = 1 LDAA #$29 ; ACCA = SUBA #$3A ; $3A = Result: ACCA = $EF Binary = Hex. = E F Example: SUBTRACT instruction
10 Example: SBCA, SBCB Subtract memory contents and the C-bit from accumulator. The result after the subtraction will be placed in accumulator. The flag bits in CCR will change according to the result of subtraction Memory ACC _ C-bit Result Subtract with Carry Instructions xx CCR
11 The flag bits in the CCR change as follow: H = XNot use N = 0Answer or result is positive Z = 0The result is not zero V = 0No overflow. Answer is correct. C = 0No borrow.. SEC ; Set Carry to “1” LDAA #$3A ; ACCA = SBCA #$23 ;$23 = C-bit = Result:ACCA = $16 Binary = Hex. = 1 6 Example: Subtract with Carry Instruction
12 Example: DAA DAA instruction adjusts accumulator A contents immediately following an ADDA or ADCA only. DAA will adjust the result to BCD format. LDAA#$29 ; ACCA = $2 9 ADDA#$3A ; $3A = $3 A + DAA ; before DAA = $6 3 (6) + Result: ACCA = $69 ; after DAA = $6 9 Decimal Adjusted Instruction
13 Example: DAA Rule for BCD addition If result is equal or less than 9 and a carry is not produced, then answer is correct. (No adjustment needed) If result is greater than 9 or a carry is produced, a correction of +6 must be added to the sum. Decimal Adjusted Instruction (Rule)
14 Examples of DAA (1) Example 1: Equal or less than 9 and NO carry LDAA#$25 ; ACCA = $2 5 ADDA#$33 ; $33 = $3 3 + DAA ; before DAA = $5 8 Result: ACCA = $58 ; after DAA = $5 8
15 Examples of DAA (2) Example 2: Greater than 9 and NO carry LDAA#$25 ; ACCA = $2 5 ADDA#$36 ; $36 = $3 6 + DAA ; before DAA = $5 B 6 + Result: ACCA = $61 ; after DAA = $6 1
16 Examples of DAA (3) Example 3: Greater than 9 and a carry LDAA#$66 ; ACCA = $6 6 ADDA#$3A ; $3A = $3 A + DAA ; before DAA = $A Result: ACCA = $06 ; after DAA = $10 6 C-bit =1
17 Example: INC, INCA, INCB, INX, INY After executing the increment instruction a value of one is added to the memory or registers. Flag bits N, Z, V will be affected. Increment Instructions
18 LDX #$2000; IX = $2000 LDAA #$20; ACCA = $20 LDAB #$35 ; ACCB = $35 STAA $1200 ; Memory ($1200) = $20 INCA; ACCA = $20 + $01 = $21 INCB; ACCB = $35 + $01 = $36 INC $1200 ; Memory ($1200) = $20+$01=$21 INX; IX = $2000+$01 = $2001 WAI Examples: INCREMENT instructions
19 Example: DEC, DECA, DECB, DEX, DEY After executing the decrement instruction a value of one is subtracted from the memory or registers. Flag bits N, Z, V will be affected. Decrement Instructions
20 LDX #$205A; IX = $205A LDAA #$2E; ACCA = $2E LDAB #$89; ACCB = $89 STAB $1500 ; Memory ($1500) = $89 DECA; ACCA = $2E - $01 = $2D DECB; ACCB = $89 - $01 = $88 DEC $1500 ; Memory ($1500)= $89 - $01= $88 DEX; IX = $205A - $01 = $2059 WAI Examples: DECREMENT instructions
21 Example: CMPA, CMPB, CMPD, CPX, CPY are used to compare the contents of registers and memory data. after instruction has been executed: −the flag bits are updated according to the result. −contents of register and memory data will not change. Compare and conditional branch instructions are usually use together. Compare Instructions
22 LDAA #$09 ; ACCA = $09 CMPA #$09 ; ACCA subtract $09 BEQ DISP; if result equal = zero ; branch to DISPLAY ; content of ACCA = $ DISP LDX $1000 Example: Compare
23 Thank You