• No results found

Integer representation

In document PC Assembly Language book pdf (Page 33-36)

1.5 Skeleton File

2.1.1 Integer representation

Integers come in two flavors: unsigned and signed. Unsigned integers (which are non-negative) are represented in a very straightforward binary manner. The number 200 as an one byte unsigned integer would be repre- sented as by 11001000 (or C8 in hex).

Signed integers (which may be positive or negative) are represented in a more complicated ways. For example, consider−56. +56 as a byte would be represented by 00111000. On paper, one could represent 56 as 111000, but how would this be represented in a byte in the computer’s memory. How would the minus sign be stored?

There are three general techniques that have been used to represent signed integers in computer memory. All of these methods use the most significant bit of the integer as a sign bit. This bit is 0 if the number is positive and 1 if negative.

Signed magnitude

The first method is the simplest and is calledsigned magnitude. It rep- resents the integer as two parts. The first part is the sign bit and the second is the magnitude of the integer. So 56 would be represented as the byte 00111000 (the sign bit is underlined) and −56 would be 10111000. The largest byte value would be 01111111 or +127 and the smallest byte value would be 11111111 or −127. To negate a value, the sign bit is reversed. This method is straightforward, but it does have its drawbacks. First, there are two possible values of zero, +0 (00000000) and 0 (10000000). Since zero is neither positive nor negative, both of these representations should act the same. This complicates the logic of arithmetic for the CPU. Secondly,

general arithmetic is also complicated. If 10 is added to−56, this must be recast as 10 subtracted by 56. Again, this complicates the logic of the CPU.

One’s complement

The second method is known as one’s complement representation. The one’s complement of a number is found by reversing each bit in the number. (Another way to look at it is that the new bit value is 1−oldbitvalue.) For example, the one’s complement of 00111000 (+56) is 11000111. In one’s com- plement notation, computing the one’s complement is equivalent to nega- tion. Thus, 11000111 is the representation for −56. Note that the sign bit was automatically changed by one’s complement and that as one would ex- pect taking the one’s complement twice yields the original number. As for the first method, there are two representations of zero: 00000000 (+0) and 11111111 (−0). Arithmetic with one’s complement numbers is complicated. There is a handy trick to finding the one’s complement of a number in hexadecimal without converting it to binary. The trick is to subtract the hex digit from F (or 15 in decimal). This method assumes that the number of bits in the number is a multiple of 4. Here is an example: +56 is represented by 38 in hex. To find the one’s complement, subtract each digit from F to get C7 in hex. This agrees with the result above.

Two’s complement

The first two methods described were used on early computers. Modern computers use a third method calledtwo’s complement representation. The two’s complement of a number is found by the following two steps:

1. Find the one’s complement of the number 2. Add one to the result of step 1

Here’s an example using 00111000 (56). First the one’s complement is com- puted: 11000111. Then one is added:

11000111

+ 1

11001000

In two complement’s notation, computing the two’s complement is equiv- alent to negating a number. Thus, 11001000 is the two’s complement rep- resentation of −56. Two negations should reproduce the original number. Surprising two’s complement does meet this requirement. Take the two’s

Number Hex Representation 0 00 1 01 127 7F -128 80 -127 81 -2 FE -1 FF

Table 2.1: Two’s Complement Representation

complement of 11001000 by adding one to the one’s complement. 00110111

+ 1

00111000

When performing the addition in the two’s complement operation, the addition of the leftmost bit may produce a carry. This carry is not used. Remember that all data on the computer is of some fixed size (in terms of number of bits). Adding two bytes always produces a byte as a result (just as adding two words produces a word, etc.) This property is important for two’s complement notation. For example, consider zero as a one byte two’s complement number (00000000). Computing its two complement produces the sum:

11111111

+ 1

c 00000000

wherec represents a carry. (Later it will be shown how to detect this carry, but it is not stored in the result.) Thus, in two’s complement notation there is only one zero. This makes two’s complement arithmetic simpler that the previous methods.

Using two’s complement notation, a signed byte can be used to represent the numbers −128 to +127. Table 2.1 shows some selected values. If 16 bits are used, the signed numbers −32,768 to +32,767 can be represented. +32,767 is represented by 7FFF,−32,768 by 8000, -128 as FF80 and -1 as FFFF. 32 bit two’s complement numbers range from−2 billion to +2 billion approximately.

The CPU has no idea what a particular byte (or word or double word) is supposed to represent. Assembly does not have the idea of types that a high level language has. How data is interpreted depends on what instruction is used on the data. Whether the hex value FF is considered to represent a signed−1 or a unsigned +255 depends on the programmer. The C language

defines signed and unsigned integer types. This allows a C compiler to determine the correct instructions to use with the data.

In document PC Assembly Language book pdf (Page 33-36)