Microcontroller Lecture #9
Microcontroller Lecture #9
1
Week 9
Biomedical Engineering Department
College of Engineering
Imam Abdulrahman Bin Faisal University, Saudi Arabia
Today's topic
5-Arithmetic, Logic
3-Branch, Call and Time 4-PIC I/O Port
Instructions, and
Delay Loop Programming
Programs
2
Today’s lecture
Code subtraction and multiplication instructions for
unsigned data
Concept of signed numbers
Perform logic operations
AND
OR
EX-OR
3
Subtraction of unsigned numbers
There are two different instructions for subtraction:
1. SUBLW
2. SUBWF
3. SUBWFB
4. SUBFWB
The last two instructions are used to subtract with borrow and
C (carry) flag for the borrow. 4
SUBLW K (WREG = K- WREG)
In subtraction, the PIC microcontrollers use the 2's complement
method.
5
Example 5-5
Show the steps involved in the following
1101 1100
+1
1101 1101
9
Concept of signed numbers in computers
In everyday life, numbers are used that could be positive or
negative.
For example, a temperature of 5 degrees below zero can be
represented as -5, and 20 degrees above zero as +20.
The most significant bit (MSB) is set aside for the sign (+ or -),
while the rest of the bits are used for the magnitude.
If D7= 1, it is negative.
11
Negative numbers
For negative numbers, D7 is 1; however, the magnitude is
represented in its 2 's complement.
12
Example 5-10
Show how the PIC would represent -5.
What is an overflow?
17
The range of byte-sized negative numbers is -1 to -128. The
following lists byte-sized signed number ranges
18
Example 5-13
Examine the following code and analyze the result, including the
N and OV flags.
20
When is the OV flag set?
In 8-bit signed number operations, OV is set to 1 if either of
the following two conditions occurs:
23
Example 5-16
Examine the following, noting the role of the OV and N flags
24
LOGIC INSTRUCTIONS
AND
ANDLW K ;WREG WREG AND K
This instruction will perform a logical AND on the two operands and place
the result in WREG.
25
Example 5-17
Show the results of the following.
26
OR
IORLW K ;WREG WREG Inclusive-OR K
This instruction will perform a logical OR on the
two operands and place the result in WREG..
27
Show the results of the following.
3F 0 0 1 1 1 1 1 1
28
EX-OR
XORLW K ;WREG = WREG XOR K
This instruction will perform a logical EX-OR on the two operands and
place the result in WREG.
EX-OR can also be used to see if two registers have the same value.
"XORWF fileReg, W" will EX-OR the WREG register and a fileReg
location, and put the result in WREG.
If both registers have the same value, 00 is placed in WREG. Then we can
use the BZ instruction to make a decision based on the result. See
Examples 5-20 and 5-21
29
Example: 5-19
30
EX-OR
The EX-OR instruction can be used to test the contents of a register by EX-
ORing it with a known value. In the following code, we show how EX-ORing
value 45H with itself will raise the Z flag:
EX-ORing a number with itself sets it to zero with Z = 1. We can use the BNZ
instruction to make the decision. EX-ORing with any other number will result
in a non-zero value 31
EX-OR
Read and test PORTB to see whether it has value 45H. If it does,
send 99H to PORTC; otherwise, it stays cleared.
32