Lecture 6 - Basic Instruction Set arithmetic instr

advertisement
ECE265
ECE 265 – LECTURE 6
The M68HC11 Basic Instruction Set
Basic Arithmetic Instructions
4/8/2015
1
Lecture Overview
2

The M68HC11 Basic Instruction Set
 The

basic mathematical instructions
REF: Chapter 3 and the appendix that details the
instructions.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Arithmetic Instructions
3




These instructions are used to add, subtract,
compare, increment, decrement, 2’s complement,
test, and decimal adjust data.
Both 8-bit and 16-bit operations are possible.
It is possible to write code that allows data of
higher precision to be operated upon. This
extended precision requires the user to write code
to support the higher precision.
Architecture also supports Binary Coded Decimal
(BCD) operations.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Add instructions
4

Add instructions – note the addressing mode
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Add operations
5

Operation:
 ABA –
add the A and B accumulators – Result in A
 ABX, ABY – add B to the specified index register
 ADCA,ADCB – add with carry to A/B
 ADDA,ADDB – add the contents of memory to A/B
 ADC
and these set the H CC bit to accommodate BCD
arithmetic
 ADDD


– a 16-bit addition using memory
Description: 2’s complement binary addition
CC effects: N X V C and H as noted
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
The overflow flag
6

What exactly is overflow? Does that mean you exceeded the binary range
represent-able?
Carry
A
+B
C=0 V=1
0 1
0110 1100
0100 0001
1010 1101
108
65
173
Carry
A
+B
C=1 V=1
1 1
1110 0000
0100 0000
0010 0000
224 (-32)
64
32
2’s complement coming
Carry
A
+B
C=1 V=0
1 0
1011 1111
1100 0000
0111 1111
-65 (note: +65 is 0100 0001)
-64
127
and not -129
 Joanne E. DeGroat, OSU
and does not represent
83 as it would in
2’s complement
ECE265
4/8/2015
More arithemetic instructions
7


Decimal adjust, increment and decrement, and
Two’s complement.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Decimal Adjust DAA
8

Description: Adjusts the result in A to a valid BCD
number.
Consider adding the BCD for $99 + $22 in Accum A
 Both $99 and $22 are valid BCD numbers.
 Adding these in binary gives a result of $BB with no half
carry or carry. Also, $B is not a valid BCD digit.
 Executing DAA will have the following effect.

For the lsd: BCD of $9 + $2= $B will give a $1 and a half carry.
For the most significant digit you have $9 + $2=$B
adjusted $B is $1 + hc = $2 and a final carry out.
 So here, accumulator A is adjusted to $21 and both C & H are set




CC effects: N Z and C H are corrected
Forms: DAA and only inherent mode
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Increment and Decrement
9





Description: Instructions that increment or
decrement a memory location or either the A or B
accumulator (Inherent mode). Note that there is no
instruction to increment or decrement the D
accumulator. (and C is not affected)
CC effects: N Z V
Forms: INCA INCB INC (opr)
DECA DECB DEC(opt)
Just add 1 or subtract 1 (2’s complement)
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Two complement operations
10


Description: Replace the contents of the location
with its two’s complement value
CC effects: N Z V and C
 The
C bit will be set in all cases except when the
contents are $00

Forms: NEGA NEGB
 Joanne E. DeGroat, OSU
NEG (opr)
ECE265
4/8/2015
Two’s Complement
11


How do you get the two’s complement?
Two simple algorithms – consider 00001001(dec 9)
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Two’s Complement
12


How do you get the two’s complement?
Two simple algorithms – consider 00001001(dec 9)
 1.
Take the 1’s complement and add 1
 Ones
complement is 11110110 + 00000001 = 11110111
 Which represents -9
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Two’s Complement
13


How do you get the two’s complement?
Two simple algorithms – consider 00001001(dec 9)
 1.
Take the 1’s complement and add 1
 Ones
complement is 11110110 + 00000001 = 11110111
 Which represents -9
 2.
Starting with the lsb keep all binary digits until you
come to the 1st 1. Keep that 1. Invert all the more
significant binary digits.
 Easy
to see on the above example.
 Now consider 0000 1110  11110010 (14 and -14)
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Two’s complement numbers
14








0111
0110
0101
0100
0011
0010
0001
0000
7
6
5
4
3
2
1
0








 Joanne E. DeGroat, OSU
1111
1110
1101
1100
1011
1010
1001
1000
-1
-2
-3
-4
-5
-6
-7
-8
For 4-bits
ECE265
4/8/2015
Additional Arithmetic Instr.
15

A few more instructions, this time for binary
subtraction
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Binary subtraction
16

Consider the following example of 7 – 5 = 2




0111
-0101
0010
Direct and very easy to see
Now throw in a borrow - 6 – 5 = 1






