Microprocessor and Assembly Language: Lecture-2-Integer Representation
Microprocessor and Assembly Language: Lecture-2-Integer Representation
ASSEMBLY LANGUAGE
LECTURE-2-INTEGER REPRESENTATION
MUHAMMAD HAFEEZ
DEPARTMENT OF COMPUTER SCIENCE
GC UNIVERSITY LAHORE
TERMINOLOGY FOR
INTEGER DATA AND
ARITHMETIC OPERATIONS
C RANGES FOR 32-BIT
PROGRAMS
C RANGES FOR 64-BIT
PROGRAMS
GUARANTEED RANGES FOR
C PROGRAMS
ENCODING INTEGERS
We
write a bit vector as either , to denote
the entire vector, or as [x w−1 , x w−2 , . . . ,
x 0 ] to denote the individual bits within the
vector. Treating as a number written in
binary notation, we obtain the unsigned
interpretation of . In this encoding, each bit
xi has value 0 or 1, with the latter case
indicating that value 2i should be included
as part of the numeric value.
ENCODING INTEGERS
Unsigned
w 1
B2U(X ) xi 2 i
i0
Two’s Complement
w2
xi 2
w1 i
B2T (X) xw1 2
i0
Sign
Bit
UNSIGNED RANGE OF VALUES
Sign Bit
For 2’s complement, most significant bit
indicates sign
• 0 for nonnegative
• 1 for negative
ENCODING EXAMPLE
x = 15213: 00111011 01101101
y = -15213: 11000100 10010011
Weight 15213 -15213
1 1 1 1 1
2 0 0 1 2
4 1 4 0 0
8 1 8 0 0
16 0 0 1 16
32 1 32 0 0
64 1 64 0 0
128 0 0 1 128
256 1 256 0 0
512 1 512 0 0
1024 0 0 1 1024
2048 1 2048 0 0
4096 1 4096 0 0
8192 1 8192 0 0
16384 0 0 1 16384
-32768 0 0 1 -32768
Sum 15213 -15213
NUMERIC RANGES
Unsigned Values Two’s Complement Values
UMin = 0 TMin = –2w–1
000…0 100…0
UMax = 2w – 1 TMax = 2w–1 –
111…1 1
011…1
Other Values
Minus 1
Values for W = 16 111…1
Decimal Hex Binary
UMax 65535 FF FF 11111111 11111111
TMax 32767 7F FF 01111111 11111111
TMin -32768 80 00 10000000 00000000
-1 -1 FF FF 11111111 11111111
0 0 00 00 00000000 00000000
NUMERIC RANGES
W
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808
Observations C Programming
|TMin | = TMax +
#include <limits.h>
1 Declares constants, e.g.,
Asymmetric range ULONG_MAX
LONG_MAX
UMax = 2*
LONG_MIN
TMax + 1
Values platform-specific
SIGNED AND UNSIGNED
NUMERIC VALUES
X B2U(X) B2T(X) Equivalence
0000 0 0 Same encodings for
0001 1 1 nonnegative values
0010 2 2 Uniqueness
0011 3 3 Every bit pattern
0100 4 4 represents unique integer
0101 5 5 value
0110 6 6 Each representable integer
0111 7 7 has unique bit encoding
1000 8 –8 Can Invert Mappings
1001 9 –7 U2B(x) = B2U-1(x)
1010 10 –6 Bit pattern for unsigned
1011 11 –5 integer
1100 12 –4 T2B(x) = B2T-1(x)
1101 13 –3 Bit pattern for two’s comp
1110 14 –2 integer
1111 15 –1
MAPPING BETWEEN SIGNED
AND UNSIGNED NUMBERS
Two’s Complement T2U Unsigned
x T2B B2U ux
X
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 T2U 7
1000 -8 8
1001 -7 9
U2T
1010 -6 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
Mapping Signed Unsigned
Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011 3 = 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
1011 -5 +16 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
The difference is 2w
Explanation of Casting Surprises
2’s Comp. UMax
Unsigned UMax – 1
Ordering Inversion
Negative Big TMax + 1 Unsigned
Positive TMax TMax Range
2’s Comp.
Range 0 0
–1
–2
TMin
Signed vs. Unsigned in C
Constants
By default are considered to be signed integers
0U, 4294967259U
Casting
Explicit casting between signed & unsigned same as U2T and
T2U
int tx, ty;
unsigned ux, uy;
tx = (int) ux;
uy = (unsigned) ty;
Implicit casting also occurs via assignments and procedure
calls
tx = ux;
uy = ty;
Signed vs. Unsigned in C
T2U32(−1) = UMax32 = 232 − 1
U2T32 (231 ) = − 231 = TMin32
The C standard does not specify a particular
representation of signed numbers, almost all
machines use two’s complement. Generally,
most numbers are signed by default. For
example, when declaring a constant such as
12345 or 0x1A2B, the value is considered
signed. Adding character ‘U’ or ‘u’ as a
suffix creates an unsigned constant, e.g.,
12345U or 0x1A2Bu.
2’s Comp.
Range 0 0
–1
–2
TMin
Why Should I Use Unsigned?
Don’t Use Just Because Number Nonnegative
Easy to make mistakes
unsigned i;
for (i = cnt-2; i >= 0; i--)
a[i] += a[i+1];
Can be very subtle
#define DELTA sizeof(int)
int i;
for (i = CNT; i-DELTA >= 0; i-= DELTA)
. . .
Do Use When Performing Modular Arithmetic
Multiprecision arithmetic
Do Use When Using Bits to Represent Sets
Logical right shift, no sign extension
Why Should I Use Unsigned?
Expanding of a Bit-Representation
of a number
One common operation is to convert between integers
having different word sizes while retaining the same
numeric value.
Of course, this may not be possible when the
destination data type is too small to represent the
desired value.
Converting from a smaller to a larger data type ,
however , should always be possible.
• • •
X • • • • • •
k w
Sign Extension Example
short int x = 15213;
int ix = (int) x;
short int y = -15213;
int iy = (int) y;
x 10011101
+ ~x 01100010
Increment -1 11111111
~x + x == -1
~x + x + (-x + 1) == -1 + (-x + 1)
~x + 1 == -x
Warning: Be cautious treating int’s as integers
OK here
Comp. & Incr. Examples
x = 15213
Decimal Hex Binary
x 15213 3B 6D 00111011 01101101
~x -15214 C4 92 11000100 10010010
~x+1 -15213 C4 93 11000100 10010011
y -15213 C4 93 11000100 10010011
0
Decimal Hex Binary
0 0 00 00 00000000 00000000
~0 -1 FF FF 11111111 11111111
~0+1 0 00 00 00000000 00000000
Unsigned Addition
Consider two nonnegative integers x and y,
such that 0 ≤ x, y ≤ 2w − 1. Each of these
numbers can be represented by w-bit unsigned
numbers. If we compute their sum, however, we
have a possible range 0 ≤ x + y ≤ 2w+1 − 2.
u v u v 2w
UAdd w (u,v) w
u v 2 u v 2w
Unsigned Addition
An arithmetic operation is said to overflow when the
full integer result cannot fit within the word size
limits of the data type.
Treat
PosOver 1 011…1
remaining bits
TAdd(u , v)
–2w –1–1 100…0
Values
4-bit two’s comp. TAdd4(u , v)
Range from -8 to
+7
Wraps Around 8
6
If sum 2 w–1 4
Becomes 2
0
negative -2 4
6
At most once -4
0
2
-6
If sum < –2w–1 -8 -4
-2
v
-8
Becomes positive -6
-4
-2
0
-6
2 -8
At most once u 4
6 PosOver
Unsigned Multiplication in C
u • • •
Operands: w bits
* v • • •
True Product: 2*w bits u · v • • • • • •
UMultw(u , v) • • •
Discard w bits: w bits
Standard Multiplication
Function
Ignores high order w bits
Implements Modular
Arithmetic
UMultw(u , v) = u · v mod 2w
Signed Multiplication in C
u • • •
Operands: w bits
* v • • •
True Product: 2*w bits u · v • • • • • •
TMultw(u , v) • • •
Discard w bits: w bits
Standard Multiplication
Function
Ignores high order w bits
Some of which are
different for signed vs.
unsigned multiplication
Lower bits are the same
Multiplying By Constants
On most machines integer multiplication is too slow,
requiring 10 clock cycles.
Whereas other integer operations—such as addition,
subtraction, bit-level operations, and shifting—
require only 1 clock cycle.
As a consequence, one important optimization used by
compilers is to attempt to replace multiplications by
constant factors with combinations of shift and
addition operations.
??????????????????????????