0% found this document useful (0 votes)
20 views

Arithmetic

Uploaded by

Aditya Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Arithmetic

Uploaded by

Aditya Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Arithmetic Instructions & Lab

Programs
Dr. REKHA.K.S.
Associate Professor,
Dept of CS&E,
NIE,MYSURU
Objectives
• Explain the Basic Programs and Basic
Arithmetic instructions used in the program.
• Explain the various ways of displaying the
messages in a program.
• Explain the execution of the program
• in 8086 you have a total of 1MB of addressable memory out
of which a single segment can be upto 64KB. ... If you do
not initialize any data segments and try to access any random
memory segment you will have a segmentation fault and your
program would crash with random values
• Mov AX,Data
• Mov DS,AX
• At first the meaning of the name data (i.e. an address of the
segment with a name data) is loaded by an
instruction mov into the ALU register AX and then it is
removed into the segment register DS. Such double-steps
operation is necessary due to the specificity of the
microprocessor architecture
it doesn’t able to place the necessary address directly from
the memory into the segment register, that is why we have to
use AX register as a “trans-shipping point”.
Arithmetic Instructions
• ADD instruction
• ADD DST,SRC
• REG,mem
• Mem,Reg
• Reg, Reg
• Memory,immediate
• Reg,immediate
Algorithm
• Operand1=operand1+operand2
• Ex: MOV AL,5 ; AL=5
• ADD Al,1 ;AL=6
• RET
• Data types
• DB define byte – 8 bits
• DW define word – 16 bits
• DD define double word-32 bits
• DQ define quad word – 64 bits
• DT define Tera word – 80 bits
Data Segment
X DB 04h
Y DB 02h
Result DB 00
Data Ends
Code Segment
Assume CS:code,DS:data
Begin: MOV AX,Data
MOV DS,AX
MOV AL,X
MOV BL,Y
ADD AL,BL
MOV Result,AL
MOV AH,4CH
INT 21H
end Begin Code ends
• data segment
• a dw 0202h
• b dw 0408h
• c dw ?
• data ends

• code segment
• assume cs:code,ds:data
• start:
• mov ax,data
• mov ds,ax
• mov ax,a
• mov bx,b
• add ax,bx
• mov c,ax
• Mov ah,4ch
• Int 21h
• code ends
• end start
SUBTRACT
• SUB DST,SRC
• (DST)<=(DST)-(SRC)
reg,memory
memory,reg
reg,reg
Memory, immediate
Reg,immediate
• Algorithm : Operand1= Operand1-operand2
• SUB AL,BL
• AL<- AL-BL
Data Segment
X DB 04h
Y DB 02h
Result DB 00
Data Ends
Code Segment
Assume CS:code,DS:data
Begin: MOV AX,Data
MOV DS,AX
MOV AL,X
MOV BL,Y
SUB AL,BL
MOV Result,AL
MOV AH,4CH
INT 21H
end Begin Code ends
• data segment
• a dw 9A88h
• b dw 8765h
• c dw ?
• data ends

• code segment
• assume cs:code,ds:data
• start:
• mov ax,data
• mov ds,ax
• mov ax,a
• mov bx,b
• sub ax,bx
• mov c,ax
• Mov ah,4ch
• Int 21h
• code ends
• end start
Binary Unsigned Multiplication
• MUL REG
Memory
When operand is a byte
AX=AL*operand
When operand is a word
(DXAX)= AX*operand
Ex: MOV AL,200; AL=0C8h
MOV BL,4
MUL BL ; AX=0320h(800)
RET
Write a program to multiply two numbers

Data Segment
X DB 02h
Y DB 02h
Result DB 00
Data Ends
Code Segment
Assume CS:code,DS:data
Begin: MOV AX,Data
MOV DS,AX
MOV AL,X
MOV BL,Y
MUL BL
MOV Result,AL
MOV AH,4CH
INT 21H
end Begin Code ends
Unsigned Divide
• DIV REG
Memory
Where operand is a byte
AL=AX/operand
AH=remainder
AL=Quotient

When operand is a word


AX = (DXAX)/operand
DX=remainder
AX=Quotient
MOV AX,203; AX=00CBH
MOV BL,4 ;AL=50->quotient
DIV BL ;AH=3->remainder
RET
Data Segment
X DB 04h
Y DB 02h
Result DB 00
Data Ends
Code Segment
Assume CS:code,DS:data
Begin: MOV AX,Data
MOV DS,AX
MOV AL,X
MOV BL,Y
DIV BL
MOV Result,AL
MOV AH,4CH
INT 21H
end Begin Code ends
• Program to find the average of two numbers
Data Segment
X dw 1122H
Y dw 2244H
Result dw ?
Data Ends
Code segment
Assume CS:code, DS:data
Begin: MOV AX,DATA
MOV DS,AX
MOV AX,X ; X & Y are DW
ADD AX,Y ;DXAX/BX
MOV BX,02H
DIV BX
MOV Result, AX
MOV AH,4CH
INT 21H
END BEGIN
CODE ENDS
• To display a character (printf statement)
MOV DL,AL
MOV AH,2
INT 21H

To display a message on the console


MOV DX,MSG
MOV AH,09H
INT 21H
• To Accept an input from the user (Scanf)
MOV AH,1
INT 10H ; will accept a character from user

with echo

MOV AH,8
INT 10H ; will accept a char from user
without echo
Load Effective Address
• LEA REG,memory
• This instruction copies the Effective Address of
the memory transferred to the specified
register.
• This instruction transfers only offset address
not the data
• LEA SI,ARR
• Write an ALP to find a key element in an array
using the Linear search algorithm

DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
MSG2 DB "NOT FOUND$"
KEY DB 33H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV AL, KEY
LEA SI, STRING1
MOV CX, 04H

UP: MOV BL,[SI]


CMP AL, BL
JZ FOUND
INC SI
DEC CX
JNZ UP
LEA DX, MSG2
MOV AH, 09H
INT 21H
JMP END1
FOUND: LEA DX, MSG1
MOV AH, 09H
INT 21H
END1: MOV AH,4ch
int 21h
CODE ENDS
END START
• The BCD form value will not show us the
Result But will print the Coresponding Ascii
Codes of the number, Hence By adding 30H to
BCD will Convert it to
ASCII code which will print the digit (number)
on screen.
• Write an ALP to find a key element in an array
using the Binary search algorithm
Binary Search Algorithm
• We basically ignore half of the elements just
after one comparison.
• Compare x with the middle element.
• If x matches with middle element, we return
the mid index.
• Else If x is greater than the mid element, then x
can only lie in right half subarray after the mid
element. So we check for right half.
• Else (x is smaller) check for the left half.
.MODEL SMALL
.DATA
A1 DW 010H,020H,030H,040H
LEN DW (LEN-A1)/2
KEY DW 30H
M1 DB 'KEY FOUND AT '
RES DB ?
M3 DB ' POSITION $'
M2 DB 'KEY NOT FOUND $'
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,01
MOV DX,LEN
MOV CX,KEY
L2:CMP BX,DX
JA FAIL
MOV AX,BX
ADD AX,DX
SHR AX,01
MOV SI,AX
DEC SI
ADD SI,SI
CMP CX,A1[SI]
JAE L1
MOV DX,AX
DEC DX
JMP L2
L1: JE SUCCESS
MOV BX,AX
INC BX
JMP L2
FAIL:LEA DX,M2
JMP L3
SUCCESS: ADD AX,30H
MOV RES,AL
LEA DX,M1
L3: MOV AH,09H
INT 21H
MOV AH,04CH
INT 21H
END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy