One can add a displacement to a variable as follows:

.data
arry BYTE 2,4,6,8,10
.code
mov al, arry
mov ah, [arry+3] ; same as mov ah,arry+3
Note: There is no range checking, so mov ah,arry+10 is a big problem.

Problem: Consider the following:

.data
v DWORD 1,2,3
.code
mov eax,v
mov ebx,v+4
mov ecx,v+1
Q#1: What values are in eax, ebx, and ecx after the above program segment is executed?

(i7) INC r/m

INC increments the operand by 1.

(i8) DEC r/m

DEC decrements the operand by 1.

.data
v BYTE 5
.code
mov dl, v
inc v
dec dl
Q#2: What values are in dl and v?

(i9) ADD dest,src

The ADD instruction adds the value in dest to src and places the result in dest.

(i10) SUB dest,src

The SUB instruction subtracts the value in src from dest and places the result in dest.

We are mainly concerned with binary addition and subtraction in modulo 2^n and two's complement number systems. An interesting note is that the computer can deal with binary addition and subtraction for both modulo 2^n and two's complement numbers in the same way. The interpretation of the result is the determining factor. This will become more clear as we do examples.

Addition

In general, we know the following is true:

0+0=00
0+1=01
1+0=01
1+1=10

P#1: Perform the following addition and interpret the result in: (a) modulo 2^n and (b) two's complement notation.

 010010101        10101010
+000111001       +10101010
 ---------        --------

We must become familiar with the use of the carry. There are a couple of different carries that we must concern ourselves with.

The first is the carry-in and carry-out.

P#2: Add the following two binary values discussing what is meant by carry-in and carry-out.

 00001111
+01010101
 --------

Note: The carry-out of the MSB is the value that the external carry flag found in the flags register takes on.

Q#3: In the previous example, what would be the value of the external carry? Why?

Subtraction

Subtraction works a little differently than one would think. In particular, subtraction is performed by taking the two's complement of the subtrahend and adding this value to the minuend. Let's look at the following example for some clarification.

Perform the following subtraction:

 00110011    (Minuend)
-00001111    (Subtahend)
 --------

Q#4: Before performing the subtraction, identify the two numbers being subtracted. Assume the numbers are represented in modulo 2^n.

P#3: Now perform the subtraction.

Q#5: Interpret the result. Is it what you would expect it to be?

P#4: Switch the minuend and the subtrahend from the above problem and then perform the subtraction. Interpret the result if the numbers are: (a) modulo 2^n and (b) two's complement numbers.

Arithmetic Overflow

Remember that the range of values that can be represented using 8-bits for modulo 2^n numbers is 0 to 255 and for two's complement is -128 to 127. The microprocessor will perform the addition or subtraction of two numbers, but the question is how do we know if the result is correct. That is, if we add two 8-bit numbers and the result is larger than the representation allows, how do we know this happened. The answer lies with two flags: (a) the carry flag and (b) the overflow flag.

First we will define overflow as a condition such that an arithmetic operation produces a result outside the range of the number system being used.

P#5: Perform the operations below and interpret the result in: (a) modulo 2^n and (b) two's complement notation.

 10101010     10101010
+11110000    -11110000
 --------     --------

Q#6: Were there any examples of overflow? Identify each case and briefly explaing why.


©Douglas J. Ryan/ryandj@pacificu.edu