Chapter 8
How to use Arithmetic instructions
-----------------------------------

All arithmetic must be done in one of the general purpose registers. To add two numbers, put one number in the a register and the other can be stored in another register or memory location. I suggest the AX register, because it was optimized to work as an accumulator in arithmetic operations. Below are three forms of this instruction that you should be familiar with.

16 bit ADD instructions. (any instruction that
----------------------- uses AX, BX, CX or DX)

Value in BX is added to the value in AX and the result is placed in AX.

Value stored at offset 120 and 121 in memory is added to the value in AX and the result is placed in AX. The CPU will automatically calculate the number of memory locations to use when it fetches data from memory. In this case, the register specified in the instruction is a 16 bit register and requires two bytes of data from memory which starts at offset 120 (the least significant byte) and 121 (the most significant byte).

The hex number 5 is added to the value in AX and the result is placed in AX. In this example, the number 5 is actually 0005 in hex. When we assemble a program it is OK to leave out the leading zero's as I did here, Debug will just insert them for you.

8 bit ADD instructions. (any instruction that ---- uses AH, AL, BH, BL, CH, CL, DH, DL are 8 bit instructions)

Value in AL is ADDED to the value in the AH and the result is placed in AH.

The value stored at offset location 150 in memory is added to the value in AH and the result is placed in AH.

Example:
This short program will add to hex numbers and store the result in memory.

MOV AX,3405 put first number in a register
MOV ,AX mov the number to a memory location for temporary storage
MOV AX,5892 put the second number in a register
ADD AX, add a number in the register and put the result back in the register
MOV ,AX save the result at a memory location
INT 20 end the program

INC AX

Increment will increase the value in the specified register or memory location by 1 each time it is executed. It is the equivalent of ADD AX,1, where 1 is added to the specified register. INC can be used as many times as you like. In this example, the AX register will be increased by one each time it is executed. So if the AX register has the number 0005 in it, the INC AX instruction will increase it to 0006.

INC BL

Increments the value in the specified 8 bit register.

INC BYTE 

Increments the byte value at the specified memory location.

INC WORD 

Increments the word (16 bit number) at the specified memory location.

SUB AX,CX

The 16 bit value in the source register is subtracted from the value in the destination register and the result is placed in the destination register.

SUB CX,

The value stored at the specified location is subtracted from the value in the specified register and the result is placed in the destination register.

SUB AL,AH

The 8 bit value in the source register is subtracted from the value in the destination register and the result is placed in the destination register.

Note:
If the result of the subtract is zero, the zero flag is set.
DEC AX

Decrement will decrease the value in the specified register or memory location by 1 each time it is executed. It is the equivalent of SUB AX,1, where 1 is subtracted from the specified register. DEC can be used as many times as you like. In this example, the AX register will be decreased by one each time it is executed. So if the AX register has the number 0005 in it, the DEC AX instruction will decrease it to 0004.

DEC AL

Decrements the value in the specified 8 bit register.

DEC BYTE 

Decrements the one byte value stored at the offset specified. In this example the byte stored at offset 250 is decreased by one.

DEC WORD 

Decrements the word (16 bit number) stored at the offset specified. In this example the 16 bit number stored at offset 200 is decreased by one.

Note: If the result is zero, the zero flag is set.

NEG AX
NEG AL
NEG BYTE 

Negates the value in the specified register
or memory location. Negate is the two's complement of a number.

CMP AX,56
CMP AL,BL
CMP AL,
CMP AX,
The compare instruction will compare the numbers in both registers or memory locations and if they are equal the zero flag is set. This instruction is used to change the flow of a program based on the condition of the zero flag. The following program will demonstrate how this is done.

MOV AH,09-----Print the message at offset 200. Message ends with hex 24
MOV DX,200---(hex 24 is the \$ character)
INT 21
MOV AH,7-------Wait for a key to be pressed and put the ascii code
INT 21------------for it in the al register. Compare the ascii code for
CMP AL,1B------the key that was pressed with 1B which is the ascii code for the
----------------------escape key. If the compare is equal, the zero flag is set.
JNZ 100----------Here the program can go in one
of two directions. If the zero flag is not zero, the instruction at offset 100 is executed.(JNZ or jump if not zero to) This causes the program to read a new key value and repeat. If the key pressed was the escape key, the zero flag is set and the next instruction is executed.

Note: Assemble this program beginning at offset 100 and use the P command to single step through the instructions. You must assemble the message at offset 200 and end it with the dollar sign.

A200

xxxx:0200 DB "PRESS THE ESC KEY IF YOU ARE TIRED OF READING THIS MESSAGE.\$"

MUL AX,BX
MUL AL,05

DIV AX,CX
DIV AL,BL

