Summary of Mathematical Concepts used in Computer Interfacing(An Example Problem Set)
Mathematics with different Based Numbers
2's Complement and Arithmetic Operations
GeneralProcedure to Find "Complements"
Binary Subtraction using 2's Complement
Subtraction Process in Modulo-2 Arithmetic
Mathematics with Different Based Numbers (return)
Working with binary limit sensor or the interface chips or boards
that are associated with them is facilitated if you have a comfortable knowledge
level of binary, hexadecimal and octal numbers. This familiarity
includes knowing how a computer deals with binary mathematics. Such skills will be used;when interpreting user manuals for equipment that has an interface card
to a computer or other computer based equipment;when trouble shooting the interface between a computer and its peripherals
when interrupting the equipment's automatic operation to insert a specific
set of commands you believe are appropriate for that equipment at this time.when designing instrument systems that must have their own computer based
interface to other parts of the system.The following text summarizes some of the math issues when dealing with binary numbers
and binary related arithmetic.This text is primary a show by simple example problem dialog. It is hoped
that this communication method is a fast and easy way for you to acquire the
fundamental concepts associated with the topic. Answers to these example problems
are provided but working a problem out before an answer is checked always has merit. (return)2's COMPLEMENT AND ARITHMETIC OPERATIONS
Microprocessors perform the subtraction of two binary numbers using
the 2's complement method. In digital logic circuits, it is easier to design
a circuit to add numbers than to design a circuit to subtract numbers.
The 2's complement of a binary number is equivalent to its negative
number; thus, by adding the complement of the subtrahend (the number to
be subtracted) to the minuend. a subtraction can be performed.
The method of 2' s complement is explained below with examples from the
decimal number system. Example problems 4, 5 and 6 bring your attention to
the properties of 2's complement arithmetic. (return)
GENERAL PROCEDURE TO FIND "COMPLEMENT'S"
OF A BINARY NUMBER
A microprocessor like the Z80 or any of the Intel processors can be designed to
quickly calculate the complements of binary numbers. The 1's complement and
the 2's complement are often used in computer calculations as well as data manipulation.
Both complements can be obtained with the completion of a two step procedure.Step 1: Find 1's complement. This amounts to replacing 0 by 1 and 1 by 0.
Step 2: To find 2's complement. add 1 to the 1's complement. This is similar to
the procedure of l0's complement. (return)
BINARY SUBTRACTION USING 2's COMPLEMENT
As suggested above, computer based mathematics does prefer addition over
subtraction. Therefore, every cpu will do subtraction via an addition procedure.
This statement is not an example of contraction but a testimony of how clever
cpu's, ( with a little help from their human designers of course) can be. Binary
subtraction is performed by a microprocessor using the 2's complement
method. Naturally, positive or negative numbers can result from any subtraction.
However, when result is negative, the cpu expresses that answer in terms of its 2's complement. (return)
SIGNED NUMBERS
Arithmetic operations can also be performed with all positive numbers, all (return)
negative numbers, or a mixture of positive and negative numbers. To perform
the arithmetic operations with signed numbers (positive and negative), the sign
must be indicated as well as the magnitude of the number. In 8-bit microprocessors,
bit D7 is used to indicate the sign of a number, 0 in D, indicates a positive number
and 1 indicates a negative number. Bit D7 can be used to indicate the sign of a
number because:1. The CPU performs the subtraction of two numbers using 2's complement
and, if the result is negative, it saves (shows) the result in the form of
2's complement.2. 2's complements of all the 7-bit numbers have 1 in bit place D7.
When a programmer uses bit D7 to indicate the sign of a number, the magnitude
of the number can be represented by seven bits (D6 - D0). For example,
number 74 Hex is represented with a sigh as follows:D7 D6 D5 D4 D3 D2 D1 D0
+74 = 0 1 1 1 0 1 0 0
- 74 = 1 0 0 0 1 1 0 0
sign magnitudeHowever, the microprocessor cannot differentiate between a positive
number and a negative number. For example, in the above illustration,
-74 Hex can be interpreted as the unsigned positive number 8C Hex or just
the bit pattern shown. It is the responsibility of the programmer to provide the
necessary interpretation. (return)
SUBTRACTION PROCESS IN THE Z80 MICROPROCESSOR
The Z80 and other cpu's perform the following operations when they subtract
(SUB) two binary numbers:Step 1: Finds l's complement of the subtrahend.
Step 2: Finds 2's complement of the subtrahend by adding 1 to the
result of Step 1.Step 3: Adds the 2's complement of the subtrahend to the minuend.
Step 4: Complements the CY flag.
These steps are internal to the microprocessor and invisible to the user:
only the result is available to the user. (return)
MODULO-2 ARITHMETIC (return)
In process control situations it is common that the data inputed to a computer
from an input port represents the status of various binary limit sensors. These
sensor were arranged as specific bit places at that input port. Although the
magnitude of that status byte has meaning, the specific bits that are set are
also of interest to the process engineer. Therefore, it is common to perform
simple arithmetic operations on these 8 bit patterns where the carry characteristics
of the answer are not the focus of attention. An example of a computer operation
that involves Modulo-2 arithmetic is binary addition (or subtraction) without carries.
In these cases there are no negative numbers, and the result in the sign bit can be
either 0 or 1.An addition or subtraction of two numbers in Modulo-2 is similar to an exclusive-OR
of two logic functions as shown, and the results are the same.
Thus, the value of such an mathematical operation when interrogating a
1 0 1 0 1 0 1 0 ( current process status byte)
+ 1 1 1 0 1 1 1 0
= 0 1 0 0 0 1 0 0
status byte from a bank of limit sensors should be clear. For example, the simple
exclusive OR exercise just shown might really be the analysis of 8 limit alarm
sensors.The process engineer already knows that the sensors attached to status
bits D1, D2, D3, D5, D6, D7 have some interdependency. An alarm signal from
some of these sensors triggers an alarm from some of the rest. However, for this
example, the process engineer has already determined that the real process problem
is because the process disturbance has forced the sensors at D3 and D6 to go into
alarm. In fact, when the sensors at D3 and D6 are in alarm, the process was
disturbed enough that the other sensors attached to D1, D5 and D7 also moved
to their process alarm state. Thus the computer based controller must recognize
this process reality and send computer instructions to the final control elements that
fix the process disturbance as defined by the actions of the alarm sensors attached
to bits D3 and D6 of the process status byte shown above. Clearly, a Modulo 2
addition, as shown above, will determine the status of bits D3 and D6 of the current
process status byte.Naturally, this example in a real process situation is more complex. This simple
example did not include any discussion of the active or passive state of the binary
alarm sensors attached to the bit places of the process status input port. Nor was
there any concern about the overall significance of any redundancy in the alarm bit
pattern. It is also not necessary that the current process status byte be considered
as a set of binary bits. The process status input can be represented as a polynomial.
Thus, a division of two polynomials can be performed and its quotient and remainder
can then be used to identify the process disturbance and determine its overall
significance.
CYCLIC REDUNDANCY CHECK (CRC) (return)
One place that polynomial divisions are used is in serial transmission error
checking. One such check is called a Cyclic Redundancy Check (CRC). The
CRC check is a somewhat complex procedure. However, the technique involves
Modulo-2 arithmetic and is commonly used when data are transferred from
and to a floppy and in serial data communications.A cyclic redundancy check is another way to check if there has been an error
in a serial transmission from one serial card to another. The technique uses a
polynomial to represent the data bit stream. The sender does division on this
data stream representative polynomial with a polynomial called a generator
polynomial. The sender then sends the data stream byte and the remainder from
this polynomial division. The receiver gathers both the data byte and the remainder
and then checks the remainder to detect an error in transmission. If the receiver's
answer to the polynomial division provides the same remainder as the remainder
value sent by the transmitter, the receiver accepts the current data byte as valid. (return)
Example Questions (Brief Text Discussions)
Example Problem 1: (return)
Convert the binary number (10011010 ) into its Hexadecimal and Octal equivalents. (answer 1)Example Problem 2: (return)
Subtract the following two decimal numbers, 52 - 23, using the following
two methods; (answer 2)
a) Borrow Method b) 10's complement methodExample Problem 3: (return)
Perform the subtraction of two numbers using the borrow method and the 10's
complement method: 23 - 52 (answer 3)Example Problem 4: (return)
Find the 2's complement of the
binary number 0001 1100 ( 1C (hex) or 28 ( base 10). (answer 4)Example Problem 5: (return)
Subtract 32 Hex (001l 0010) from 45 Hex (0100 0101). (answer 5)Example Problem 6: (return)
Subtract 45H (0100 0100) from 32H (0011 0010). (answer 6)Example Problem 7: (return)
Show the internal steps performed by the microprocessor to subtract (answer 7)
the following unsigned numbers:
a. FA Hex - 62 Hex b. 62 Hex -- FA HexExample Problem 8: (return)
From Example 7a, assume that the numbers are signed numbers, and (answer 8)
interpret the result.Example Problem 9: (return)
Add the following two positive numbers and interpret (answer 9)
the sign flag: + 4l Hex. + 54 Hex.Example Problem 10: (return)
Outline the way a Z80 performs a CRC. (answer 10)
Answer Example Problem 1:
Convert the binary number (10011010 ) into its Hexadecimal and Octal equivalents.a) Hex equivalent
DECIMAL SUBTRACTION
Step 1: Starting from the right (LSB) arrange the binary digits in groups of four. 1001 1010.
Step 2: Convert each group into its equivalent Hex number; 9A.b) Octal equivalent
Step 1: Starting from the right (LSB) arrange the binary digits in groups of three. 010 011 010
Step 2: Convert each group into its equivalent octal digit; 2 3 2. (question memu) (return)
Answer Example Problem 2: (return)
Subtract the following two decimal numbers, 52 - 23, using the following two methods;
a) Borrow Method b) 10's Complement Methoda) Borrow Method
Minuend: 52 = 5 x 10 + 2
Subtrahend: 23 = 2 x 10 + 3Step 1: To subtract 3 from 2, 10 must be borrowed from the second place
of the minuend.
52 = 4 x 10 + 12Step 2: The subtraction of the digits in the first place and the second place
is as follows.
52 = 4 x 10 +12
- 23 = 2 x 10 + 3 (which gives an answer of 29)
29 = 2 x 10 + 9b) The 10's Complement Method (return)
Step 1: Find 9's complement of the subtrahend (23),( meaning subtract each
digit of the subtrahend from 9).
9's complement of 52: 9 9
-2 3
= 7 6Step 2: Add 1 to the 9's complement to find the 10's complement of the subtrahend .
10's complement of 23: 76 + 1
= 77
The reason for find the 9's complement exercise is to just demonstrate a similar
procedure to find the 2's complement of a binary number. However, in reality
the 10's complement of 23 is equivalent to subtracting 23 from 100.Step 3: To subtract 23 form 52, add 10's complement of the subtrahend (77)
to the minuend (52):10's complement of 23: 77
Minuend: + 52
129 = 29 (By dropping the most significant digit)(The elimination of the most significant bit is equivalent to subtracting
100 from the sum. This is necessary to compensate for the 100 that
was added to find the 10's complement of 23. ) (question memu)Answer Example Problem 3 (return)
Perform the subtraction of two numbers, 23 - 52, using the
a)Borrow Method b) The 10's complement methodIn both cases the Minuend = 23 and the Subtrahend = 52
a) Borrow Method
Step 1: Perform the subtraction of the digits in the first place:
3 - 2 = 1.Step 2: To subtract the digits in the second place a borrow is required
from the third place. Assuming the borrow is available from the
third place. Thus, the digit 5 can be subtracted from 2 as follows:1 2
- 5
7 ( this operation assumed that there
was a nonexistent digit (1) to borrow)Final Result: 23
- 52
171The same result is obtained with the 10's complement method.
b) 10's Complement Method
Step 1: Find the 9's complement of the subtrahend (52).
9's complement of 52: 99 - 52 = 47Step 2: Add 1 to the 9's complement to find 10's complement: 47 + 1 = 48
Step 3: Add the 10's complement of the subtrahend to the minuend.
10's complement of 52: 48
Minuend: 23
= 71 (this is negative 29, expressed
in 10's complement)By examining these two examples, the following conclusions can be drawn,
and these conclusions can be used for any number system.1. The complement of a number is its equivalent negative number.
2. A number can be subtracted by using its complement.
3. The sum of a number and its complement results in 0 if the most
significant digit of the sum is ignored.
4. When the subtrahend is larger than the minuend, the result of the
10's complement method is negative, and it is expressed in terms
of 10's complement. The same result can be obtained by borrowing
a digit from the most significant (question memu) (return)Answer Example Problem 4: (return)
Find the 2's complement of the binary number 0001 1100
( 1C (hex) or 28 ( base 10).
Step 1: Find l's complement, meaning replace 0 with 1 and 1 with 0.
1's complement = 1110 0011
Step 2 : Add 1 2's complement = 1110 0100By examining the result of the example, the following rule can be stated:
To find the 2's complement of a binary number:
( instead of the above procedure of the 1's complement).Rule l: Start at the LSB of a given number, and check all the bits to the left.
Keep all the bits as they are up to and including the least significant 1.Rule 2: After the first 1, replace all 0's with 1's and 1's with 0's.
As an example these rules can be applied to the number
1C (Hex) as illustrated:Binary Number: 0 0 0 1 1 1 0 0
Replace 0 with 1 and 1 with 0 on the left side of of blue digits.
The results is the 2's complement for the number in question.
The 2's complement for 0 0 0 1 1 1 0 0 is the number:1 1 1 0 0 1 0 0
The 2's complement of the number can be verified by
adding the complement to the original number as follows;Binary Number: 0 0 0 1 1 1 0 0
2's Complement: 1 1 1 0 0 1 0 0(Answer) 1 0 0 0 0 0 0 0 0
If the 2's complement was done correctly the answer
should be , ignoring the carry bit: 0 0 0 0 0 0 0 0Note: As a general rule, in any number base, a number and its
complement should add to zero. (question memu) (return)
Answer Example Problem 5: (return)
Subtract 32 Hex (001l 0010) from 45 Hex (0100 0101).
Subtrahend: 32 Hex = 0 0 1 1 0 0 1 0
2's Complement of 32 Hex = 1 1 0 0 1 1 1 0
Minuend: 45 Hex = 0 1 0 0 0 1 0 1
Carry CY = 1 1 1
1 0 0 0 1 0 0 1 1 = 13 (Hex)
(The last step is to complement the carry (9th bit).
This will indicate that the result is + 13 Hex) (question emu)
Answer Example Problem 6: (return)
Subtract 45 Hex (0100 0100) from 32 Hex (0011 0010).
Subtrahend: 45 Hex = 0 1 0 0 0 1 0 1
2's Complement: = 1 0 1 1 1 0 1 1
Minuend: = 0 0 1 1 0 0 1 0
Carry: = 0 1 1 1
Addition answer = 1 1 1 0 1 1 0 1 = ED Hex
The result is negative and it is expressed in 2's complement.
To verify this work, take the 2's complement of the result. The 2's
complement of the result should be 13 Hex as in Example 4. In this
subtraction, the 9th bit is zero. Thus a micro processor sets the 9th bit
indicating a borrow which, in turn, indicates to us that the
calculation has produced a negative result.
ED Hex = 1 1 1 0 1 1 0 1
2's complement = 0 0 0 0 1 0 0 1 1 (question memu)
Answer Example Problem 7: (return)
Show the internal steps performed by the microprocessor to subtract
the following unsigned numbers:
a. FA Hex - 62 Hex b. 62 Hex -- FA Hex
a. FA Hex - 62 Hex
Minuend: FA = 1 1 1 1 1 0 1 0
Subtrahend: 62 = 0 1 1 0 0 0 1 0Step 1: invert number
1's complement of 62 Hex = 1 0 0 1 1 1 0 1
Step 2: Add 1 + 1
2's complement of 62 Hex = 1 0 0 1 1 1 1 0
Step 3: Add minuend
FA Hex = 1 1 1 1 1 0 1 0
= 1 1 0 0 1 1 1 1 0Step 4: Complement Carry CY = 0 1 0 0 1 1 0 0 0
Result: = 0 1 0 0 1 1 0 0 0 98 (Hex)
Flags: CY = 0, Sign = 1, Zero = 0 (question memu)
b. 62 Hex -- FA Hex (return)
Minuend: 62 = 0 1 1 0 0 0 1 0
Subtrahend: FA = 1 1 1 1 1 0 1 0Step 1: invert number
1's complement of FA Hex = 0 0 0 0 0 1 0 1
Step 2: Add 1 + 1
2's complement of FA Hex = 0 0 0 0 0 1 1 0
Step 3: Add minuend
62 Hex = 0 1 1 0 0 0 1 00 0 1 1 0 1 0 0 0
Step 4: Complement Carry CY = 1 0 1 1 0 1 0 0 0
Result: 1 0 1 1 0 1 0 0 0 68 (Hex) (carry =1)
Flags: CY = 1, Sign = 0, Zero = 0
This result is negative and expressed in 2's complement of the magnitude.
a. FA Hex -- 62 Hex = 98 Hex (positive), CY = 0, S = 1
b. 62 Hex -- FA Hex = 68 Hex (negative), CY = 1, S = 0These results and associated flags appear to be confusing. In Example 7a,
the result is positive but the sign flag indicates that it is negative.
On the other hand, in Example 6b, the result is negative but the sign
flag indicates that it is positive. This confusion might be explained by
considering the following points:1. This subtraction is concerned with the unsigned numbers; therefore,
the sign flag is irrelevant. In signed arithmetic, the number FA is
invalid because it is an 8-bit number.2. You can check whether the result indicates the true magnitude by
checking the CY flag. If CY is reset, the result is positive, and if
CY is set, the result is expressed in 2's complement. (question menu ) (return)
Answer Example Problem 8 (return)
From Example 7a, assume that the numbers are signed numbers,
and interpret the result.
Minuend: FANow this is a negative number because D7 = 1; therefore, the Z80
represents it in 2's complement. The magnitude of the number
can be found by taking the 2's complement of FAH:FA = 1 1 1 1 1 0 1 0
2's complement = 0 0 0 0 0 1 1 0
Magnitude = 06 (Hex)
Subtrahend: 62 Hex
(this is a positive number because D7 = 0.)The problem given in 6a can be represented as follows:
FA -- 62 = (-O6 Hex) ( + 62 Hex) = - 68 Hex
The final result, -68 Hex, is in the form of its 2's complement:
-68 Hex = -( 0 1 1 0 1 0 0 0)
2's complement of 68 Hex = 1 0 0 1 1 0 0 0 = 98 Hex
The final answer is the same as before; however, it will be
interpreted as a negative number with the magnitude of 68 Hex. (question memu) (return)When signed numbers are used in arithmetic operations,
the sign flag will indicate the proper sign of the result.Answer Example Problem 9
Add the following two positive numbers and interpret
the sign flag: + 4l Hex. + 54 Hex.
41 Hex = 0 1 0 0 0 0 0 1
+ 54 Hex = 0 1 0 1 0 1 0 0
(Answer) = 95 Hex = 1 0 0 1 0 1 0 1
Sign = 1, Carry = 1, Z = 1This is an addition of two positive numbers. The sign flag indicates
that the sum is negative. However, the overflow (P/V = 1) flag indicates
that there is an overflow; therefore, the sum is inaccurate if these are
signed numbers. If this had been the sum of two unsigned numbers,
the sign flag would have no significance. (question memu) (return)Answer Example Problem 10
Outline the way a Z80 performs a CRC.
If, for example the data stream to be sent is 1000 1010, the
modified polynomial for data bits 1000 1010
becomes : x6 + x4 + 1 .
An arbitrary ( from this discussion's perspective) generator
polynomial might be: x4 + 1. The CRC serial transmission error
checking procedure would be:
Step 1) Raise x by the power of the CRC code length
( 4 in most cases) and multiply the modified
polynomial by x4.
Step 2) Divide this polynomial product by the generator
polynomial:( x10 + x8 + x4 ) / ( x4 + 1 ) = ( X6 + X4 + X2 ) + X2
Step 3) Transmit the message code and the remainder to the
receiver for verification. (question memu) (return)