Signed Data Operations (Chapter 5)
Bytes Unsigned Range C Data Type Book’s Data
1 Byte = 8 bits (PIC 24 type definition
compiler)
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
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).
1
Slide credit: Reese
Unsigned vs. Signed Data
The signed/unsigned modifiers in type declarations determine if
the variables are treated as unsigned or signed values. Signed
values use sign-2’s complement representation.
C Type Textbook Type Size
unsigned char uint8 8-bit
unsigned int uint16 16-bit
unsigned long uint32 32-bit
signed char int8 8-bit
signed int int16 16-bit
signed long int32 32-bit
1
PIC24 extended precision
The PIC24 instruction set includes instructions for
manipulating single-bit, 8-bit, 16-bit data types. For greater
than 16 bits, some instructions can be extended to double words
(32 bits). For example:
mov.d Ws, Wnd does (Ws:Ws+1) Wnd:Wnd+1
We can also manipulate larger data types piece-by-piece. For
example, 32-bit addition:
0x A734 F082
+ 0x 13C0 4370
______________
= 0x BAF5 33F2
Note: we will talk about fractional number representation (10.3456) or
floating point representation (i.e. 9.23 * 1013) and fixed point operations 3
later in the course.
Signed Integer Representation
We have been ignoring large sets of numbers so far; i.e. 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 * 1013).
We WILL talk about signed integer representation.
The PROBLEM with signed integers ( - 45, + 27, -99) is the
SIGN! How do we encode the sign? Of course, that means
we will use sign- 2’s complement!
4
Slide credit: Reese
2
2's Complement Representation
This the standard way of representing signed integers !!
5
Copyright Thomson/Delmar Learning 2005. All Rights Reserved. Slide credit: Reese
A common Question from Students
Given a hex number, how do I know if it is 2’s
complement OR is it already in 2’s complement or do I
have to convert it to 2’s complement, ….. ?????? etc.
A Hex or binary number by itself can represent
ANYTHING (unsigned number, signed number,
character code, color codes, etc). You MUST
HAVE additional information that tells you what
the encoding of the bits mean!
3
4-bit
Binary outside
example
the circle
Inside the circle:
Decimal equivalent, if
binary is interpreted as
Sign-2’s complement,
Range is +7 to -8
overflow boundary
Observations ?
0, -1
Sign extensions
3-bit: -4 2 1
4-bit: -8 4 2 1
8-bit: -128 64 32 16 8 4 2 1
4
Adding Precision (unsigned)
What if we want to take an unsigned number and add more
bits to it?
Just add zeros to the left:
128 = 0x80 (8 bits)
= 0x0080 (16 bits)
= 0x00000080 (32 bits)
9
Slide credit: Reese
Adding Precision (2’s complement)
What if we want to take a 2's Complement number and
add more bits to it?
Take the SIGN BIT, and extend it to the left.
-128 = 0x80 = 0b 10000000 (8 bits)
= 0xFF80 = 0b 1111111110000000 (16 bits)
= 0xFFFFFF80 (32 bits)
+ 127 = 0x7F = 0b 01111111 (8 bits)
= 0x007F = 0b 0000000001111111 ( 16 bits)
= 0x0000007F (32 bits)
This is called SIGN EXTENSION. Extending the MSB to
the left works for two’s complement numbers and
unsigned numbers. 10
Slide credit: Reese
5
Example Conversions
0xFE as an 8 bit unsigned integer = 254
0xFE as an 8 bit 2's Complement integer = -2
0x7F as an 8 bit unsigned integer = 127
0x7f as an 8 bit 2's Complement integer = +127
To do hex to signed decimal conversion, we need to
determine sign (Step 1), determine Magnitude (step 2),
combine sign and magnitude (Step 3)
11
Slide credit: Reese
Signed Decimal to 2’s complement
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 = 0x22
20 = 1 * 16 + 4 = 0x14
Step 2 (for positive decimal number): If the decimal number
was positive, then you are finished!
+34 as an 8 bit 2s complement number is 0x22
12
Slide credit: Reese
6
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 0x00
- 0x14
0xEC This is the final result.
-20 as an 8-bit 2s complement number is 0xEC
13
Slide credit: Reese
Signed Decimal to 2’s complement
Summary
14
Copyright Thomson/Delmar Learning 2005. All Rights Reserved.
7
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 ‘+’.
If the Most Significant Hex Digit is > 7, then MSB = ‘1’ !!!
(eg, 0x8,9,A,B,C,D,E,F => MSB = ‘1’ !!!)
15
Slide credit: Reese
Hex to Signed Decimal (cont)
STEP 2 (positive sign): If the sign is POSITIVE, then just
convert the hex value to decimal.
0x64 is a positive number, decimal value is
6 * 16 + 4 = 100.
Final answer is +100.
0x64, a 8-bit 2's Complement integer, in decimal is +100
16
Slide credit: Reese
8
Hex to Signed Decimal (cont)
STEP 2 (negative sign): If the sign is Negative, then need to
compute the magnitude of the number.
We will use the fact that 0 - (-N) = + N, which is the magnitude!
0x00
- 0xF0
0x10 = 16
STEP 3 : Just combine the sign and magnitude to get the result.
0xF0, an 8-bit 2's Complement integer, is decimal -16
0x64, an 8-bit 2's Complement integer, is decimal +100
17
Slide credit: Reese
2’s complement Hex to Decimal Summary
18
Copyright Thomson/Delmar Learning 2005. All Rights Reserved.
9
Two’s Complement Overflow
Consider two 8-bit 2’s complement numbers. I can represent the
signed integers -128 to +127 using this representation.
Consider (+1) + (+127) = +128. The number +128 is 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
-------------------
128 != 0x80 (this is actually -128 as a 2’s
complement number!!! - the wrong answer!!!)
How do I know if overflowed occurred? Added two
POSITIVE numbers, and got a NEGATIVE result.
19
Slide credit: Reese
2’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 positive)
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.
In hardware, overflow is detected by the Boolean equation:
V = CMSB xor C MSB-1
For N bits, CMSB = Carry out of bit[N-1]
CMSB-1 = Carry out of bit[N-2] 20
Slide credit: Reese
10
2’s Complement Overflow, Subtraction
Two’s complement overflow for subtraction occurs if:
-N - +M = +R (this is just -N + (-M) = +R, stated differently
(+N) - (-M) = -R (this is just +N + (+M) = -R, stated differently
CANNOT get two’s complement overflow when subtracting
numbers of the same signs.
21
Slide credit: Reese
Some Examples
22
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
11
Unsigned vs. Signed 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:
Operations that work differently Operations that work the same
for signed, unsigned for unsigned,unsigned
comparison(>,>=,<,<=), Bitwise logical, addition,
right shift (>>), subtraction, left shift (<<),
multiplication, division equality, inequality.
If these operations are implemented in logic gates, must use
different logic networks. If implemented in assembly code,
must use different sequences of instructions.
23
Slide credit: Reese
Signed Right Shift (>>)
(asr)
24
Copyright Delmar Cengage Learning 2009. All Rights Reserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
12
C Right Shift versus C Divide by 2
C division in GNU C and Visual Studio always truncates to zero.
So:
int8 i8_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
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.
Slide credit: Reese
Signed Right Shift in PIC24 Assembly
Use the signed data types of int16, int32.
Note: The right shift operation (>>) in ANSI C is compiler dependent.
The Microchip PIC24 compiler we are using, preserves the sign bit.
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
26
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Slide credit: Reese
13
Signed Left Shift (<<)
There is no need for signed left shift. If the sign bit changes
due to the shift operation, then overflow occurs!
0x20 << 1 == 0x40
+32 * 2 = +64
0 0 1 0 0 0 0 0 0x20
no overflow, +64 can
be represented in 8 bits 0 1 0 0 0 0 0 0 0x40
+64 * 2 = +128 0x40 << 1 == 0x80 = -128
overflow!! +128 cannot be 0 1 0 0 0 0 0 0 0x40
represented in 8 bits!
Multiplied positive number
1 0 0 0 0 0 0 0 0x80
by 2, got a negative number!
27
Slide credit: Reese
Signed Branches (cont)
Signed branches are used for Signed comparisons and test one or
more flags, depending on the comparison
Description Syntax Branch taken when
Branch >, signed BRA GT, label (~Z & N & OV) |
(~Z & ~N & ~OV)
Branch >=, signed BRA GE, label ( N & OV) | (~N & ~OV)
Branch <, signed BRA LT, label ( N & ~OV) | (~N & OV)
Branch <=, unsigned BRA LE, label ( N & ~OV) |
(~N & OV ) | Z
Use a Compare instruction to affect the flags before using a
signed branch.
28
Slide credit: Reese
14
PIC24 Signed Compare
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
29
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Slide credit: Reese
Mixed Signed 8/16-bit Computations
30
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”. Slide credit: Reese
15
branch versus goto
Recall that a goto used two instruction words which encoded a 23-bit value
that is loaded directly into the PC. A branch takes 1 instruction word, whose
format is:
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
31
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
branch Machine Code example
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
32
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Slide credit: Reese
16
branch, goto Pros/Cons
The branch instructions use a 16-bit offset. This means the
target address must be within –32768 to +32767 instruction
words from the branch.
A branch has limited range. This is ok, most loops are not that
large.
The advantage of a goto is that it can jump anyway in program
memory.
The advantage of a branch is it only takes one instruction word.
33
Slide credit: Reese
What do you need to know?
• 2’s complement notation
• conversions
• Detecting overflow
• Arithmetic shift right
• Signed comparison
• How to determine the machine code for a
branch instruction.
• Branch vs goto
34
Slide credit: Reese
17