Characters, Words, and Bytes



Download 55.9 Kb.
Date20.10.2016
Size55.9 Kb.
#6683
CIS 258 Assembly Language Programming - Lecture Notes
Chapter 1 Data Representation and Computer Arithmetic

Characters, Words, and Bytes


  • Bit - The smallest quantity of information that can be stored and manipulated inside a computer, A bit can be in one of two states, on or off, 1 or 0, true or false.

  • Byte - A group of 8 bits.

  • Word - the basic unit of information stored in memory and processed by a computer. A word is a group of bits. The number of bits, or word size, varies from computer to computer.

    • An n-bit word can contain 2n unique bit patterns. A given bit pattern has no intrinsic meaning associated with it. The actual meaning of a particular pattern of bits is the meaning given to it by the programmer.

  • Instruction (op-code) - An instruction or operation code defines an action to be performed by the CPU. A bit pattern is associated with each op-code by the CPU designer.

  • Numeric Quantity - A word that is used to represent a number, Numbers may be represented in many formats.

Character - A bit pattern used to represent a letter (A-Z or a-z) or any symbol from the keyboard. The most common scheme for coding characters is the ASCII code (American Standard Code for Information Interchange). ASCII Chart
MSb --> | 0 1 2 3 4 5 6 7

| 000 001 010 011 100 101 110 111

_________ |_________________________________________

0 0000 | NUL DLC SP 0 @ P ' p

1 0001 | SOH DC1 ! 1 A Q a q

2 0010 | STX DC2 " 2 B R b r

3 0011 | ETX DC3 # 3 C S c s

4 0100 | EOT DC4 $ 4 D T d t

5 0101 | ENQ NAK % 5 E U e u

6 0110 | ACK SYN & 6 F V f v

7 0111 | BEL ETB ' 7 G W g w

8 1000 | BS CAN ( 8 H X h x

9 1001 | HT EM ) 9 I Y i y

A 1010 | LF SUB * : J Z j z

B 1011 | VT ESC + ; K [ k {

C 1100 | FF FS , < L \ l |

D 1101 | CR GS - = M ] m }

E 1110 | SO RS . > N ^ n ~

F 1111 | SI US / ? O _ o DEL



  • Picture Element - Part of a picture that has been digitally encoded, Many techniques exist for encoding pictures. The term pixel is derived from Picture Element.

  • Analog Signal - A signal that varies over time and is not constrained to different levels, Music is an example of an analog signal. Before analog signals can be stored using binary digits they must first be converted into a digital form. An analog to digital converter (ADC) is a device that performs this conversion. See Fig 1-3.


Number Bases

Name of base Base Set of digits


Decimal b = 10 a = {0,1,2,3,4,5,6,7,8,9}

Binary b = 2 a = {0,1}

Octal b = 8 a = {0,1,2,3,4,5,6,7}

Hexadecimal b = 16 a = {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}




Number Base Conversion

Decimal Binary Hex

0 0000 0


1 0001 1

2 0010 2


3 0011 3

4 0100 4


5 0101 5

6 0110 6


7 0111 7

8 1000 8


9 1001 9

10 1010 A

11 1011 B

12 1100 C

13 1101 D

14 1110 E

15 1111 F
Decimal to Binary To convert a decimal integer to binary, divide the number successively by 2, and after each division, record the remainder which is either 1 or 0. The process is terminated only when the result of the division is 0 remainder 1.

For example, 12310 becomes:

123 / 2 = 61 R = 1

61 / 2 = 30 R = 1

30 / 2 = 15 R = 0

15 / 2 = 7 R = 1

7 / 2 = 3 R = 1

3 / 2 = 1 R = 1

1 / 2 = 0 R = 1

The result is read from the most-significant bit (the last remainder) upward to give

12310 = 11110112.
Decimal to Hexadecimal To convert a decimal integer to hexadecimal, divide the number successively by 16, and after each division, record the remainder which in the decimal range 0 to 15, corresponding to the hexadecimal range 0 to F.

For example, 5324110 becomes:

53241 / 16 = 3327 R = 9

3327 / 16 = 207 R = 1510 = F16

207 / 16 = 12 R = 1510 = F16

12 / 16 = 0 R = 1210 = C16

Therefore, 5324110 = CFF916.
Binary to Decimal For manual conversion of small binary numbers simply add together all the 1 bits weighted by the proper power of two.

For example, 10101112 is represented by:


64 32 16 8 4 2 1

1 0 1 0 1 1 1


64 + 0 + 16 + 0 + 4 + 2 + 1 = 87
A better method to use when writing a program to perform the conversion is based on a recursive algorithm as follows: Take the left-most non-zero bit, double it, and add it to the bit on its right. Now take this result, double it, and add it to the next bit on the right. Continue in this way until the least-significant bit has been added in.

