Docsity

Microcomputers: Extended Precision, Signed Data & 32-bit Logic, Slides of Microcomputers

Toggle sidebar
bg1
1
Microcomputers
Extended Precision Operations, Signed
Data Operations Additional Control
Dr. Jeff Jackson Lecture 5-1Electrical & Computer Engineering – Microcomputers
Data
Operations
,
Additional
Control
Operations
Extended Precision Operations
To represent larger numbers, more bits are needed.
N bits can represent the unsigned range 0 to 2N-1.
Bytes
1B t 8bit
Unsigned Range CData Type
(PIC 24
Book’s Data
t d fi iti
1
B
y
t
e =
8
bit
s
(PIC
24
compiler)
t
ype
d
e
fi
n
iti
on
1 (8 bits) 0 to 255 unsigned char uint8
2 (16 bits) 0 to 65,535 unsigned short uint16
2 (16 bits) 0 to 65,535 unsigned int uint16
4 (32 bits)
0 to 4 294 967 295
unsigned long
uint32
Dr. Jeff Jackson Lecture 5-2Electrical & Computer Engineering – Microcomputers
4
(32
bits)
0
to
4
,
294
,
967
,
295
unsigned
long
uint32
The size of int, long depends on the Cimplementation; on some
machines both int and long are 4 bytes, with a short being 2 bytes.
On some machines a long is 8 bytes (64 bits).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Microcomputers: Extended Precision, Signed Data & 32-bit Logic and more Slides Microcomputers in PDF only on Docsity!

Microcomputers

Extended Precision Operations, Signed

Data Operations Additional Control

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Data Operations, Additional Control

Operations

Extended Precision Operations

To represent larger numbers, more bits are needed. N bits can represent the unsigned range 0 to 2 N-1. Bytes 1 B t 8 bit

Unsigned Range C Data Type (PIC 24

Book’s Data 1 Byte = 8 bits (PIC 24 t d fi iti compiler)

type definition

1 (8 bits) 0 to 255 unsigned char uint 2 (16 bits) 0 to 65,535 unsigned short uint 2 (16 bits) 0 to 65,535 unsigned int uint 4 (32 bits) 0 to 4 294 967 295 unsigned long uint

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

4 (32 bits) 0 to 4,294,967,295 unsigned long uint

The size of int , long depends on the C implementation; on some machines both int and long are 4 bytes, with a short being 2 bytes. On some machines a long is 8 bytes (64 bits).

32-bit Data Moves

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

The term ‘double word’ refers to 32-bit data. A double word mov instruction copies 32 bits of data, 16 bits at a time. Double words must start at an even memory address.

Double Word Move Example

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

32-Bit Addition, Subtraction

Operation done on least significant words first. Addition of most significant words includes CARRY.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Addition of most significant words includes CARRY. Subtraction of most significant words includes BORROW (~C).

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

32-bit Addition/Subtraction in Assembly

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

addc is add with carry subb is subtract with borrow Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Other Forms of Add/Sub Extended Precision

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Z Flag Behavior in Extended Precision

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

32-bit Non-Zero Test Using Compare

Compare 32-bit K against 32-bit zero using compare, compare ith b

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

with borrow

32-bit Non-Zero Test

A little more efficient than the previous slide. Bitwise OR the LSW and MSW; if result is non-zero, the 32-

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

bit value is zero. You CANNOT just do: mov k mov k+ bz end_if

in this case, Z flag is only based on MSW!!!!

Another Example of 32-bit Testing

Note that the logical OR operation (||) has nothing to do with the fact

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

g p (||) g that Bitwise OR is used for testing each 16-bit value for zero or non- zero!

If the code is if (k && !j), bitwise OR is still used for zero/non-zero test.

32-bit Equality

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Use same structure as used for 8/16-bit tests, except use a 32- bit comparison ( cp for LSW comparison, cpb for MSW comparison). The sticky ‘Z’ flag behavior enables this comparison.

Unsigned vs. Signed Data

The signed/unsigned modifiers in type declarations determine if the variables are treated as unsigned or signed values (signed is assumed if no modifier is present). Signed values use 2’sassumed if no modifier is present). Signed values use 2 s complement representation. C Type Book Type Size unsigned char uint8 8-bit unsigned int uint16 16-bit unsigned long uint32 32-bit signed char int8 8-bit

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

The following slides discuss how signed integers are represented in binary.

signed int int16 16-bit signed long int32 32-bit

Signed Integer Representation

We have been ignoring large sets of numbers so far; ie. the sets of signed integers, fractional numbers, and floating point numbers.

We will not talk about fractional number representation (10.3456) or floating point representation (i.e. 9.23 * 10 13 ).

We WILL talk about signed integer representation.

The PROBLEM with signed integers (-45, +27, -99) is the

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

g g ( , , ) SIGN! How do we encode the sign?

The sign is an extra piece of information that has to be encoded in addition to the magnitude. Hmmmmm, what can we do??

Signed Magnitude Representation

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Problems: two zeros (+, - zero). Also +K + (-K) does not equal zero. -5 + 5 = 0x85 + 0x 5 = 0x8A = -10 !!! Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

1's Complement Representation

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Problems: two zeros (+, - zero), and addition is off by 1 LSb if two negative numbers are added or a negative and a positive are added. Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

Signed Decimal to 2’s complement

Step 1: Ignore the sign convert the magnitude of the number to

Convert +34, -20 to 8-bit 2’s complement.

Step 1: Ignore the sign, convert the magnitude of the number to hex.

34 = 2 * 16 + 2 = 0x 20 = 1 * 16 + 4 = 0x

Step 2 (for positive decimal number): If the decimal number

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

p ( p ) was positive, then you are finished!

+34 as an 8 bit 2s complement number is 0x

Signed Decimal to Hex conversion (cont)

Step 2 (for negative decimal number): Need to do more work if decimal number was negative. To get the final representation, we will use the fact that: 0x00 - (+N) = -N

  • 0x

0x

0xEC This is the final result.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

-20 as an 8-bit 2s complement number is 0xEC

Signed Decimal to 2’s complement Summary

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

Hex to Signed Decimal Conversion Rules

Given a Hex number, and you are told to convert to a signed integer (Hex number uses 2s complement encoding)

STEP 1: Determine the sign! If the Most Significant Bit is zero, the sign is positive. If the MSB is one, the sign is negative.

0xF0 = 0b 11110000 (MSB is ‘1’), so sign of result is ‘-’ 0x64 = 0b 01100100 (MSB is ‘0’), so sign of result is ‘+’.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

If the Most Significant Hex Digit is > 7, then MSB = ‘1’ !!! (eg, 0x8,9,A,B,C,D,E,F => MSB = ‘1’ !!!)

2’s complement Hex to Decimal Summary

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

Two’s Complement Overflow

Consider two 8-bit 2’s complement numbers. I can represent the signed integers -128 to +127 using this representation.

CConsider (+1) + (+127) = +128. id (+1) + (+127) +128 ThThe number +128 is OUT b +128 i OUT of the RANGE that I can represent with 8 bits. What happens when I do the binary addition? +127 = 0x 7F

  • +1 = 0x 01

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

128 != 0x80 (this is actually -128 as a twos complement number!!! - the wrong answer!!!)

How do I know if overflowed occurred? Added two POSITIVE numbers, and got a NEGATIVE result.

Two’s Complement Overflow, Addition

Two’s complement overflow for addition occurs if:

+N + +M = -R (add two positive, get a negative) (-N) + (-M) = +R (add two negative, get a postive) CANNOT get two’s complement overflow when adding numbers of different signs. The Carry out of the MSB means nothing if the numbers are two’s complement numbers.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

In hardware, overflow is detected by the boolean equation: V = CMSB xor C (^) MSB- For N bits, CMSB = Carry out of bit[N-1] CMSB-1 = Carry out of bit[N-2]

Two’s Complement Overflow, Subtraction

Two’s complement overflow for subtraction occurs if:

-N - +M = +RN +M +R (thi(this is just -N + (-M) = +R, stated differently i j t N + ( M) +R t t d diff tl (+N) - (-M) = -R (this is just +N + (+M) = -R, stated differently

CANNOT get two’s complement overflow when subtracting numbers of the same signs.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Unsigned vs. Signed Arith. Implementation

2’s complement is nice in that the binary adder logic circuit used for unsigned numbers can also be used for 2’s complement numbers.

This is NOT TRUE for some operations.p

Operations that work differently for signed, unsigned

Operations that work the same for unsigned,unsigned

comparison(>,>=,<,<=), right shift (>>), multiplication, division

Bitwise logical, addition, subtraction, left shift (<<), equality, inequality.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

If these operations are implemented in logic gates, must use different logic networks. If implemented in assembly code, must use different sequences of instructions.

Signed Right Shift (>>)

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

C Right Shift versus C Divide by 2

C division in GNU C and Visual Studio always truncates to zero. So: i t8int8 i8 ki8_k; i8_k = -5; //this is 0xFB i8_k = i8_k/2; //returns –2, which is 0xFE

However, the right shift version is: i8_k = -5; //this is 0xFB

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

i8_k = i8_k >> 1; //returns 0xFD, which is –3! This is not an issue with unsigned numbers. This is why the right shift operation (>>) behavior in ANSI C is compiler dependent; most compilers preserve the sign bit for signed numbers but some always shift in zero. But even preserving the sign bit does not guarantee a match to C division by 2. If you need division by 2, use division by 2. If you need right shift, then use right shift.

Signed Right Shift in PIC18 Assembly

Use the signed data types of int16, int32. N t Th i ht hift ti (>>) i ANSI C i il d d t Th

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 5-

Note: The right shift operation (>>) in ANSI C is compiler dependent. The Microchip PIC24 compiler used in this book preserves the sign bit. In this class, we will assume the sign bit is preserved for signed data types.

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.