Assembly Language and Computer Architecture Using C++ and Java 5/27/05.
-
date post
21-Dec-2015 -
Category
Documents
-
view
254 -
download
1
Transcript of Assembly Language and Computer Architecture Using C++ and Java 5/27/05.
![Page 1: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/1.jpg)
Assembly Language and Computer Architecture Using C+
+ and Java
5/27/05
![Page 2: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/2.jpg)
Chapter 1
Number Systems
![Page 3: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/3.jpg)
Would you trust a medical doctor who did not know the location of the human appendix?
Not likely.
A doctor must know the structure and operation of the human body.
![Page 4: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/4.jpg)
Similarly, a computer expert must know the structure and operation of computers.
Providing a clear, concrete, and thorough view of the structure and operation of computers is our objective.
![Page 5: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/5.jpg)
Work at this level
Understand at this level
![Page 6: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/6.jpg)
Positional number systems
• Decimal: base 10
• Binary: base 2
• Hexadecimal: base 16
![Page 7: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/7.jpg)
Decimal
• Ten symbols: 0, 1, 2, …, 9
• Weights change by a factor of 10 from one position to the next.
• Shifting the decimal point changes the value by a factor of 10 for each position shifted.
• Each symbol is called a “digit”
![Page 8: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/8.jpg)
25.4 decimal
![Page 9: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/9.jpg)
25.4 decimal =
![Page 10: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/10.jpg)
Binary
• Two symbols: 0 and 1
• Weights change by a factor of 2 from one position to the next.
• Shifting the binary point changes the value by a factor of 2 for each position shifted.
• Each symbol is called a “bit”.
![Page 11: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/11.jpg)
1011.1 binary
![Page 12: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/12.jpg)
0111010110010111
MSB lsb
Most and least significant bits
![Page 13: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/13.jpg)
Hexadecimal
• Sixteen symbols: 0, 1, 2, …, 9, A, B, C, D, E, F• Weights change by a factor of 16 from one
position to the next.• Shifting the hexadecimal point changes the
value by a factor of 16 for each position shifted.• Each symbol is called a “hex digit”• A = 10 decimal B = 11 decimal• C = 12 decimal D = 13 decimal• E = 14 decimal F= 15 decimal
![Page 14: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/14.jpg)
1CB.8 hex
![Page 15: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/15.jpg)
Memorize this table8 4 2 1
![Page 16: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/16.jpg)
Complement of a symbol s in a base n system
(n – 1) - s
Binary: Complement of 1 is 1 – 1 = 0
Decimal: Complement of 3 = 9 – 3 = 6
Hex: Complement of 4 = 15 – 4 = 11 = B
![Page 17: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/17.jpg)
Byte
• Sequence of 8 bits
• Each bit has two possibilities: 0 or 1
• Thus, there are 2x2x2x2x2x2x2x2 = 28 distinct patterns
Exercise: Bytes are also called Octals. Why do we have two names?
![Page 18: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/18.jpg)
Example: 128 distinct numbers are representable with 7 bits but the number 128 requires 8 (7+1) bits to be represented
![Page 19: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/19.jpg)
Arithmetic with positional numbers
Rules are essentially the same for all bases
![Page 20: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/20.jpg)
Arithmetic in decimal
![Page 21: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/21.jpg)
Adding in binary
![Page 22: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/22.jpg)
Subtraction in binary
![Page 23: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/23.jpg)
Addition in hex
![Page 24: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/24.jpg)
Subtraction in hex
![Page 25: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/25.jpg)
Converting to base n
• Integer part: Repeatedly divide by n. Remainders are the digits of the base n number (in reverse order).
• Fractional part: Repeatedly multiply by n. Whole parts are the digits of the base n number ( times n = move decimal point to the right one place).
![Page 26: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/26.jpg)
Each remainder is a digit
![Page 27: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/27.jpg)
Convert 11 decimal to binary
11 decimal = 1011 binary
![Page 28: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/28.jpg)
Convert 30 decimal to hex
30 decimal = 1E hex
![Page 29: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/29.jpg)
Each whole part is a digit (strip whole part of product after each multiplication by 10)
.43
x 10
1st digit 4. 3
x 10
2nd digit 3. 0
Fractional Conversion:
![Page 30: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/30.jpg)
Convert .6875 decimal to binary
.6875 decimal = .1011 binary
![Page 31: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/31.jpg)
Repeating fraction
.1 decimal = .0 0011 0011 ... binary
![Page 32: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/32.jpg)
Converting between binary and hex
• Very easy to do
• The ease of conversion is the reason why hex is often used as a shorthand representation of binary.
• To do conversion, you need to know the binary-hex equivalents for the numbers 0 to 15.
![Page 33: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/33.jpg)
Binary to hex
![Page 34: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/34.jpg)
Hex to binary
![Page 35: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/35.jpg)
Horner’s method
Efficient technique for converting to a new number base
(just multiple by that base)
![Page 36: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/36.jpg)
Inefficient evaluation of 2CD5
![Page 37: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/37.jpg)
Use Horner’s rule to convert 2CD5
![Page 38: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/38.jpg)
Horner’s rule written horizontally
![Page 39: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/39.jpg)
Horner’s Rule Algorithm
N = 0;
Get keystroke;
While (keystroke is digit) { N = N * 10 + keystroke;
Get keystroke;
}
![Page 40: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/40.jpg)
Convert 38 to binary using Horner’s rule
Shift right == *2corresponds to positionof multiplier
![Page 41: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/41.jpg)
Signed binary numbers
• Sign-Magnitude
• Two’s Complement
• One’s Complement
• Excess-n
![Page 42: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/42.jpg)
Sign-magnitude
Exercise: Add these two numbers???
![Page 43: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/43.jpg)
Adding sign-magnitude requires sign analysis:
Like signs, then add magnitudes Unlike signs, then subtract magnitudes
Sign-magnitude representation not good for
computers
![Page 44: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/44.jpg)
Two’s complement
The two’s complement of the number M is the number which yields 0 when added to M.
Just the definition of negative
![Page 45: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/45.jpg)
Note this behavior
So 1111…1 is just 1 less than 0. Remember that.
what number is this?
![Page 46: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/46.jpg)
Simply “flipping” the bits does not yield the two’s complement.
But you are not far from 0; just add 1
![Page 47: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/47.jpg)
Getting the two’s complement
• Simply flipping the bits does not yield the two’s complement—it yields all 1’s.
• But adding 1 to all 1’s yields all 0’s.
• So flipping the bits and adding 1 should yield the two’s complement.
![Page 48: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/48.jpg)
Flip bits and add 1
![Page 49: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/49.jpg)
Two’s complement of 1
![Page 50: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/50.jpg)
3-bit two’s complement numbers
![Page 51: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/51.jpg)
Terminology
![Page 52: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/52.jpg)
Positive result is in true form.Negative result in complement form.
![Page 53: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/53.jpg)
One’s complement
• Like two’s complement, but don’t add 1
• Requires addition of end-around carry
• 0000000000000001 = +1
• 1111111111111110 = -1
![Page 54: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/54.jpg)
Adding one’s complement numbers
Processing end-around carry is hard for computers
![Page 55: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/55.jpg)
One’s complement has two representations of 0:
0000000000000000and
1111111111111111
This feature is not good for computers.
![Page 56: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/56.jpg)
Excess-n representation
• n is added to a number to get its excess-n representation.
• n is subtracted from an excess-n number to get its value.
![Page 57: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/57.jpg)
![Page 58: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/58.jpg)
When adding two excess-n numbers, must subtract n
![Page 59: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/59.jpg)
Subtracting by two’s complement addition: subtracting +3 from 5
![Page 60: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/60.jpg)
Use two’s complement to subtract unsigned numbers
![Page 61: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/61.jpg)
Range of unsigned numbers
• n bits means 2n patterns
• Thus, range is 0 to 2n – 1
• n = 8, range is 0 to 28 – 1 ( 0 to 255)
![Page 62: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/62.jpg)
Range of two’s complement numbers
• Positive numbers have msb = 0. Thus, there are n – 1 bits that can be set.
• Positive range is 0 to 2n-1n-1 - 1 - 1
• Negative numbers have msb = 1. Thus, there are n – 1 bits that can be set
• Negative range is -2n-1n-1 to -1.
• Total range: -2n-1n-1 to 2n-1n-1 – 1. – 1.
![Page 63: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/63.jpg)
2’s Complement Problem:
• Since there are the same number of +ve and –ve numbers, why is the largest –ve number (-2n-1) while the largest +ve number appears to be 1 less: (2n-1 -1)
0-1 2n-1 - 12n-1
same distance
![Page 64: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/64.jpg)
memorizethis
![Page 65: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/65.jpg)
Danger in using char to represent signed number
• When extending a variable of type char to a longer format, extension may be signed number extension OR unsigned number extension.
• Type of extension depends on the compiler.
![Page 66: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/66.jpg)
00000000 11111111
11111111 11111111
11111111
![Page 67: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/67.jpg)
Signed overflow
• Overflow never occurs when adding numbers of unlike signs.
0-1 2n-1 - 12n-1 a b
a + boverflowregion
overflowregion
![Page 68: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/68.jpg)
Signed overflow
• Overflow is indicated if the carry into the msb column differs from the carry out of the msb column.
0-1 2n-1 - 12n-1 ab
a + b
overflowregion
overflowregion
ab
also possible
![Page 69: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/69.jpg)
MSBs
(1)
(0)
![Page 70: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/70.jpg)
![Page 71: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/71.jpg)
Flags
• V is the signed overflow flag
• S is the sign flag
• C is the carry flag
![Page 72: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/72.jpg)
Unsigned overflow on an addition indicated by a carry out
![Page 73: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/73.jpg)
Another type of unsigned overflow.Indicated by a borrow into msb on a
subtraction.
1
![Page 74: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/74.jpg)
Unsigned overflow during a subtraction
• Indicated by a borrow into the msb.
• But computers do not subtract using the borrow technique.
• Computers subtract using the two’s complement technique.
• Carry out occurs if and only if a borrow in would not have occurred had the borrow technique been used.
![Page 75: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/75.jpg)
Unsigned overflow test
• “borrow in” with the borrow technique iff no “carry out” with two’s complement technique.
• Addition: – set the carry flag to the carry out.
• Subtraction:– set the carry flag to the complement of the carry
out.
• So carry flag functions as carry/borrow flag.
• Addition or subtraction: a 1 in the carry flag indicates unsigned overflow.
![Page 76: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/76.jpg)
Examples
borrow 2s-comp
(1) 0001 0001
- 0010 +1110
1111 (0) 1111
(0) 0010 0010
- 0001 +1111
0001 (1) 1111
![Page 77: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/77.jpg)
Flip carry out on a subtraction
0: on addition1: on subtraction
![Page 78: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/78.jpg)
Analyzing two’s complement numbers
2n is an (n+1)-bit number. For example,
24 = 10000 (5 bits)
2n – 1 is an n-bit number containing all 1’s.
For example, 24 – 1 = 1111, but to make it positive we put it in a 5-bit number, 01111.
![Page 79: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/79.jpg)
Analyzing two’s complement numbers
To flip an n-bit number, first subtract from all 1’s:
1111
- 0101
1010 (0101 flipped)
All 1’s is 2n -1. Thus, the n-bit two’s complement of x is 2n -1 – x + 1 = 2n - x
(N – 1) - number
![Page 80: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/80.jpg)
Analyzing two’s complement numbers
2n – x (2’s comp of x) x 2n + 0
Carry out of leftmost column
![Page 81: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/81.jpg)
Analyzing two’s complement numbers
In mathematics, adding two positives yields a positive; adding two negatives yields a negative; adding a positive and a negative yields either a positive or negative depending on which of the two numbers added has the larger magnitude.Two’s complement numbers behave in the same way.
![Page 82: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/82.jpg)
Analyzing two’s complement numbers
Adding two positive two’s complement numbers:
a
b
a + b
The n-bit result equals the true value of a + b
as long as overflow does not occur. If it occurs it turns the msb on making the number negative
![Page 83: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/83.jpg)
Analyzing two’s complement numbers
Adding two negative two’s complement numbers:
2n – c (two’s complement of c)
2n – d (two’s complement of d)
2n + 2n - (c + d) (two’s complement of c+d)
Lost
Notice that the sum of two negatives is negative.
![Page 84: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/84.jpg)
Analyzing two’s complement numbers
Adding a positive and a negative two’s complement number, a < d:
a
2n – d (two’s complement of d)
2n - (d - a) (two’s complement of d - a)
Notice the result is negative.
![Page 85: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/85.jpg)
Analyzing two’s complement numbers
Adding a positive and a negative two’s complement number, a >= c
a
2n – c (two’s complement of c)
2n + a - c
2n + a - c > 2n so needs n+1 bits
Lost
Notice the result is positive.
![Page 86: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/86.jpg)
Full Adder
1 carry in 1 +0------ 0 1 carry out
![Page 87: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/87.jpg)
![Page 88: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/88.jpg)
Gates
Simple circuits usually with two input lines and one output line, each carrying one bit. The output at any time depends on the inputs at that time.
![Page 89: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/89.jpg)
negates the original meaning
![Page 90: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/90.jpg)
XOR gate
Complementing gate
![Page 91: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/91.jpg)
Complementing action of the XOR gate
![Page 92: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/92.jpg)
![Page 93: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/93.jpg)
NOR gate
Zero detecting gate
Outputs 1 if all inputs are 0
![Page 94: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/94.jpg)
![Page 95: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/95.jpg)
Subtracting a number from itself
• Using the borrow technique, a borrow would never occur.
• Thus, using the two’s complement technique, a carry out should ALWAYS occur.
![Page 96: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/96.jpg)
A carry out should always occur when subtracting a number from
itself.
![Page 97: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/97.jpg)
Subtracting 0 from 0 is not an exception
![Page 98: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/98.jpg)
Sign of true result
• Needed to determine the result of a signed comparison.
• The S flag has the sign of the true result of a two’s complement addition only if overflow does NOT occur.
• To get true sign, use S XOR V.
![Page 99: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/99.jpg)
Getting the sign of the true result
![Page 100: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/100.jpg)
Scientific notation
Significand x power of 10
where significand is the number in which the decimal point is to the immediate right of the leftmost non-zero digit.
![Page 101: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/101.jpg)
154 in scientific notation is1.54 x 102
![Page 102: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/102.jpg)
Binary scientific notation
1110.11 = 1.11011 x 23
![Page 103: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/103.jpg)
Floating point format for+1.11011 x 23
• Its sign is +.
• 1.11011 is called the significand
• .11011 is called the fractional part
• 3 is the exponent
• In floating-point format, store the sign (0 for +, 1 for -), the exponent in excess-127, and the fractional part.
![Page 104: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/104.jpg)
Floating-point format for 1.11011 x 23
Note that the bit to the left of the binary point does not appear in the floating point format. It is called the hidden bit.
![Page 105: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/105.jpg)
Normalization
Shifting a number so that the binary point is in the correct place for floating-point format (to the immediate right of the leftmost 1).
![Page 106: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/106.jpg)
Special values
![Page 107: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/107.jpg)
Denormal numberExponent = -126 and hidden bit = 0
![Page 108: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/108.jpg)
Computational error
• Floating-point computations are not exact.
• Errors can grow to the point where the final results of a computation may be meaningless.
![Page 109: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/109.jpg)
The sum computed by the first program on the next slide is not 1.
![Page 110: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/110.jpg)
![Page 111: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/111.jpg)
The first program on the next slide is an infinite loop.
![Page 112: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/112.jpg)
![Page 113: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/113.jpg)
Round-off error
![Page 114: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/114.jpg)
![Page 115: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/115.jpg)
The program on the next slide displays “equal”.
![Page 116: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/116.jpg)
![Page 117: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/117.jpg)
Associative operator
Order of performing operations does not matter.
(a + b) + c = a + ( b + c)
![Page 118: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/118.jpg)
The programs on the next slide output different sums.
![Page 119: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/119.jpg)
![Page 120: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/120.jpg)
Suppose x and y are true values, and X and Y, respectively, are their computed values that include small errors.
![Page 121: Assembly Language and Computer Architecture Using C++ and Java 5/27/05.](https://reader036.fdocuments.in/reader036/viewer/2022081506/56649d545503460f94a31a82/html5/thumbnails/121.jpg)
X = x + 0.0001 Y = y -0.0001
Now compute X – Y:
X - Y = x + 0.0001 – (y – 0.0001) = (x – y) + 0.0002
The absolute error (0.0002) will be large relative to the true result (x - y) if x and y are nearly equal.