Arithmetic
Arithmetic
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
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