0% found this document useful (0 votes)
4 views

Microcontroller Lecture #9

Chapter 5 of BIOEN 442 focuses on arithmetic and logic instructions for PIC microcontrollers, covering subtraction, signed numbers, and logic operations like AND, OR, and EX-OR. It explains how to perform these operations using assembly language, including handling signed numbers and overflow issues. The chapter provides examples to illustrate the concepts and their applications in programming.

Uploaded by

lamazah78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Microcontroller Lecture #9

Chapter 5 of BIOEN 442 focuses on arithmetic and logic instructions for PIC microcontrollers, covering subtraction, signed numbers, and logic operations like AND, OR, and EX-OR. It explains how to perform these operations using assembly language, including handling signed numbers and overflow issues. The chapter provides examples to illustrate the concepts and their applications in programming.

Uploaded by

lamazah78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

BIOEN 442: Microprocessor

Chapter 5 – ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS

1
Week 9
Biomedical Engineering Department
College of Engineering
Imam Abdulrahman Bin Faisal University, Saudi Arabia
Today's topic

0- Course 1- Pic 2- PIC Architecture PIC


Overview /Introduction Microcontrollers:history Programming using
to Computing and features Assembly Language

5-Arithmetic, Logic
3-Branch, Call and Time 4-PIC I/O Port
Instructions, and
Delay Loop Programming
Programs

17- Motor Control:


12-LCD and Keypad 13- ADC, DAC and
Relay, PWM, DC and
Interfacing sensor interfacing
Stepper Motors

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:

SUB and SUBB (subtract with borrow)

 The PIC18 has four 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.

Assuming that the PIC is executing a simple subtract instruction


and that C= 0 prior to the execution of the instruction

 The SUBLW instruction for unsigned numbers as follows:

1. Take the 2's complement of the subtrahend (WREG


operand).
2. Add it to the minuend (K operand).

5
Example 5-5
 Show the steps involved in the following

1101 1100
+1
1101 1101

 The flags would be set as follows:


C=1
 N = 0 (notice that D7 is the negative flag).
6
 After the execution of SUB, if N = 0 (or C = 1), the result is
positive.

 If N = 1 (or C = 0), the result is negative and the destination has


the 2's complement of the result.

 Normally, the result is left in 2's complement, but the NEGF


(negate, which is 2's complement) instruction can be used to
change it.

 Another SUB instruction in PIC is SUBWF


(Destination = fileReg - WREG).

 This is shown in Example 5-6 along with the NEGF instruction


7
Example 5-5
Write a program to subtract 4C - 6E.

1101 1110 (DE)


0010 0001
+1
0010 0010 (22)

After SUBWF, N= 1 (or C = 0), and the result is negative, in 2's


complement. Then it falls through and NEGF will be executed. The NEGF
instruction will take the 2's complement, and we have MYREG = 22H 8
SUBWFB (dest = fileReg- W- Borrow) subtract

with borrow
This instruction is used for multibyte numbers and will take care of the
borrow of the lower byte. If C = 0 prior to executing the SUBWFB
instruction, it also subtracts 1 from the result
 Example: 5-7 Write a program to subtract two 16- bit numbers. The numbers
are 2762H- 1296H. Assume fileReg location 6 = (62) and location 7 = (27).
Place the difference in fileReg locations 6 and 7; loc 6 should have the lower
byte

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 following arrangement for the representation of signed


positive and negative numbers:

The most significant bit (MSB) is set aside for the sign (+ or -),
while the rest of the bits are used for the magnitude.

The sign is represented by 0 for positive (+) numbers and 1 for


negative (-) numbers.
10
Signed 8-bit operands
 In signed byte operands, D7 (MSB) is the sign and DO to D6
are set aside for the magnitude of the number.

 If D7 = 0, the operand is positive

 If D7= 1, it is negative.

 The N flag in the status register is the D7 bit.

11
Negative numbers
 For negative numbers, D7 is 1; however, the magnitude is
represented in its 2 's complement.

 The assembler does the conversion, it is still important to


understand how the conversion works.

 To convert to negative number representation (2's


complement), follow these steps:

1) Write the magnitude of the number in 8-bit binary (no sign).


2) Invert each bit.
3) Add 1 to it

12
Example 5-10
Show how the PIC would represent -5.

Therefore, -5 = FBH, the signed number representation in 2's


complement for -5.

The D7 = N = 1 indicates that the number is negative 13


Example 5-11
Show how the PIC would represent -34H.

Therefore, -34 = CCH, the signed number representation in 2's


complement for 34H.

The D7 = N = 1 indicates that the number is negative 14


Example 5-12
Show how the PIC would represent -128.

Therefore, -128 = 80H, the signed number representation in 2's


complement for -128. The D7 = N = 1 indicates that the number is
negative.

Notice that 128 (binary10000000) in unsigned representation is


the same as signed -128 (binary 10000000). 15
Overflow problem in signed number
operations
When using signed numbers, a serious problem
arises. This is the overflow problem. The PIC indicates
the existence of an error by raising the OV (overflow)
flag.

What is an overflow?

If the result of an operation on signed numbers is too


large for the register, an overflow has occurred and the
programmer must be notified. Look at Example 5-13.
(Next slide)
16
Positive numbers
 The range of positive numbers that can be represented by the
format is 0 to +127.

 If a positive number is larger than +127, a 16-bit operand must


be used. Because the PIC18 does not support 16-bit data, we will
not discuss it.

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.

According to the CPU, the result is negative (N = 1), which is


wrong. The CPU sets OV= 1 to indicate the overflow error.
19
 In Example 5-13, +96 is added to +70 and the result,
according to the CPU, was -90. Why?

 The reason is that the result was larger than what


WREG could contain. Like all other 8-bit registers,
WREG could only contain up to +127.

 The designers of the CPU created the overflow flag


specifically for the purpose of informing the
programmer that the result of the signed number
operation is erroneous.

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:

1. There is a carry from 06 to 07 but no carry out of 07 (C = 0).


2. There is a carry from 07 out (C = 1) but no carry from 06 to 07

 In other words, the overflow flag is set to 1 if there is a carry


from 06 to 07 or from 07 out, but not both.

 This means that if there is a carry both from 06 to 07 and from


07 out, OV = 0.

 In Example 5-13, because there is only a carry from 06 to 07


and no carry from 07 out, OV = 1
21
Example 5-14
Observe the following, noting the role of the OV and N flags:

 The result is +126, which is wrong, and OV = 1


22
Example 5-15
 Observe the following, noting the role of the OV and N flags

0000 0010 (-2)


1111 1101
+1
1111 1110 (+2)

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.

 The AND instruction will affect the Z and N flags.

 N is D7 of the result, and Z = 1 if the result is zero.

 The AND instruction is often used to mask


(set to 0) certain bits of an operand

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..

 The OR instruc­tion will affect the Z and N flags.

 N is D7 of the result and Z = 1 if the result is zero.

 The OR instruc­tion can be used to set certain bits


of an operand to 1.

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.

 The EX-OR instruction will affect the Z and N flags. N is D7 of the


result and Z = I if result is zero. See Examples 5-19 and 5-20.

 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy