Download Ch.3 Representation and Manipulation of Information

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Ch.3 Representation and
Manipulation of Information
From Introduction to Embedded
Systems: Interfacing to the
Freescale 9s12 by Valvano, published
by CENGAGE
3.1 Precision
• The number of distinct or different values
(page 57).
• Alternatives—the total number of
possibilities—decimal digits, bytes, or
binary bits.
• Table 3.1 shows the relationship between
bits, bytes and alternatives.
Checkpoint
• Checkpoint 3.1 How many bytes of
memory would it take to store a 50 bit
number?
Precision (cont.)
• Decimal digita are used to specify
precision of measurement systems that
display results as numerical values.
• Table 3.2 (page 58 of text).
• (1/2) decimal digit –a digit that can be 0 or
1.
• Abbreviations for large numbers (Table
3.3).
Checkpoints
• Checkpoint 3.2: How many binary bits is
equivalent to 3 and (1/2) decimal digits.
• Checkpoint 3.3: About how many
decimal digits is 64 binary bits? You can
this without a calculator, just using the
“rule of thumb” (see page 58).
• Checkpoint 3.4 A 2 tebibyte storage
system can store how many bytes?
3.2 Boolean Information
• Two states—logical true and false.
• When interfacing to a light, motor, or a
heater, the Boolean could mean on and
off.
• Positive logic—False is all zeros; true is
any nonzero value.
• Negative logic—absence of a voltage is
true and the presence of a voltage is false.
3.3 8-bit numbers
• Value of an unsigned number for 8-bits
– N = 128*b7 + 64*b6 + 32*b5 + 16*b4 + 8*b3 +
4* b2 + 2*b1 + b0.
– Table 3.4 (page 61 of the text)—shows
examples of conversions.
Conversion and Basis Elements
• The basis of a number sysstem is a subset from
which linear combinations of the basis elements
can be used to construct the entire set.
• Algorithm:
–
–
–
–
–
Start with MSB.
Do we need the basis element for the number?
If yes, then the bit is a 1—if no, then it is a 0.
Continue to the next basis element.
See Table 3.5, page 61 for an example.
Checkpoints
• Checkpoint 3.6: Convert the binary number
%01101010 to unsigned decimal.
• Checkpoint 3.7: Convert the hex number $45
to unsigned decimal.
• Checkpoint 3.8: In this conversion algorithm,
how can we tell if a basis element is needed?
• Checkpoint 3.9 Give the representations of the
decimal 45 in 8-bit binary and hexadecimal
• Checkpoint 3.10 Give the representations of the
decimal 200 in 8-bit binary and hexadecimal
Other Number Schemes for
Negative Number Representation
• One’s complement — complement each bit.
– 0001 1001 –the one’s complement is 1110 0110—this
is the negative of the number.
– Problems: two representations for 0 and no basis
elements.
• Two’s complement — complement each bit
then add 1 to the result.
– 0001 1001 when negated becomes 1110 0111.
– N = -128*b7 + 64*b6 + 32*b5 + 16 * b4 + 8 * b3 +
4*b2 + 2*b1+ b0.
Checkpoints
• Checkpoint 3.11 Convert the signed
binary number%11101010 to signed
decimal.
• Checkpoint 3.12 Are the signed and
unsigned decimal representations of the 8bit number $45 the same or different?
Other Conversion Techniques
•
•
Table 3.7 illustrates conversion of -100 to
signed 8-bit binary (page 63).
Other techniques
1. Convert them into unsigned binary, then do
a two’s complement negate.
2. Add 256 to the number, then conert the
unsigned result to binary using the unsigned
method.
Checkpoints
• Checkpoint 3.13: Give the
representations of -45 in 8-bit binary and
hexadecimal.
• Checkpoint 3.14: Why can’t you
represent the number 200 using 8-bit
signed binary?
Other schemes
•
Sign-Magnitude Representation --if b7
is a 1, then the number is negative.
– Problems:
1. No basis function
2. Two representations for the number zero.
3. Different hardware is needed for addition and
subtraction (unlike two’s complement).
•
Binary Coded Decimal (BCD)—easy for
humans to read—each decimal digit is
represented by a 4-bit binary.
Checkpoint
• Checkpoint 3.15: What binary values
are used to store the number 25 in 8-bit
BCD format?
3.4 16-bit Numbers
•
•
•
•
•
Word or double byte.
Extension of the 8-bit concept.
See Figure 3.4 (page 64).
See Table 3.9 (page 65).
Two’s Complement—Table 3.10.
Checkpoints
• Checkpoint: 3.16: Convert the 16-bit binary number %0010 0000
0110 1010 to unsigned decimal.
• Checkpoint 3.17: Convert the 16-bit hex number $1234 to
unsigned decimal.
• Checkpoint 3.18: Convert the unsigned decimal number 1234 to
16-bit hexadecimal.
• Checkpoint 3.19: Convert the unsigned decimal number 10000 to
16-bit binary.
• Checkpoint 3.20: Convert the 16-bit hex number $1234 to signed
decimal.
• Checkpoint 3.21: Convert the 16-bit hex number $ABCD to signed
decimal.
• Checkpoint 3.22: Convert the signed decimal number 1234 to 16-bit
hexadecimal.
• Checkpoint 3.23: Convert the signed decimal number -100000 to
16-bit binary.
3.5 Extended Precision Numbers
• Unsigned numbers with n bits (see page
66).
• Two’s complement n-bit numbers.
• Binary Coded Decimal n-bit numbers.
Checkpoint
• Checkpoint 3.24: What hexadecimal
values are used to store the number 3456
in 16-bit BCD format?
3.6 Logical Operations
• Unary Operations—produces its result
given a single input parameter—negate,
increment, decrement.
• Logical Not Operation—Figure 3.5.
• Binary Operations—produce a single
result given two inputs—AND(&), OR(|),
and exclusive OR(^)—Table 3.12, Figure
3.6.
Logical Operations and the 9S12
• Operations are performed in a bit-wise
fashion.
• N bit will be set if the result is negative.
• Z bit will be set if the result is zero.
• Logical operations at bottom of page 68
will clear the V bit (signed overflow) and
leave the C bit unchanged.
Examples (pages 69-74)
• 3.1 Write software to set bit 4 and clear bits 1 and 0 of an 8-bit
variable N. (page 69).
• 3.2 Write software that sets a global variable to true if a switch is
pressed.
• 3.3 Write software that make PT4 and PT5 outputs and clears both
outputs without affecting the other bits of PTT.
• 3.4 Write software that togles the PT 3 output without affecting the
other bits of PTT.
• 3.5 Generate two out-of-phase square waves a shown in Figure 3.8
(page 72).
• 3.6 The goal is develop a means for the microcontroller to turn on
and turn off an AC-powered appliance. The interface will use a
solid-state relay with a control parameters of 2 V and 10 ma. Write
necessary subroutines to operate the system.
Digital Storage Elements
• Figure 3.11— (page 74)
• Table 3.14—D flip-flops
3.7 Shift Operations
• In assembly language, the shift is a unary
operation and is for one bit.
–
–
–
–
lsr – logical shift right
asr– arithmetic shift right
lsl– logical shift left
asl – arithmetic shift left
• C will contain the carry out.
• Figure 3.13 – 3.16 illustrates the operations.
• Roll (ror, rol) — operations can be used to
create multiple-byte shift functions (Fig. 3.7).
• See page 77 for a list with related registers.
Example 3.7
• Write assembly code to implement
M=N>>2, where M and N are 16-bit
unsigned variables.
• Solution—
–
–
–
–
ldd N
lsrd
lsrd
std M
Checkpoint
• Checkpoint 3.31: Let N and M be 8-bit
signed locations. Write assembly code to
implement M = 4*N.
Example 3.8
• Take two 4-bit nibbles and combine them
into one 8-bit value.
• Solution—Use the shift operation to move
the bits into position, then use the or
operation to combine the two parts into
one number.—See page 78.
3.8 Arithmetic operations: Addition
and Subtractions.
• Operations are performed using hardware.
• Overflows occur and have to be checked.
Checkpoints
• Checkpoint 3.32: How many bit does it take to
store the result of two unsigned 8-bit numbers
added together?
• Checkpoint 3.33: How many bits does it take to
store the result of two signed 8-bit numbers
added together?
• Checkpoint 3.34: How many bits does it take to
store the result of two unsigned 8-bit numbers
multiplied together?
• Checkpoint 3.35: How many bit does it take to
store the result of two signed 8-bit numbers
multiplied together?
3.8 Arithmetic Operations (cont.)
• Four of the condition code bits stored in
the Condition Code Register (CCR) are
used in Addition/Subtraction.
• See Table 3.16, page 79.
• The adda and addb instructions work for
both signed and unsigned data.
• N, Z, V (signed overflow), and C (unsigned
overflow) are set as shown (page 79.
Example 3.9 (page 79)
• Write assembly code to implement M =
N+10, where M and N are 8-bit variables.
• Solution:
– Perform an 8-bit read to get N into RegA.
– 10 is added to Reg A.
– Result is stored in M.
• C and V bits are set when overflows occur
on unsigned and signed operations.
Arithmetic Operations –16 bit
numbers
• The addd instruction can be used to add
16 bit numbers as discussed at the top of
page 80.
• N, Z, V, and C are set as needed (see
page 80).
Example 3.10
• Write assembly code to implement M = N +
1000, where M and N are 16-bit variables.
• Solution
–
–
–
–
–
Need to use 16-bit register (D).
16 bit read to get N (use ldd N).
Add 1000 (addd #1000).
Store the result in M ( std M).
Check C or V (whichever is appropriate) for possible
overflows.
Checkpoint
• Checkpoint 3.36: Wrie assembly code
that adds a constant 100 to Register X.
Subtraction and Compare
• The instructions at the bottom of page 80
show that compare instructions subtract a
value from memory.
• The other instructions (subtraction and
test) all subtract values as shown.
• 16-bit instructions are shown on page 81.
• Note that the programmer keeps track of
the values being signed or unsigned, since
the computer sets both C and V.
Example 3.11
• Write assembly code to implement M = N10, where M and N are 8-bit variables.
• Solution (page 81)
–
–
–
ldaa N
suba #10
staa M
Example 3.12
• Write assembly code to implement M = N1000, where M and N ar 16-bit variables.
• Solution: (page 81)
–
–
–
ldd N
subd #1000
std M
• Object and source code are illustrated on
page 82.
Adder/Subtractor Hardware
• Figure 3.18 shows a binary full adder.
• Figure 3.19 shows an 8-bit adder using 8 full-adders.
• Consider the operation adda #64
– The contents of RegA and constant binary 64 are placed at the
inputs of the hardware.
– The result is placed in Reg A.
– Condition codes are set.
• Figure 3.20 Shows a number wheel description.
• Figure 3.21 Shows how a two’s complement approach to
subtraction can use full-adders.
• Figure 3.22 Shows a number wheel for subtraction.
• More number wheels—Fig. 3.23, Fig. 3.24.
Checkpoints
• Checkpoint 3.37 Assume Register A is initially -100.
After executing the instruction adda #64 what is the
value in Register A, and the NZVC bits.
• Checkpoint 3.38 Assume Register A is initially -100.
After executing the instruction adda #64 what is the
value in Register A, and the NZVC bits.
• Checkpoint 3.39 Assume Register A is initially 200.
After executing the instruction suba #-64 what is the
value in Register A, and the NZVC bits?
• Checkpoint 3.40 Assume Register A is initially 200.
After executing the instruction suba #64 what is the
value in Register A, and the NZVC bits.
Error Handling
• Usually only have to deal with C or V.
• Promotion involves the increasing of the
precision of the input numbers. (Page 88)
• Ceiling and floor —establishing upper
and lower bounds for the result of an
operation. (Page 90).
3.9 Arithmetic Operations:
Multiplication and Divide
• As an embedded programmer, it is important to
understand the strengths and weaknesses of the
computers used.
• Many embedded computers have a limited
ability for mathematical operations (page 92).
• If precision is not supported, then a different
processor is needed (for speed) or develop
software algorithms for extended precision
(slower).
• A combination of shifts and additions can be
used.
Example 3.13
• Write assembly code to implement
unsigned M = 5*N + 25 where M is 16 bits
and N is 8 bits.
• Solution:
–
–
–
–
–
ldaa N
; 0 to 255
ldab #5
mul
; RegD = 5*N, 0 to 1275
addd #25 ;RegD+5*N+25, 25 to 1300
std M
Example 3.14
• Write assembly code to implement M =
2.3*(N + 5.5), where M is 16 bits and N is
8 bits.
• Solution
– (use integer operations)—page 96.
– Use the idiv instruction (bottom of page 95)
Example 3.15
• Write assembly code to scale an unsigned
8-bit integer into a number from 0 to 500.
• Solution
– (see page 96)
Example 3.16
• Write assembly code to implement M =
12.34*N, where M and N are unsigned 16
bits.
• Solution
– Page 97
– fdiv instruction is used--(performs a 16-bit
by 16-bit unsigned divide)
3.10 Character Information
• ASCII—American Standard Code for Information
Interchange.
• Usually 7 bits with 8th bit (MSB) set to 0.
• See Table 3.21 (page 98).
• ISO/IEC 8859 uses the 8th bit to define additional
characters.
• Unicode Standard—handles some ambiguities,
but is more complex.
• Figure 3.33 illustrates the concept of “nulltermination for storage of a string of ASCII.
3.11 Conversions
• C-examples illustrate the conversion
process.
3.12 Debugging Monitor Using a
LED
• Monitors are used in real-time systems as
a debugging tool (page 102).
• An LED attached to a port is an example
of a “Boolean” monitor.
Checkpoints
• Checkpoint 3.45: How is the character 0
represented in ASCII?
• Checkpoint 3.46: Assume Register A
contains an ASCII code 0 to 9. Write
assembly code that converts the ASCII
code into the corresponding decimal
number.