For example, 10101112 becomes:


1 0 1 0 1 1 1

 2+0


 4+1=5

 10+0=10

 20+1=21

 42+1=43

 86+1=87

Therefore, 10101112 = 8710.


Hexadecimal to Decimal This method is identical to the procedure for binary, except that 16 is used as a multiplier.

For example, 1AC16 becomes:


1 A C

*16 16+10=26

*16 416+12=428

Therefore, 1AC16 = 42810.


Binary to Hexadecimal The binary number is formed into groups of four bits starting at the decimal point. Each group is replaced by a hexadecimal digit from 0 to 9, A, B, C, D, E, F.

For example, 0110010111012 becomes 0110 0101 1101

6 5 D

Therefore, 0110010111012 = 65D16.


Hexadecimal to Binary Each hexadecimal digit is replaced by its four-bit binary equivalent.
For example, AB4C16 becomes A B 4 C

1010 1011 0100 1100

Therefore, AB4C16 = 10101011010011002.

Converting Binary Fractions to Decimal Fractions The algorithm for converting binary fractions to their decimal equivalent is based on the fact that a bit in one column is worth half the value of a bit in the column on its left. Starting at the right-mist non-zero bit, take that bit and halve it. Now add the result to the next bit on its left. Halve this result and add it to the next bit on the left. Continue until the binary point is reached.

For example, consider the conversion of 0.011012 into decimal form


0. 0 1 1 0 1

1/2 


+0

1/4  1/2

+ 4/4

5/8  5/4



+ 8/8

13/16  13/8

+ 0

13/32  13/16


Therefore, 0.011012 = 13/32 = 0.4062510.
Converting Decimal Fractions to Binary Fractions The decimal fraction is multiplied by two and the integer part noted. The integer, which will be either 1 or 0, is then stripped from the number to leave a fractional part. The new fraction is multiplied by two and the integer part noted. Continue until the process ends, or a sufficient degree of precision has been achieved. The binary fraction is formed by reading the integer parts from the top to the bottom as the following example illustrates.
For example, 0.687510 becomes:

0.6875 x 2  1.3750

0.3750 x 2  0.7500

0.7500 x 2  1.5000

0.5000 x 2  1.0000

0.0000 x 2  0.0000 ends the process


Therefore, 0.687510 = 0.10112
Now the same process for 0.110

0.1000 x 2  0.2000

0.2000 x 2  0.4000

0.4000 x 2  0.8000

0.8000 x 2  1.6000

0.6000 x 2  1.2000

0.2000 x 2  0.4000  at this point the values start repeating

0.4000 x 2  0.8000

0.8000 x 2  1.6000

0.6000 x 2  1.2000

0.2000 x 2  0.4000
Therefore, 0.100010 = 0.00011001102 to ten binary places. Because the values start repeating there is no exact representation of 0.110 in binary fractions.

Counting in Hex


Before moving on lets take another look at hexadecimal numbers and what they represent. The hex digits are: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. Just as in the decimal system when we run out of digits to represent a value we start using more than one digit to represent the value. For example: if we are counting in decimal and we reach 9 the next quantity is represented by using two digits. In the decimal system that number is 10. The question is, if we are counting in hex and we reach F16, what is the next number? The answer is 1016. Don't confuse 1016 with 1010. When verbally reading the values, 1016 is read a "One Zero" not "Ten". The hex digits then continue 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20, 21,… etc. 98, 99, 9A, 9B, 9C, 9D, 9E, 9F, A0, A1, … etc. EF, F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, FA, FB, FC, FD, FE, FF, 100, 101,… etc.
Hex values are often indicated by using a dollar sign '$', for example: 1016 is written as $10. They are also sometimes indicated by preceding them with '0x', for example 0x10. They may also be represented by a trailing 'h', for example: 10h. We must always use some means of indicating the base of a number because 1010 is not the same as 1016.
Some handy hex values to know are:

$10 = 16 $400 = 1024

$FF = 255 $800 = 2048

$100 = 256 $1000 = 4096

$200 = 512 $FFFF = 65535


Special Purpose Codes (BCD)

BCD - Binary Coded Decimal

BCD is a technique that can be used to encode decimal numbers. Each decimal digit is represented using four binary digits. Wait a minute, isn't that the same way that Hex digits are encoded into binary? Yes it is! What's the difference? The answer is, as far as encoding is concerned, there isn't any difference. Let's look at an example that encodes two numbers. One is a hex value that is encoded into binary and the other is a decimal value that is encoded into BCD.

987616 = 1001 1000 0111 01102


