Upload
joseph-bates
View
224
Download
1
Tags:
Embed Size (px)
Citation preview
Microcontroller Fundamentals &
Programming
Arithmetic Instructions
2
Arithmetic InstructionsThe 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.
112335
Memory
ACC
+
Result
Goes Back
ADD Instructions
xx
4
LDAA #$29 ACCA = 0 0 1 0 1 0 0 1
ADDA #$3A 3A = 0 0 1 1 1 0 1 0 +
Result: ACCA = $63 Binary= 0 1 1 0 0 0 1 1
Hex = 6 3The flag bits in the CCR change as follow:
H = 1 A “1” is carried over from bit-3 to bit-4
N = 0 Answer or result is positive
Z = 0 The result is not zero
V = 0 No overflow. Answer is correct.
C = 0 No 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
112335
+
C-bit
ADD with Carry Instructions
CCR
xx
6
The flag bits in the CCR change as follow: H = 1 A “1” is carried over from bit-3 to bit-4 N = 0 Answer or result is positive Z = 0 The result is not zero V = 0 No overflow. Answer is correct. C = 0 No carry.
SEC ; Set Carry to “1”
LDAA #$29 ; ACCA = 0 0 1 0 1 0 0 1
ADCA #$3A ; $3A = 0 0 1 1 1 0 1 0 +
C-bit = 0 0 0 0 0 0 0 1 +
Result: ACCA = $64 Binary = 0 1 1 0 0 1 0 0
Hex. = 6 4
Example: ADD with CARRY instruction
7
The flag bits in the CCR change as follow:
H = X Not use.
N = 0 Answer or result is positive.
Z = 0 The result is not zero
V = 0 Answer is correct.
C = 0 No carry.
LDD #$2299 ACCD = 0010 0010 1001 1001
ADDD #$0800 ;$0800 = 0000 1000 0000 0000 +
Result: ACCD = $2A99 ;Binary = 0010 1010 1001 1001
Hex. = 2 A 9 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.
112335
Memory
ACC
Result
_
SUBTRACT Instructions
xx
9
The flag bits in the CCR change as follow: H = X Not use. N = 1 Answer or result is negative Z = 0 The result is not zero V = 0 Answer is correct. C = 1 $3A larger than $29, so a borrow is required.
C = 1LDAA #$29 ; ACCA = 0 0 1 0 1 0 0
1 SUBA #$3A ; $3A = 0 0 1 1 1 0 1 0 - Result: ACCA = $EF Binary = 1 1 1 0 1 1 1 1
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.
112335
MemoryACC
_
C-bit
Result
Subtract with Carry Instructions
xx
CCR
11
The flag bits in the CCR change as follow:
H = X Not use N = 0 Answer or result is positive Z = 0 The result is not zero V = 0 No overflow. Answer is correct. C = 0 No borrow..
SEC ; Set Carry to “1” LDAA #$3A ; ACCA = 0 0 1 1 1 0 1 0 SBCA #$23 ;$23 = 0 0 1 0 0 0 1 1 -
C-bit = 0 0 0 0 0 0 0 1 -Result:ACCA = $16 Binary = 0 0 0 1 0 1 1 0
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 0 6 6 +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 = $09
- - -
DISP LDX $1000
Example: Compare
23
Thank You