27
Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

Embed Size (px)

Citation preview

Page 1: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

Embedded SystemSpring, 2011Lecture 10: Arithmetic, Logic Instruction and Programs

Eng. Wazen M. Shbair

Page 2: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

2IUG- Embedded System

Today’s Lecture

Arithmetic Instructions Signed Number Concepts and Arithmetic

Operations Logic and Compare Instructions

Page 3: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

3

Arithmetic Instructions

Unsigned numbers are defined as data in which all the bits are used to represent data (no bits are set aside for neg. or pos. sign).

Addition of unsigned numbers ADDLW k ADDWF fileReg, d, a ADDWFC (adding two 16-bit numbers)

What happens to flag register?

Page 4: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

4

Addition of unsigned numbers

Page 5: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

5

ADDWFC & addition of 16 bit number

When adding two 16-bit data operand, we need to be concerned with the propagation of a carry form lower byte to higher byte.

Multi byte addition vs. individual byte addition ??

Page 6: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

6

ADDWFC

Page 7: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

7

BCD Number System

We use the digits 0 to 9 in everyday Binary Coded Decimal (BCD)

Unpacked BCD The lower 4 bits is just used Requires 1 byte Ex. (0000 0010)

Packed BCD A single byte has two BCD numbers Efficient in storing data Ex. ( 0101 0010)

Page 8: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

8

BCD Number System

What is the result if you add 0x17

0x28

0x3F

This is not BCD number

To convert it to BCD add 6

The result will be : 45

Page 9: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

9

DAW, Decimal Adjust WREG

Works only with WREG Add 6 to the lower or higher nibble if needed. After execution

If the lower nibble is greater than 9, or if DC = 1, add 0110 to the lower nibble.

If the upper nibble is greater than 9, or if C =1, add 0110 to the upper nibble.

Doesn’t require the use of arithmetic instructions prior the DAW execution.

Page 10: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

10

DAW, Decimal Adjust WREG

Page 11: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

11

DAW, Decimal Adjust WREG

Page 12: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

12

Subtraction of unsigned numbers

Subtracter circuit is cumbersome. (Why?) PIC performs the 2’s complement then uses

adder circuit to the result. Take one Clock Cycle. There are four sub instructions

SUBLW k (k – WREG) SUBWF f d ( destination = fileReg – WREG) SUBWFB (subtract with borrow ) SUBFWB (subtract with borrow )

Page 13: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

13

Subtraction of unsigned numbers

Page 14: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

14

Page 15: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

15

Multiplication of unsigned number

PIC supports byte-by-byte multiplication One of the operand must be in WREG and the

other operand is literal K value. After multiplication, the result is stored in SFR

registers PRODH and PRODL (16 bit)

Page 16: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

16

Division of unsigned numbers

There is no single instruction for the division of byte/byte numbers.

You need to write a program. Repeated subtraction The numerator is place in a fileReg Denominator is subtracted from it repeatedly The quotient is the number of times we subtracted The reminder is in fileReg upon completion

Page 17: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

17

Example

Page 18: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

18

Signed Number and Arithmetic Operations

The Most Significant Bit (MSB) is set aside for the sign (+ or -) (0 for positive & 1 for negative)

The rest, 7 bits, are used for the magnitude. To convert any 7-bit positive number to negative

use the 2’s complement.

Page 19: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

19

Signed Number

You have 128 negative numbers and 127 positive numbers

Page 20: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

20

Signed Number

Page 21: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

21

Logic and Compare Instructions

Widely used instructions ANDLW k ANDFW FileReg, d IORLW k IORFW FileReg, d XORLW k XORFW FileReg, d

Effect only Z and N Flags

Page 22: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

22

Example

Page 23: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

23

Page 24: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

24

Complement Instructions

COMF FileReg,d Takes the 1’s complement of a file register Effect only Z and N Flags

NEGF FileReg Takes the 2’s complement of a file register Effect all Flags

Page 25: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

25

Compare Instructions

Page 26: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

26

Compare Instructions

Page 27: Embedded System Spring, 2011 Lecture 10: Arithmetic, Logic Instruction and Programs Eng. Wazen M. Shbair

27IUG- Embedded System 27

References

Jie Hu , ECE692 Embedded Computing Systems , Fall 2010.

PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. McKinlay and D. Causey, Prentice Fall, 2008.

Eng. Husam Alzaq, Embedded System Course, IUG, 2010