MUL and DIV are special instructions and will not be explained here.

Special debug instructions
--------------------------

DB (define byte)
DB (define word)

DB and DW is used to define a data type in memory.

DB "This is a message" will store the ascii string that is enclosed in quotations at the address of the assembly instruction.

For example, if you enter,

A150

XXXX:0150 DB "My name is John Smith"

then, the ascii string My name is John Smith is stored in memory, starting at offset 150.

You can also use DB to load specific byte of data into memory.

DB 41 42 43 44 45

Would load ascii letters A B C D E into memory at the specified memory location .

How to use the Data transfer instructions (MOV)
-----------------------------------------------

There are six different data-addressing modes used for data transfers with the 8088 CPU. A number enclosed in brackets is a memory location at the offset specified my the number.  is a memory cell at offset 150 in RAM that can hold 8 bits or one byte of data. If a register is enclosed with brackets [BX], then the CPU will go to the memory location specified by the register for the data.

1. MOV AX,BX

register,register Data is transferred from the BX reg. into the AX reg. (16 bit transfer)

MOV AH,BL

register,register Data is transferred from the BL reg. into the AH reg. (8 bit transfer)

2. MOV AX,5

immediate The hex value in the source (5) is moved directly into the AX reg.(16 bit transfer) 5 is really 0005

MOV AH,5

immediate The hex value in the source (5) is moved directly into the AH reg. (8 bit transfer) 5 is really 05
3. MOV AX,

direct The brackets are used to enclose a memory location.The value stored at offset location 200 and 201 is moved into the into the AX reg. Each memory location is one byte and since this a 16 bit or two byte transfer it will use two memory locations. The Al reg. will get the value at 200 and the AH will get the value at 201.

MOV AL,

direct The value stored at offset450 is moved into the AL reg. Since this is an 8 bit transfer it will only use one memory location.
4.MOV AX,[BX]

indirect The value stored at the location that the BX reg points to will be transferred into the AX reg.Suppose the BX reg has the value 345 in it. The AX reg would than get the value at location 345 and 346. (16 bits)

MOV CL,[BX]

indirect same as above but is an 8 bit transfer

5. MOV AX,[BX+5]

base+displacement Same as above but the number is added to BX to get the location of the data.(16 bit transfer)

MOV AH,[BX+5]

base+displacement Same as above but 8 bit transfer.

6. MOV AX,[BX+SI+8]

base+index+8 Here there are two reg.displacement used + a value. The CPU will calculate the location in memory using the values in the BX and SI reg. plus the value specified.(16 bit transfer)

MOV AL,[BX+SI+6]

base+index+6 same as above but 8 bit displacement transfer.

Note: We are only going to assemble programs using modes 1,2,3 and 4

How to use the PUSH and POP transfer instructions
-----------------------------------------------

PUSH AX
POP AX

There are four general purpose registers that can be used by our program to transfer data and preform arithmetic operations. This may seem like enough, but when writing a program you may find yourself looking for a few more registers to work with. The PUSH and POP instruction will allow us to save the contents of the registers being used into the stack area of memory. Once the contents are saved, we can re-use them for more operations and when finished, restore them to their original values they had before they were saved.

PUSH CX will cause the contents of the CX register to be saved in the stack area of memory. The stack area is located at the top of the segment our program is in at the time. The first location available in the stack is located at offset FFFE and will decrement downward in memory. The stack is what we call "First In and Last Out" and we must pay attention to how we use the PUSH and POP instructions. Think of the stack as a pile of dishes, The first dish we put in the shack will have to be the last dish we take off of the stack. If we try to take the dishes off in any other order the pile will most likely crash.If we do the same thing in our program, the computer will crash.

Example:

Look at the following program,

MOV CX,115F
MOV AX,BX
OUT DX,AL
INC CX
PUSH CX
PUSH AX
MOV DX,03BC
MOV AX,
MOV CX,BX
OUT DX,AL
POP AX
POP CX
RET

The first 3 instructions are using the CX and AX registers to hold values needed in this part of the program. After the CX register is incremented, two push instructions are exetuted,.(PUSH CX and PUSH AX) that will save the current value of CX and AX on the stack. CX and AX can then be used for more instructions, then POP AX and POP CX in that order, will restore the values that were in them in the first part of the program.

When our program uses an INT instruction,the CPU will change the IP reg to point to a sub-routine program inside the ROM bios chip or the IBMDOS.SYS program in low memory. These sub-routines will use all of the general purpose registers that are program was using before the INT instruction was executed. IBM says all registers are preserved (saved) when an INT is executed except the AX register. I would suggest that you PUSH the AX register every time you use the INT instruction.

• Chapter 9