987610 = 1001 1000 0111 0110BCD
Remember, the way values are represented in the computer is entirely up to the programmer. There is in fact no difference between the way the previous two values are represented in binary. The difference is in how the programmer deals with the binary values. Most microprocessors have special instructions for working with BCD values. We will take a closer look at BCD numbers later.
The main disadvantage of using BCD numbers is their inefficient use of storage. With four binary digits we could represent decimal numbers from 0  15 however in BCD we only represent digits 0  9. This means a 16 bit BCD number can represent decimal values 0  9999 whereas a 16 bit binary number can represent decimal values 0  65535.
Binary Arithmetic

Table 1.5 The binary tables



ADDITION SUBTRACTION MULTIPLICATION
0 + 0 = 0 0 - 0 = 0 0 x 0 = 0

0 + 1 = 1 0 - 1 = 1 borrow 1 0 x 1 = 0

1 + 0 = 1 1 - 0 = 1 1 x 0 = 0

1 + 1 = 0 carry 1 1 - 1 = 0 1 x 1 = 1



Binary addition

00110111 55

+01010110 +86

111 11  carries

-------- ----

10001101 141




Hexadecimal addition

9A345


6701B

11 1  carries

------

101360



Binary subtraction

01010110 86

-00101010 -42

1 1  borrows

-------- ----

00101100 44


Signed Numbers

Sign and Magnitude Representation An 8 bit binary number can represent 256 different decimal numbers 0…255. One way of representing negative numbers is to use the most-significant bit as a sign bit. Usually 0 indicates a positive number and 1 indicates a negative number. Using this technique:
0 0001101 = +13 1 0001101 = -13

| |


|-sign bit |- sign bit
One side effect of this method is that it yields two representations of zero:
00000000 = +0 and 10000000 = -0
Complementary Arithmetic

In complementary arithmetic negative numbers are represented by their complement. The complement of an n-digit decimal number, N, is defined as 10n - N. The 10's complement may also be calculated by subtracting each of the digits of N from 9 and adding 1 to the result. For example the 10's complement of 1234 is:


104 - 1234 = 8766 or 9999

-1234


----

8765 + 1 = 8766


8766 is the 10's complement of 1234. Adding the complement of a number is very similar to the result obtained by subtracting the original value. For example
8576 8576

+ 8766 - 1234

------ ------

17342 7342


The results are identical in the least-significant four digits.
Two's Complement Representation To calculate the two's complement of a binary number we simply invert all of the bits and add 1. For example, the two's complement of 0011 1001 is 1100 0110 + 1 = 1100 0111. Adding the two's complement of a binary number to another number has the same effect as subtracting the original value, provided we ignore the most significant bit of the result.
9 01001 9 01001

+6 +00110 -6 +11010

15 01111 3 100011


-9 10111 -9 10111

+6 +00110 -6 +11010

-3 11101 -15 110001


Note, that all results are in two's complement form.

