Download Number Representations and Encodings in Microcontroller Design and more Study notes Microprocessors in PDF only on Docsity!
1
ECE 372 – Microcontroller DesignAssembly Programming – Arrays
unsigned short
a[10];
for
(j=0; j<10; j++)
if
(a[j]==1) PORTT=0x04;
else
PORTT=0x00;
Array For Loop Example:}
Initialize J
Compare J to 10
If Not Less than 10,1.
End Loop
Else1.
Load a[j]
If a[j] == 11.
PORT T = 4
Else1.
PORT T = 0
Increment J
Repeat Loop (Step 2)
Programming Steps:
ECE 372 – Microcontroller DesignAssembly Programming – Arrays
unsigned
short
a[10];
for
(j=0;
j<10;
j++)
if
(a[j]==1)
PORTT=0x04;
else
PORTT=0x00;
Array For Loop Example:} 1.
Initialize J
Compare J to 10
If Not Less than 10,1.
End Loop
Else1.
Load a[j]
If a[j] == 11.
PORT T = 4
Else1.
PORT T = 0
Increment J
Repeat Loop (Step 2)
Programming Steps:
Assembly Code:
ldaa
; Initialize J
ldx
#$
; Initialize index to A[0]
Loop
:
cmpa
; Compare J to 10
bge
EndLoop
; Else !(J<10)
ldd
0,X
; load A[J]
cpd
; Compare J to 1
bne
Else
; Else !(A[J]==1)
ldab
; Value to write to PORT T
bra
EndIf
Else
:
ldab
; Value to write to PORT T
EndIf
:
stab
PTT
; Write value to PORT T
adda
; Increment J
inx
; Increment A[J]
inx
; Need to increment by 2
bra
Loop
; Repeat Loop
EndLoop
:
; do something else
Will this code work?
NO! ldd overwrite value of J
How can we correct this?
3
ECE 372 – Microcontroller DesignAssembly Programming – Arrays
unsigned
short
a[10];
for
(j=0;
j<10;
j++)
if
(a[j]==1)
PORTT=0x04;
else
PORTT=0x00;
Array For Loop Example:} 1.
Initialize J
Compare J to 10
If Not Less than 10,1.
End Loop
Else1.
Load a[j]
If a[j] == 11.
PORT T = 4
Else1.
PORT T = 0
Increment J
Repeat Loop (Step 2)
Programming Steps:
Assembly Code:
ldaa
; Initialize J
ldx
#$
; Initialize index to A[0]
Loop
:
cmpa
; Compare J to 10
bge
EndLoop ; Else !(J<10)
staa
$
; Store J to RAM
ldd
0,X
; load A[J]
cpd
; Compare J to 1
bne
Else
; Else !(A[J]==1)
ldab
; Value to write to PORT T
bra
EndIf
Else
:
ldab
; Value to write to PORT T
EndIf
:
stab
PTT
; Write value to PORT T
ldaa
$
; Read J from RAM
adda
; Increment J
inx
; Increment A[J]
inx
; Need to increment by 2
bra
Loop
; Repeat Loop
EndLoop
:
; do something else
We need to store value of J to memory
and reload it when needed.
ECE 372 – Microcontroller DesignTime for Fun (or maybe not?)^ 10 Gallon
7 Gallon
3 Gallon
5
ECE 372 – Microcontroller DesignHumor
There are 10 types of people in the world:Those who get binary and those whodon’t.
ECE 372 – Microcontroller DesignInformation vs. Data
Information
An abstract description of facts, processes or perceptions
How can we represent information?
How can we represent changing information?
We need to associate different values with different events
Data
Individual fact value or set of facts or values
Measurement or storage
1.8 Trillion
October 17, 2006
September 19
th
7
ECE 372 – Microcontroller DesignInformation vs. Data
Information vs. Data
Information is data with context or meaning
1.8 Trillion
October 17, 2006
September 19
th
Number of pennies to fill Empire State Building
How much Bill Gates earns per second:
International Talk Like a Pirate Day:Date of your first ECE 372 midterm:
ECE 372 – Microcontroller DesignData Representation
Data Representation
The same data can be represented with:
Different symbols
English, Cyrillic, Arabic
Different numeric bases
Binary, Octal, Hexadecimal, Decimal
Different formats
Little Endian, Big Endian, Binary Coded Decimal
13
ECE 372 – Microcontroller DesignData Structure (Encodings)
Converting from Decimal to Binary:
Divide decimal number by 2 and insert remainder into new binarynumber.
Continue dividing quotient by 2 until the quotient is 0.
Example: Convert decimal number 12 to binary
divide by 2
Decimal Number
Binary Number
insert remainder
Continue dividing since quotient (6) is greater than 0
divide by 2
insert remainder
Continue dividing since quotient (3) is greater than 0
ECE 372 – Microcontroller DesignData Structure (Encodings)
Example: Convert decimal number 12 to binary (cont.)
divide by 2
Decimal Number
Binary Number
insert remainder
Continue dividing since quotient (1) is greater than 0
divide by 2
insert remainder
Since quotient is 0, we can conclude that 12 is 1100 in binary
15
ECE 372 – Microcontroller DesignData Structure (Encodings)
Convert the following decimal value to a binary (zero’sand one’s) value:
Choose your answer:
A) 110110B) 100010C) 1000010D) 10100100
54
10
ECE 372 – Microcontroller DesignData Structure (Encodings)
Generally, a number can be converted from one base toanother by
Converting the number to base 10
Then, converting the base ten number to the desired base usingthe divide-by-
n method
May not always be the easiest way…
17
ECE 372 – Microcontroller DesignData Structure (Encodings)
Hexadecimal Numbers
Uses the
ten
numbers from 0 to 9 and
six
letter A to F
Each column represents a power of
16
16
16
16
16
16
16
16
0
0
5
C
0
0
A
0
0123456789 ABCDEF ??
Hexadecimal
Decimal
Binary
18
ECE 372 – Microcontroller DesignData Structure (Encodings)
16
16
16
16
16
16
16
16
0
0
5
C
0
0
A
0
= A*
3
2
+ C*
1
10
19
ECE 372 – Microcontroller DesignData Structure (Encodings)
Hexadecimal Numbers
Each position actually represents four base two positions
Used as compact means to write binary numbers
Often called just
hex
11110000
F 0
0123456789 ABCDEF ??
Hexadecimal
Decimal
Binary
20
ECE 372 – Microcontroller DesignData Structure (Encodings)
Convert the following hexadecimal value to a binary(zero’s and one’s) value:
Choose your answer:
A) 110111101010B) 110001011001C) 110010101011D) 110010111010
CAB
16
25
ECE 372 – Microcontroller DesignPositive vs. Negative Numbers
Data Ranges
Unsigned
0 to 2
n
Signed-Magnitude
n-
-1 to 2
n-
One’s Complement
n-
-1 to 2
n-
Two’s Complement
n-
to 2
n-
ECE 372 – Microcontroller DesignPositive vs. Negative Numbers
Determine the two’s complement representation forthe following decimal numbers
(assume we are using
8-bit binary numbers):
27
ECE 372 – Microcontroller DesignOverflow
Overflow
Occurs then a result cannot berepresented with given number of bits
Either the result is too large magnitude ofpositive or negative
ECE 372 – Microcontroller DesignOverflow
Detecting Overflow
Can detect overflow by detecting when the two numbers’ sign bits arethe same but the result’s sign bit is different
If the two numbers’ sign bits are different, overflow is impossible
Adding a positive and negative can’t exceed largest magnitude positive ornegative
overflow
overflow
no overflow
29
ECE 372 – Microcontroller DesignOverflow
30
ECE 372 – Microcontroller DesignBinary Coded Decimal
Binary Coded Decimal
Each digit of a decimal number is representedas a 4-bit binary number
Often used for 7-segment displays
UndefinedUndefinedUndefinedUndefinedUndefinedUndefined
7447
A
2 A
0 A
1 A
3
1010
RBIRBOLT
+V
CC
+5V
abcdefg
0110000
abcdefgdp
dp
Common-anode
LED display
+5V
7447
A
2 A
0 A
1 A
3
1010
RBIRBOLT
+V
CC
+5V
abcdefg
0110000
abcdefgdp
dp
Common-anode
LED display
+5V
BCD
Binary
10
(BCD)
10
10
(BCD)
10
31
ECE 372 – Microcontroller DesignASCII Characters
0123456789 ABCDEF
Control characters
Printable characters (96)
32
ECE 372 – Microcontroller DesignReal Numbers
Real Numbers
How can we represent a real number (i.e. numbers that containa fractional part)?
Fixed Point Numbers
Floating Point Numbers
Note: our C compiler already has built-in routines to deal withreal numbers
However the computational needs will be significant.
37
ECE 372 – Microcontroller DesignFloating Point Numbers
Floating Point Numbers
Real number representation similar to scientific notation
x = M * B
E
Base (B)
Base of the numbering systems considered
Binary (2) for computer based implementations
We will assume base of 2 for remaining description
Sign
(S)
Indicating positive of negative number
0 – Positive, 1 – Negative
0
S
E
M
ECE 372 – Microcontroller DesignFloating Point Numbers
Floating Point Numbers
Real number representation similar to scientific notation
x = M * 2
E
Mantissa
(M)
Digits corresponding to the magnitude
Stored in a normalized form, where the first bit is assumed to be 1
1 is the only possible non-zero number in binary
Remaining bits correspond to fraction values (similar to fixed point)
0
S
E
M
2
2
2
2
2
2
2
1
39
ECE 372 – Microcontroller DesignFloating Point Numbers
Floating Point Numbers
Real number representation similar to scientific notation
x = M * 2
E
Exponent
(E)
Needs to represent both positive and negative values
Stored exponent is adjust using the exponent bias
Exponent bias =
2
N-
-
, where N is the number of bits in the exponent
E
Actual
= E
Stored
Bias
Example, 8-bit exponent:
E
Bias
= 2
8-
-1 = 127
E
Stored
= 1000 0000
2
= 128
E
Actual
= 128 - 127 = 1
0
S
E
M
ECE 372 – Microcontroller DesignFloating Point Numbers
Floating Point Numbers
Example: Convert the value
118.625 to floating point representation
1. Determine sign bit:
-118.625 is negative, S = 1
2. Convert to binary:
3. Normalize number
6
4. Determine exponent
E
Stored
= E
Actual
+ E
Bias
E
Stored
10
2
31
0
S
E
M
Mantissa
41
ECE 372 – Microcontroller DesignFloating Point Numbers
Floating Point Numbers
Real number representation similar to scientific notation
x = M * 2
E
Zero
Due to assuming a leading 1 in the mantissa, we cannot directly represent the value0 using floating point
Defined special case for value of 0
Define special case: Exponent and mantissa of all 0’s corresponds to the value 0
Other special cases exist:
+/- Infinity
Demoralized value
Not a Number
0
S
E
M
ECE 372 – Microcontroller DesignFloating Point Numbers
Floating Point Numbers
IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754)
Single Precision Floating Point
Double Precision Floating Point
0
S
E
M 52
0
S
E
M
43
ECE 372 – Microcontroller DesignData vs. Information
What does the bit pattern mean: 1011 1001
Unsigned: 185 decimal
Sign-Magnitude: -57 decimal
1’s complement: -70 decimal
2’s complement: -71 decimal
Fixed point 4bit.4bit: 11.5625 decimal
BCD:
2
=B (Undefined)
2
10
ASCII: ’9’
HC12 Opcode: $39 = ADCA
(Add with Carry to A, Ext. Addressing)
ECE 372 – Microcontroller DesignInformation Representation
Text Page
1 page letter
8.5x11 inches, 60x68 12 point characters
ASCII 8bit: 60x68x8 = 32640 bits
4 Kbytes
Fax Page Scan
Fax low resolution 204x98 dots/inch
8.5x204x11x98 = 1869252 dots
235 Kbytes (B/W)
DVD
8Mbit/sec
2 hours 2x60x60x8 = 57600 Mbit
Approximately 7.2 Gbytes