Arithmetic Operations Multiplication Division System Programming Lab Computer

Arithmetic Operations Multiplication – Division System Programming Lab Computer Engineering Department College of Engineering

Objectives • Write programs in 8086 assembly language for multiplication and division by using different size of data as bytes or words. • Use arithmetic instructions to accomplish simple binary, BCD, and ASCII arithmetic.

Theory • • Another arithmetic instructions found in any microprocessor includes multiplication and division. Also shown are their uses in manipulating register and memory data. These operations used any addressing mode with 8 -bit or 16 bit. These arithmetic instructions can be divided into two types of instructions: Ø First Group : MUL, IMUL, TEST Ø Second group: DIV, IDIV, CBW, CWD

Theory • These types of operands are supported: Ø REG Ø Memory • REG: AX, BX, CX, DI, SI, BP, SP. • Memory: [BX], [BX+SI+7], variable, etc. . . • Multiplication is performed on bytes, words, or double words, and can be signed integer or unsigned.

Theory • After operation between operands, result is always stored in AL, AX. • The product after a multiplication is always a double-width product. • Some flag bits (overflow and carry) change when the multiply instruction executes CF, OF

Multiplication Ø MUL – unsigned multiply When operand is a byte When operand is a word Ø IMUL – signed multiply When operand is a byte When operand is a word AX = AL * operand (8 bit) (AX, DX) = AX * operand (8 or 16 bit)

Unsigned Multiply Instr. Operands Description Algorithm: when operand is a byte: AX = AL * operand. when operand is a word: MUL REG Memory (DX AX) = AX * operand. Example: MOV AL, 200 ; AL = 0 C 8 h MOV BL, 4 MUL BL ; AX = 0320 h (800) RET Flags: C, O

Signed Multiply Instr. Operands Description Algorithm: when operand is a byte: AX = AL * operand. when operand is a word: IMUL REG Memory (DX AX) = AX * operand. Example: MOV AL, -2 MOV BL, -4 IMUL BL ; AX = 8 RET Flags: C, O

TEST Instruction Instr. Operands Description Logical AND between all bits of two operands for flags only. TEST REG , Memory , REG 1 AND 1 = 1 0 AND 1 = 0 REG , REG 1 AND 0 = 0 0 AND 0 = 0 Memory , Immed. MOV AL, 00000101 b REG , Immed. TEST AL, 1 ; ZF = 0. TEST AL, 10 b ; ZF = 1. RET Flags: Z, S, P

Division Ø DIV – unsigned division When operand is a byte When operand is a word Ø IDIV – signed division When operand is a byte When operand is a word AL = AX / operand (8 bit) AH = remainder (modulus) AX = (AX, DX) / operand (8 or 16 bit) DX = remainder (modulus)

Unsigned Divide Instr. Operands Description Algorithm: when operand is a byte: AL = AX / operand AH = remainder (modulus) when operand is a word: DIV REG Memory AX = (DX AX) / operand DX = remainder (modulus) Example: MOV AL, 203 ; AX = 00 CBh MOV BL, 4 DIV BL ; AL = 50 (32 h), AH = 3 RET Flags: NON

Signed Divide Instr. Operands Description Algorithm: when operand is a byte: AL = AX / operand AH = remainder (modulus) when operand is a word: IDIV REG Memory AX = (DX AX) / operand DX = remainder (modulus) Example: MOV AX, -203 ; AX = 0 FF 35 h MOV BL, 4 IDIV BL ; AL = -50 (0 CEh), AH = -3 (0 FDh) RET Flags: NON

Convert Byte into Word Instr. Operands Description Algorithm: If high bit of AL = 1 then: AH = 255 (0 FFh) else AH = 0 CBW No operands Example: MOV AX, 0 MOV AL, -5 CBW RET Flags: NON ; AH = 0, AL = 0 ; AX = 000 FBh (251) ; AX = 0 FFFBh (-5)

Convert Word to Double word Instr. CWD Operands Description Algorithm: if high bit of AX = 1 then: DX = 65535 (0 FFFFh) else DX = 0 Example: No operands MOV DX, 0 ; DX = 0 MOV AX, 0 ; AX = 0 MOV AX, -5 ; DX AX = 00000 h: 0 FFFBh CWD ; DX AX = 0 FFFFh: 0 FFFBh RET Flags: NON

Examples Multiply two 8 -bit unsigned numbers the first in the register BL, and the second in CL, the result is stored in DX (assumed BL=05, CL=10) ORG 100 MOV BL, 05 H MOV CL, 10 MOV AL, CL MUL BL MOV DX, AX HLT

Examples Divide the unsigned byte contents of memory location NUMB by the contents of memory location NUMB 1. Store the quotient in location ANSQ and reminder in location ANSR (assume [NUMB]=15 and [NUMR]=4). ORG 100 HLT MOV AL, NUMB DB 15 H MOV AH, 00 H NUMB 1 DB 04 H DIV NUMB 1 ANSQ DB 0 H MOV ANSQ, AL ANSR DB 0 H MOV ANSR, AH

Examples Divide two 16 -bit signed numbers, 1 st number= -100 the 2 nd = +9, store the result in DX, AX. ORG 100 h After the division the results MOV AX, -100 appear in DX, AX MOV CX, 9 AX = -11 quotient CWD DX = -1 reminder IDIV CX HLT

Examples Write 8086 program that generate a byte size integer in the memory location define as RESULT, the value of integer is to be calculated from the following equation: (RESULT) = ( AL. NUM 1 ) + ( NUM 2. AL ) + BL MOV AL, 01 H MOV BX, 0001 H MOV DL, AL ADC AX, BX MOV CL, NUM 1 MOV RESULT, AX MUL CL ; ( AL. NUM 1 ) HLT MOV SI, AX NUM 1 DB 02 h MOV AL, DL NUM 2 DB 02 h MUL NUM 2 ADD AX, SI ; ( AL. NUM 2 ) RESULT DW ?

Examples Write the program to find the value of AX=((3*A+B ))/2 ; A = 2 H, B = 2 H A) By using successive addition. B) By using multiplication and division instruction. MOV AL, A ADD AL, AL ADC AL, A ADC AL, B SUB AL, 02 H HLT 10+10+10 =30 30 + 6 = 36 36 – 2 – 2 = 16 2+2+2 =6 6+2=8 8 – 2 = 4 MOV AL, A MOV BL, 03 H MUL BL ADD AL, B MOV BL, 02 H DIV BL HLT A DB 02 H B DB 02 H

Examples MOV SI, OFFSET X ADC DX, AX MOV DI, OFFSET Y INC BX MOV BX, 00 H LOOP L 1 MOV DX, 00 H MOV SUM, DX MOV CX, 0 BH HLT L 1: X DB 11 DUP (+2) MOV AL, [SI+BX] Y DB 11 DUP (+3) IMUL [DI+BX] SUM DW ? ADD AX, 0002 H
- Slides: 20