Properties of Two's Complement Numbers


  • There is one unique zero, 00…0

  • If the number is positive the most-significant bit, MSB, is 0, and if it is negative the MSB is 1. Thus, the MSB is a sine bit.

  • The range of two's complement numbers in n bits is from -2(n-1) to +2(n-1) - 1.

  • The complement of the complement of X is X. (i.e., -(-X) = X.



Arithmetic Overflow


When performing two's complement arithmetic it is possible to obtain results that are unexpected if the resulting value is too large or small for the given number of bits.
12 01100

+13 +01101

25 11001 = -710 (as a two's complement number)


Floating Point Numbers

Representation of Fixed Point Numbers



Case 1 Integer arithmetic Case 2 Fixed-point arithmetic

7632135 763.2135

+ 1794821 + 179.4821

9426956 942.6956


There is no difference between the calculations in the previous two examples. The same is true for binary arithmetic. All the computer programmer has to do is remember where the binary point is assumed to lie. All input to the computer is called to match this convention and all output is similarly scaled. The internal operations themselves are carried out as if the numbers were in integer form. For example, assume an 8-bit fixed point binary number, the most-significant four bits represent the integer part and the least-significant four bits represent the decimal part. We want to add 3.625 and 6.5.
3.62510  0011.10102

6.510  0110.10002


Each of these values is treated as an integer and the addition is performed.
00110101

+ 01101000

101000102  1010.00102  10.12510
The resulting integer is separated into its integer and decimal parts yielding the correct answer.


  • The advantage of using fixed-point numbers is that no complex software or hardware is needed to implement it.

  • A limitation of using fixed-point numbers is that working with very large or very small values requires many bytes of storage, most of which goes unused.


Representation of Floating Point Numbers

The decimal number 1234.56 may be written as 0.123456 x 104. Binary numbers may also be written using the same technique. For example, 1101101.11011012 may be written as 0.11011011101101 x 27 where the 7 is also stored in binary format.


Range - How big or how small the number can be. The range is determined by the number of bits used to represent the exponent.
Precision - How many digits can be accurately represented. Precision is determined by the number of bits used to represent the mantissa.

Normalization of Floating Point Numbers
The floating point mantissa is always normalized so that it is expressed in the form 0.1 … x 2e. By normalizing a mantissa, the greatest advantage is taken of the available precision.
For example: the binary number 0.000010100011 has eight significant bits, storing it with an un-normalized 8-bit mantissa 0.00001010 reduces it to just four significant bits, while using a normalized 8-bit mantissa 0.10100011 x 2-4 keeps eight significant bits.

Biased Exponents

Floating point representations of numbers must make provision for both positive and negative numbers, and positive and negative exponents. The exponent is usually represented in a biased form. For example: Assume we wish to represent an exponent by using three bits. The possible range of numbers would be 0,1,2,3,4,5,6,7. When biased exponents are used a bias value is picked that divides the range of values in half. In our example this value would be 4. This bias number is then added to each exponent before it is represented in binary. If the original exponent is -4 then adding the bias value of 4 yields 0. The new range of exponents we can represent in three bits with a bias value of four is -4,-3,-2,-1,0,1,2,3.



Original Bias Result Binary


-4 + 4 = 0  000

-3 + 4 = 1  001

-2 + 4 = 2  010

-1 + 4 = 3  011

0 + 4 = 4  100

1 + 4 = 5  101

2 + 4 = 6  110

3 + 4 = 7  111


With biased exponents, the most negative exponent is represented by zero.
IEEE Floating Point Format
The Institute of Electrical and Electronics Engineers (IEEE) has produced a standard floating point format. IEEE floating point numbers are normalized so that their mantissas always have an integer part of 1. That is, all IEEE floating point binary numbers are in the range 1.0 <= mantissa < 2.0. An IEEE format floating point number X is defined as:

X = -1S x 2E-B X 1.F


where:

S = sign bit: 0 = positive mantissa, 1 = negative mantissa

E = exponent biased by B

F = fractional mantissa

Since the normalized mantissa always contains a 1 in the integer part, there is not need to store it. In this way a bit of storage can be saved, permitting the precision of the mantissa to be extended by one bit. The following is an example of the use of the IEEE 32-bit format to store the decimal number -2345.125 on a machine having a 16-bit word length.
-2345.12510 = -100100101001.0012 (as an equivalent binary number)

= -1.00100101001001 x 211 (normalized binary number)




  • The mantissa is negative, so the sign bit S is 1.

  • The biased exponent is given by +11 + 127 = 138 = 100010102.

  • The fractional part of the mantissa is .00100101001001000000000 (in 23 bits).

Therefore, the IEEE single format representation of -2345.125 is:


1 10001010 00100101001001000000000
A 32-bit computer would store this number as two consecutive 16-bit words:
1100010100010010 1001001000000000

Table 1.7 Basic IEEE floating point formats

Type Single Double Quad



Field width in bits

S = sign 1 1 1

E = exponent 8 11 15

L = leading bit 1 1 1

F = fraction 23 52 111

Total width 32 64 128



Exponent


Maximum exponent 255 2047 32767

Minimum exponent 0 0 0

Bias 127 1023 16383
Normalized numbers (all formats)

Range of exponents: (Min E = 1) to (Max E - 1)

Represented number: -1S x 2 E-bias x L.F
A signed zero is represented by the minimum exponent, L = 0, and F = 0, for all three formats. The maximum exponent has a special function and is used to represent signed infinity for all three formats.

Floating Point Arithmetic

Addition and Subtraction


Floating point numbers may only be added or subtracted when they have the same exponent. To perform a floating point addition/subtraction the following steps must be carried out:


  1. Identify the number with the smaller exponent.

  2. Make the smaller exponent equal to the larger by dividing the mantissa of the smaller number by the same factor by which its exponent was increased.

  3. Add (or subtract) the mantissas.

  4. If necessary, normalize the result (post-normalization).


Logical Operations on Binary Values

AND OR


x y x AND y x y x OR y

0 0 0 0 0 0

0 1 0 0 1 1

1 0 0 1 0 1

1 1 1 1 1 1


XOR NOT


x y x EOR y x NOT x

0 0 0 0 1

0 1 1 1 0

1 0 1


1 1 0
Examples:

AND x = 11010110

y = 11110000

x&y = 11010000

The AND operation acts like a mask and can be used to force one or more bits of a word to zero.


OR x = 00010110

y = 11000000

x|y = 11010110

The OR operation can be used to force one or more bits of a word to ones.


EOR x = 11010110

y = 01100111



xy = 10110001
Download 55.9 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page