0110
-0101 but right off see that we need to subtract 1 from 0
so borrow from the next binary position and get
0102 (and yes 2 is not a binary digit – but this is for illustration)
-0101 (and it is the weight when a digit is moved right)
0001
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
More binary subtraction
17

Consider 12 – 3 = 9
1100
 -0011 and we again have borrows

 Joanne E. DeGroat, OSU
ECE265
4/8/2015
More binary subtraction
18

Consider 12 – 3 = 9
1100
 -0011 and we again have borrows

1020 after 1st step of borrow
 -0011 but we still need a borrow

 Joanne E. DeGroat, OSU
ECE265
4/8/2015
More binary subtraction
19

Consider 12 – 3 = 9
1100
 -0011 and we again have borrows

1020 after 1st step of borrow
 -0011 but we still need a borrow




1012 after 2nd step of borrow
-0011
1001 which is the binary for 9
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
How about a 2’s complement result
20


An example to get a 2’s complement result, i.e., a
negative number result.
Consider 3 – 5


0011
- 0101
Borrow in =1 2011  1211

- 0101

giving
1110 which is the 2’s

complement for -2
 And here would also have a CC carry bit of 1 to indicate a
borrow.

 Joanne E. DeGroat, OSU
ECE265
4/8/2015
The subtract instruction
21


Description: Subtract the contents of two locations
CC effects: N Z V and C
C






is set if contents of 2nd operand is larger
Forms: SBA – subtract accumulator B from A  A
SUBA (opr) – subtract memory location
SUBB (opr) contents from accumulator
SUBD (opr) - 16 bit operation
SBCA (opr), SBCB (opr) – subtract with
carry
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Compare Instructions
22

Instruction to compare two values and set condition
codes.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Compare Instructions
23





Description: compare the data at two locations and
set the CC bits. The data is not altered. (Compare
instructions perform a subtraction to update the bits
of the CC register.)
CC effects: N V C Z
Forms: CBA
CPD (opr) CMPA (opr) and CMPB (opr)
Addressing modes: Note the difference in the
mnemonic for register compare of D, A, or B to
memory.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Compare instruction example
24

Comparison of a subtract
versus a compare. A subtract
instruction does alter one of
the operands which is the
destination.

Note that only the affected
bit of the CCR is shown.

 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Compare example problem
25

PROBLEM: What programming steps are needed
to compare the data at address $1031 with a set
point value of $50 using accumulator A. Note that
this address is one of the A/D result registers, i.e.,
where the result of an A/D conversion is stored.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Compare example problem
26






PROBLEM: What programming steps are needed to
compare the data at address $1031 with a set point
value of $50 using accumulator A. Note that this
address is one of the A/D result registers, i.e., where
the result of an A/D conversion is stored.
LDDA #$50 Load the set point into Reg A
CMPA $1031 Compare A to memory (A-M)
Results will indicate if A=M : Z=1
A>M : N=0 Z=0
A<M : N=1 Z=0
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Another example
27

If the data at address $1031 in the previous
example was $45 which bits in the CC register are
set or cleared?
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Test Instruction
28

The test instructions

Allows testing of values for positive, negative, or
zero values.
The instruction subtracts $00 from the location,
setting the CC register bits. The contents of the
location are not modified.

 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Multiply and Divide instructions
29

The processor can perform and 8-bit by 8-bit
multiply and two forms of divide. The forms for
divide are integer and fractional.
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
The multiply
30

Example 3.12 from text: What programming steps
are needed to multiply the data at location $D500
with the data at location $D510. The result is to be
stored at $D520 and $D521.
 LDAA
$D500 Load value #1 into A accum
 LDAB $D510 Load value #2 into B accum
 MUL
A x B -> D
 STD
$D520 Store result (16 bits)
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Another multiply example
31

Figure 3.5
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Binary Multiplication
32

Multiply in the previous example
 $FF
= 1111 1111
255
 $14 = 0001 0100
20

11 1111 1100 5100

1111 1111 0000

1 0011 1110 1100 ($13EC)

Is this 5100?
 4096
+512+256 +128+64+32 +8+4
 4096 +768
+128+96
+12
 4864 +140 +96 = 4864 + 236 = 5100 
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
The Divide instruction
33

2 divide instructions
 IDIV
Binary integer division
 Used
when D is larger than X
 Divide D/X -> X with the remainder going into D
 FDIV
Binary fractional division
 Used
when X is larger than D
 Divide D/X -> X with the remainder (or continuation of the
fractional part going into D
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Division Examples
34

Integer and Fractional examples
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Lecture summary
35

Have covered
 Cover
data transfer instructions in Lecture 5
 In this lecture went over the basic arithmetic
instructions
 Add
 Subtract
 Increment
and Decrement
 Testing data
 Multiply and Divide
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Assignment
36




Problems Chapter 3 page 87
Problem 13
Problem 14
Problem 17
 Joanne E. DeGroat, OSU
ECE265
4/8/2015
Download