Lecture 5 Arithmetic Instructions

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 5 Arithmetic Instructions as PDF for free.

More details

  • Words: 1,552
  • Pages: 7
‫ا  ا   ا د‬    ‫ا   ا‬ -  ‫ا   ا‬

Kingdom of Saudi Arabia Royal Commission at Yanbu University College – Yanbu Department of ACS & AIT Yanbu Al-Sinaiyah

  ‫ ا‬

2ND Semester 2007-08

CS-203 HANDOUT 1 (HO - 5) ARITHMETIC INSTRUCTIONS IN 8086

5. Introduction This unit deals with the arithmetic instructions used with the 8086 processor. 5.1

Instructions to Add and Subtract

8086 uses same instructions to add and subtract signed and unsigned numbers. These are as follows: ADD used for adding INC used for incrementing SUB used for subtracting DEC used for decrementing All these instructions operate on values in single registers, memory or constants. They can also be combined to handle larger values that require two registers for storage. The syntax for Add and subtract are as follows: ADD

Register, Immediate Number Register, Register Register, Memory Location Memory Location, Register

Operation Addition Subtraction

Opcode

Code Example

Meaning

ADD INC SUB DEC

ADD AX,5 INC BX SUB CL,AH DEC VAR1

AX ← AX + 5 [BX]←[BX]+1 CL ← CL – AH [VAR1] ← [VAR1] – 1

Flags Affected OF

SF

ZF

AF

PF

CF

* * * *

* * * *

* * * *

* * * *

* * * *

* * -

Example: ADD AL, 34; ADD AH, AL; ADD AX, Number1; ADD Number1, BX;

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

Prepared by: Khurram Tanvir

1

CS203 HO#5

Note: Destination cannot be an immediate number. In an instruction, source and destination cannot be memory. ADD 34, AL;

-> WRONG

ADD NUM1, NUM2 -> WRONG (memory to memory addition not allowed in 8086) 5.2

Instructions of Multiplication and Division

To multiply and divide, 8086 has different instructions for signed and unsigned numbers. Multiplication and division instructions have also special requirements depending on the size of the operands and the processor the code runs on. • • • •

MUL used to multiply the unsigned numbers IMUL used to multiply the signed numbers DIV used to divide the unsigned numbers IDIV used to divide the unsigned numbers

The syntax for multiply and divide are as follows: • • • •

MUL Register IMUL Register DIV Register IDIV Register

example example example example

MUL CX IMUL CX DIV CX IDIV CX

Note that before performing the multiplication, we need to store the first number in the accumulator register. Also to perform the division, we need to store the number to be divided (dividend) in data register. Following table shows the example codes, their meaning, and flags affected after the execution of the instruction.

• • •

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

The next examples show 8-bit signed and unsigned addition and subtraction. 8-bit signed and unsigned addition .DATA MEM8 .CODE

DB

; ADDITION MOV INC ADD ADD MOV ADD

Prepared by: Khurram Tanvir

3

AL, AL AL, AL, AH,

; ; ; 4 ; MEM8 ; AL ; 2

AL, AH

START WITH REGISTER AL + 1 -> AL AL + 4 -> AL AL + 3 -> AL COPY TO AH

; AL + AH -> AL

2

CS203 HO#5

8-bit signed and unsigned subtraction ; SUBTRACTION MOV DEC SUB

SUB

MOV SUB

; ; ; ; ; ; AL, MEM8 ; ; ; AH, 4 ; AL, AH ; ; ; AL, 9 AL AL, 2

AL <- 9 AL – 1 ->AL AL – 2 ->AL

AL – 3 ->AL

AH <- 4 AL – AH ->AL

Example 1 TITLE "PROGRAM 1 EXPERIMENT 4" ; 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 INT SUB MOV

AH,01H 21H AL,30H CL,AL

;READ FIRST NUMBER

LEA MOV INT LEA MOV INT

DX,CRLF AH,09H 21H DX,PROMPT2 AH,09H 21H

;MOVE CURSOR TO NEXT LINE

MOV INT SUB ADD

AH,01H 21H AL,30H AL,CL

;READ SECOND NUMBER

MOV CL,AL ADD CL,30H

;Convert character to number ;SAVE THE NUMBER IN CL

;DISPLAY PROMPT2

;Convert character to number ;PERFORM ADDITION AND SAVE RESULT IN CL

;CONVERT DIGIT TO CHARACTER

Prepared by: Khurram Tanvir

3

CS203 HO#5

LEA MOV INT LEA MOV INT

DX,CRLF AH,09H 21H DX,PROMPT3 AH,09H 21H

MOV DL,CL MOV AH,02H INT 21H

;MOVE CURSOR TO NEXT LINE

;DISPLAY PROMPT3

;DISPLAY SUM

.EXIT END

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

Prepared by: Khurram Tanvir

4

CS203 HO#5

MOV AH,02H INT 21H .EXIT END

5.3 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,

Operation Multiplication

Opcode

Code Example

Meaning

DEC MUL

DEC VAR1 MUL CL MUL CX

[VAR1] ← [VAR1] – 1 AX ← AL * CL (DX,AX) ← AX* CX

Flags Affected OF

SF

ZF

AF

PF

CF

* *

* ?

* ?

* ?

* ?

*

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

5.4 Using Division Instructions: The DIV instruction divides unsigned numbers, and IDIV divides signed numbers. Both

Prepared by: Khurram Tanvir

5

CS203 HO#5

Operation Division

Flags Affected

Opcode

Code Example

Meaning

OF

SF

ZF

AF

PF

CF

DIV

DIV CX

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

?

?

?

?

?

?

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 mov mov div

unsigned by 8-bit ax, 700 bl, 36 bl

Prepared by: Khurram Tanvir

6

; ; ; ; ;

Load dividend 700 Load divisor DIV 36 Divide BL -----Quotient in AL 19 Remainder in AH 16

CS203 HO#5

Example Test Questions Fill in the blanks 1. DEC VAR1 means ___________________________________. 2. ___________statement decrements the register by 1. Specify True or False 1. ADD instruction is used for both signed and unsigned numbers (True/False) 2. IMUL instruction is used for both signed and unsigned numbers (True/False) 3. As a result of multiplication the higher 16 bits go to AX (True/False) Choose the best answer. As a result of DIV instruction, the remainder goes to a. b. c. d.

CX register AX register DX register BX register

AS a result of MUL instruction, the higher 16 bits are saved in a. b. c. d.

CX register AX register DX register BX register

Question: How division of unsigned numbers is carried out in 8086.

Prepared by: Khurram Tanvir

7

CS203 HO#5

Related Documents

5 Instructions
May 2020 1
Lecture 5
August 2019 29
Lecture 5
November 2019 23
Lecture 5
May 2020 16
Lecture 5
April 2020 19