Sei sulla pagina 1di 8

Kingdom of Saudi Arabia $%‫ ا*)('د‬$,-.

(*‫ ا‬$/011*‫ا‬
Royal Commission at Yanbu 234% $,/01*‫ ا‬$5,6*‫ا‬
Yanbu University College (Boys' and Girls Campus) ( ;,43*‫م ا‬.=) 234%-$,(789*‫ ا‬$,0/*‫ا‬
Department of CS / MIS 116ext 3932961 :Tel
Yanbu Al-Sinaiyah $,@84A*‫ ا‬234%

BACHELOR OF SCIENCE IN COMPUTER SCIENCES


Lab Work

Computer Organization and Assembly ( CS203)


Second Semester - Academic Year 2007 – 08
Lab Sheet 3

Practicing arithmetic instructions in 8086

Objective:
The objective of this lab is to make students practice

1. Demonstrate ADD and INC instructions


2. Practice SUB and DEC instructions

Outline:
Using ADD, SUB, INC AND DEC arithmetic instructions in 8086

1
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Introduction:

In this experiment, you will be introduced to the arithmetic instructions of the 8086 family of
processors.

ADD used for adding


INC used for incrementing
SUB used for subtracting
DEC used for decrementing

Flags Affected
Operation Op-code Code Example Meaning OF SF ZF AF PF CF

Addition ADD ADD AX,5 AX ← AX + 5 * * * * * *


INC INC BX [BX]←[BX]+1 * * * * * -
Subtraction SUB SUB CL,AH CL ← CL – AH * * * * * *
DEC DEC VAR1 [VAR1] ← [VAR1] – 1 * * * * * -
Multiplication MUL MUL CL AX ← AL * CL * ? ? ? ? *
MUL CX (DX,AX) ← AX* CX

Division DIV DIV CX AX ← Q(([DX,AX])/CX) ? ? ? ? ? ?


DX ←R(([DX,AX])/CX)

• * means flag changed


• - means flag not affected by instruction
• ? means flag is undefined after executing of instruction

ADD instruction

ADD Register, Immediate Number


Register, Register
Register, Memory Location
Memory Location, Register

Example:

ADD AL, 34; 34 is added with data of register AL and result is stored in AL
ADD AH, AL; Data of AL is added with data of AH and result is stored in AH
ADD AX, Number1; Data stored in memory location ‘Number1’ added with AX
ADD Number1, BX; Data of register BX is added with Memory Location

2
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Exercise:

1. Write and complete the program given in the example below which should also display the
result of AL register on the screen. Use CodeView to see the changes in the registers while
stepping over the code using F10.

8-bit signed and unsigned addition


.DATA

MEM8 DB 3

.CODE

; ADDITION
;
MOV AL, 2 ; START WITH REGISTER
INC AL ; AL + 1 -> AL
ADD AL, 4 ; AL + 4 -> AL
ADD AL, MEM8 ; AL + 3 -> AL
;
MOV AH, AL ; COPY TO AH

ADD AL, AH ; AL + AH -> AL

2. Type the program below and observe the output.


TITLE "PROGRAM 1 EXPERIMENT 3"

; This program reads two numbers from the keyboard and


; gives their sum. This program uses internal registers
; to store the variables.

.MODEL SMALL
.STACK 200
.DATA
CRLF DB 0DH,0AH,'$'
PROMPT1 DB 'Enter the first positive integer: ','$'
PROMPT2 DB 'Enter the second positive integer: ','$'
PROMPT3 DB 'The sum of the two numbers is: ','$'
.CODE
.STARTUP
LEA DX,PROMPT1 ;DISPLAY PROMPT1
MOV AH,09H
INT 21H

MOV AH,01H ;READ FIRST NUMBER


INT 21H
SUB AL,30H ;Convert character to number
MOV CL,AL ;SAVE THE NUMBER IN CL

LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE


MOV AH,09H

3
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
INT 21H
LEA DX,PROMPT2 ;DISPLAY PROMPT2
MOV AH,09H
INT 21H

MOV AH,01H ;READ SECOND NUMBER


INT 21H
SUB AL,30H ;Convert character to number
ADD AL,CL ;PERFORM ADDITION AND SAVE RESULT IN CL

MOV CL,AL
ADD CL,30H ;CONVERT DIGIT TO CHARACTER

LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE


MOV AH,09H
INT 21H
LEA DX,PROMPT3 ;DISPLAY PROMPT3
MOV AH,09H
INT 21H

MOV DL,CL ;DISPLAY SUM


MOV AH,02H
INT 21H
.EXIT
END

SUB Instruction

SUB instruction works in a similar manner as ADD instruction.

Exercise

1. Complete the following program and write it, assemble it and run it to view the contents of AL
register on the screen. Use CodeView to trace through program using F10. You can give the
following command.

C:\Program Files\MASM615\cv subexample.exe

Then use F10 to trace through the program

8-bit signed and unsigned subtraction


; SUBTRACTION
;
MOV AL, 9 ; AL <- 9
DEC AL ; AL – 1 ->AL
SUB AL, 2 ; AL – 2 ->AL
;
;
SUB AL, MEM8 ; AL – 3 ->AL
;
;
MOV AH, 4 ; AH <- 4
SUB AL, AH ; AL – AH ->AL

4
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
2. Write the following program and view the output.

Example 2
TITLE "PROGRAM 2 EXPERIMENT 4"
; This program reads two numbers from the keyboard and
; displays their sum. This program uses the memory to
; store the variables.
.MODEL SMALL
.STACK 200
.DATA
CRLF DB 0DH,0AH,'$'
PROMPT1 DB 'Enter the first positive integer: ','$'
PROMPT2 DB 'Enter the second positive integer: ','$'
PROMPT3 DB 'The sum of the two numbers is: ','$'
NUM1 DB ?
NUM2 DB ?
RES DB ?
.CODE
.STARTUP
LEA DX,PROMPT1 ;DISPLAY PROMPT1
MOV AH,09H
INT 21H
MOV AH,01H ;READ FIRST NUMBER
INT 21H
SUB AL,30H ;Convert character to number
MOV NUM1,AL ;SAVE NUM1
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT2 ;DISPLAY PROMPT2
MOV AH,09H
INT 21H
MOV AH,01H ;READ SECOND NUMBER
INT 21H
SUB AL,30H ;Convert character to number
MOV NUM2,AL ;SAVE NUM2
ADD AL,NUM1 ;PERFORM ADDITION
MOV RES,AL ;SAVE RESULT IN RES
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT3 ;DISPLAY PROMPT3
MOV AH,09H
INT 21H
;DISPLAY SUM
MOV DL,RES ;RETREIVE RES FROM MEMORY
ADD DL,30H ;CONVERT DIGIT TO CHARACTER
MOV AH,02H
INT 21H
.EXIT
END

5
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Using Multiplication Instructions:

The MUL instruction multiplies unsigned numbers. IMUL multiplies signed numbers. For both
instructions, one factor must be in the accumulator register (AL for 8-bit numbers, AX for 16-bit
numbers). The other factor can be in any single register or memory operand. The result overwrites the
contents of the accumulator register. Multiplying two 8-bit numbers produces a 16-bit result returned
in AX. Multiplying two 16-bit operands yields a 32-bit result in DX:AX.
The following examples illustrate multiplication of unsigned 8 integers.

multiplication of unsigned 8-bit integers


; 8-bit unsigned multiply
mov al, 3 ; Load AL
mov bl, 2 ; Load BL
mul bl ; BL * AL -> AX
; Product in AX
; overflow and carry set

multiplication of unsigned 16-bit integers


; 8-bit unsigned multiply
mov ax, 3 ; Load AL
mov bx, 2 ; Load BL
mul bx ; BX * AX -> (DX:AX)
; DX has high 16 bits
; AX has lower 16 bits

6
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
Exercise

1. Write a program which multiplies two digits 25 with 36 (both 8 bit) and then display the
result on the output screen. You can save these two digits in the memory using DB
directive. Use CodeView to trace through the program.
2. Write a program which multiplies two 16 bit digits and stores the 32 bit result in DX:AX
register. The numbers are 269h and 345h. You can save these two digits using DW
directive. (Note: To show the full result you have to show first display the DX register
contents and then AX register contents).Use CodeView to trace through the program.

Using Division Instructions:

The DIV instruction divides unsigned numbers, and IDIV divides signed numbers. Both return a
quotient and a remainder. Table summarizes the division operations. The dividend is the number to be
divided, and the divisor is the number to divide by. The quotient is the result. The divisor can be in any
register or memory location except the registers where the quotient and remainder are returned.

AX ← Quotient(([DX,AX])/CX)

DX ←Remainder(([DX,AX])/CX)
Size of Operand Dividend Register Size of Divisor Quotient Remainder
16 bits AX 8 bits AL AH
32 bits DX:AX 16 bits AX DX

Table: Division Operations

Unsigned division does not require careful attention to flags. The following examples illustrate signed
division, which can be more complex.

unsigned division
; Divide 16-bit unsigned by 8-bit
mov ax, 700 ; Load dividend 700
mov bl, 36 ; Load divisor DIV 36
div bl ; Divide BL ------
; Quotient in AL 19
; Remainder in AH 16

Exercise

1. Write and complete the following two codes for the examples given below. Use CodeView
to trace the execution of the program and see the contents of the register. (You can use
F10 to trace the execution of program)

7
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC
2. Write a program which divides a number with 8 bit divisor. Divide 344h with 02h. Use
CodeView to trace through program.

8
___________________________________________________________________________________________________
Prepared by: Khurram Tanvir CS Department YUC

Potrebbero piacerti anche