C-1 Record PDF
C-1 Record PDF
Year-II Semester-IV
(Code- 16CSC07)
Submitted to Submitted by
Year-II Semester-4
CERTIFICATE
This is to certify that this is an organized record of the work done by Mr./Miss.
________________bearing Roll No._______________ of B.E –II/IV, CSE, III
semester in the Data Structures lab during the academic year 2019-20.
North Bridge
Northbridge or host bridge is one of the two chips in the core logic chipset architecture on a PC
motherboard, the other being the south bridge. The Northbridge, also known as Memory
Controller Hub, is usually paired with a Southbridge.
South Bridge
The Southbridge is one of the two chips in the core logic chipset on a personal
computer motherboard, the other being the Northbridge. The Southbridge typically
implements the slower capabilities of the motherboard in a Northbridge/Southbridge chipset
computer architecture.
Memory
In computing, memory refers to a device that is used to store information for immediate use in
a computer or related computer hardware device.
It typically refers to semiconductor memory.
Cache Memory is a special very high-speed memory. It is used to speed up and synchronizing
with high-speed CPU. Cache memory is costlier than main memory or disk memory but
economical than CPU registers. Cache memory is an extremely fast memory type that acts as a
buffer between RAM and the CPU. It holds frequently requested data and instructions so that
they are immediately available to the CPU when needed.
______________________________________________________________________________
2.AIM: To Convert Signed decimal number to Signed binary number using 2’s complement.
DESCRIPTION: Two's complement is a mathematical operation on binary numbers, and is an
example of a radix complement. It is used in computing as a method of signed number
representation. The two's complement of a number is to take its ones' complement and add
one: the sum of a number and its ones' complement is all '1' bits, or 2N − 1; and by defini on,
the sum of a number and its two's complement is 2N.
Two's complement is the way every computer I chooses to represent integers. To get the two's
complement negative notation of an integer, you write out the number in binary. You then
invert the digits, and add one to the result.
Suppose we're working with 8 bit quantities (for simplicity's sake) and suppose we want to find
how -28 would be expressed in two's complement notation. First we write out 28 in binary
form.
00011100
Then we invert the digits. 0 becomes 1, 1 becomes 0.
11100011
Then we add 1.
11100100
PROGRAM:
def comp(str):
n = len(str)
i=n-1
while(i >= 0):
if (str[i] == '1'):
break
i=i-1
if (i==-1):
return '1'+str
k=i-1
while(k >= 0):
if (str[k] == '1'):
str = list(str)
str[k] = '0'
str = ''.join(str)
else:
str = list(str)
str[k] = '1'
str = ''.join(str)
k -= 1
return str
def dec_bin(x):
if(x==0):
return ''
else:
return dec_bin(x//2)+str(x%2)
while(1):
c=int(input("enter \n 1. decimal to 2's complement enter \n 2. 2's complement to decimal \n
3. quit "))
if(c!=1 and c!=2):
break;
elif(c==1):
l=int(input("enter number of bits"))
a=int(input("enter decimal number"))
if(a>=0):
ans=dec_bin(a).rjust(l,'0')
print("2's complement is:",ans)
if(a<0):
t=dec_bin(-a).rjust(l,'0')
k=comp(t)
print("2's complement is:",k)
elif(c==2):
l=int(input("enter number of bits"))
a=input("enter 2's complement")
r=a.rjust(l,'0')
ans=comp(r)
#k=len(a)
print(r[0],ans)
if(r[0]==1):
ans=0-int(ans,2)
print("minus")
else:
ans=int(ans,2)
print("decimal value is",ans)
OUTPUT:
______________________________________________________________________________
3.AIM: To implement any to any number system convertor
DESCRIPTION:
Decimal to Binary:
Decimal numbers can be converted to binary by repeated division of the number by 2
The remainders are to be read from bottom to top to obtain the binary equivalent.
Decimal to Octal:
Decimal numbers can be converted to octal by repeated division of the number by 8 while
recording the remainder read from bottom to top.
Decimal to Hexadecimal:
Decimal numbers can be converted to octal by repeated division of the number by 16 while
recording the remainder read from bottom to top.
Binary to Decimal:
The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2**n)
Octal to Decimal:
Regular decimal number is the sum of the digits multiplied with 10**n.
Octal numbers are read the same way, but each digit counts 8**n instead of 10**n.
Multiply each digit of the octal number with its corresponding 8**n.
Hexadecimal to Decimal:
Hex is a base 16 number and decimal is a base 10 number. We need to know the decimal
equivalent of every hex number digit. Get the decimal equivalent of hex .Multiply every digit
with 16 power of digit location.Sum all the multipliers.
PROGRAM:
def bin_dec(x):
i=0
decimal=0
while(x != 0):
dec = x % 10
decimal = decimal + dec * pow(2, i)
x= x//10
i += 1
return decimal;
def oct_dec(x):
i=0
decimal=0
while(x != 0):
dec = x % 10
decimal = decimal + dec * pow(8, i)
x= x//10
i += 1
return decimal;
def hex_dec(x):
i=len(x)-1
decimal=0
for t in x:
if((t>='A' and t<='F')):
dec=ord(t)-ord('A')+10
elif((t>='a' and t<='f')):
dec=ord(t)-ord('a')+10
else:
dec=ord(t)-ord('0')
decimal = decimal + dec * pow(16, i)
i -= 1
return decimal;
def dec_bin(x):
if x==0: return ''
else:
return dec_bin(x//2) + str(x%2)
def dec_oct(x):
if x==0: return ''
else:
return dec_oct(x//8) + str(x%8)
def dec_hex(x):
if x==0: return ''
else:
if(x%16<10):
z=str(x%16)
else:
z=chr((x%16)-10 +ord('A'))
return dec_hex(x//16) + z
while(1):
aaa=int(input("enter 1. conversion 2.exit"))
if(aaa==1):
a=input("enter input number system: ")
b=input("enter the output number system: ")
if(a=="binary" and (b == "decimal")):
x=eval(input("enter number:"))
decimal=bin_dec(x)
print(decimal)
elif(a=='decimal' and b=='binary'):
x=eval(input("enter number:"))
binary=dec_bin(x)
print(binary)
elif(a=='decimal' and b=='octal'):
x=eval(input("enter number:"))
octal=dec_oct(x)
print(octal)
elif(a=='decimal' and b=='hex'):
x=eval(input("enter number:"))
hexa=dec_hex(x)
print(hexa)
elif(a=='octal' and b=='decimal'):
x=eval(input("enter number :"))
decimal=oct_dec(x)
print(decimal)
elif(a=='hex' and b=='decimal'):
x=input("enter number :")
decimal=hex_dec(x)
print(decimal)
elif(a=='binary' and b=='octal'):
x=eval(input("enter number :"))
y=bin_dec(x)
ans=dec_oct(y)
print(ans)
elif(a=='binary' and b=='hex'):
x=eval(input("enter number :"))
y=bin_dec(x)
ans=dec_hex(y)
print(ans)
elif(a=='octal' and b=='binary'):
x=eval(input("enter number :"))
y=oct_dec(x)
ans=dec_bin(y)
print(ans)
elif(a=='octal' and b=='hex'):
x=eval(input("enter number:"))
y=oct_dec(x)
ans=dec_hex(y)
print(ans)
elif(a=='hex' and b=='binary'):
x=input("enter number:")
y=hex_dec(x)
ans=dec_bin(y)
print(ans)
elif(a=='hex' and b=='octal'):
x=input("enter number:")
y=hex_dec(x)
ans=dec_oct(y)
print(ans)
else:
break
OUTPUT:
______________________________________________________________________________
4.AIM: To implement Binary addition
DESCRIPTION:
A basic Binary Adder circuit can be made from standard AND and Ex-OR gates allowing us to
“add” together two single bit binary numbers, A and B.The addition of these two digits
produces an output called the SUM of the addition and a second output called the CARRY or
Carry-out.The addition of the binary number system is similar to that of the decimal number
system. The only difference is that the decimal number system consists the digit from 0-9 and
their base is 10 whereas the binary number system consists only two digits (0 and 1) which
make their operation easier. There are four basic operations for binary addition, as mentioned
below:
0+0=0;0+1=1;1+0=1 and 1+1=10.
The idea is to start from last characters of two strings and compute digit sum one by one. If sum
becomes more than 1, then store carry for next digits.
PROGRAM:
def dec_bin(x):
if(x==0):
return ''
else:
return dec_bin(x//2)+str(x%2)
l=int(input("enter number of bits:"))
a=int(input("enter the number in decimal:"))
b=int(input("enter another number in decimal:"))
x=dec_bin(a)
y=dec_bin(b)
m=x.rjust(l,'0')
n=y.rjust(l,'0')
c=0
ans=''
for i in range(l-1,-1,-1):
r=c
if(m[i]=='1'):
r+=1
if(n[i]=='1'):
r+=1
if(r%2==1):
ans='1'+ans
else:
ans='0'+ans
if(r<2):
c=0
else:
c=1
if(c!=0):
ans='1'+ans
print(ans)
OUTPUT:
if (str[k] == '1'):
str = list(str)
str[k] = '0'
str = ''.join(str)
else:
str = list(str)
str[k] = '1'
str = ''.join(str)
k -= 1
return str
def bin_add(m,n):
c=0
ans=''
for i in range(3,-1,-1):
r=c
if(m[i]=='1'):
r+=1
if(n[i]=='1'):
r+=1
if(r%2==1):
ans='1'+ans
else:
ans='0'+ans
if(r<2):
c=0
else:
c=1
return ans;
def right_shift(ans):
ans=ans[:7]
ans=ans[0]+ans
return ans;
def dec_bin(x):
if(x==0):
return ''
else:
return dec_bin(x//2)+str(x%2)
m=int(input("enter multiplicand"))
q=int(input("enter multiplier"))
if(m>=0):
m=dec_bin(m).rjust(4,'0')
elif(m<0):
m=dec_bin(-m).rjust(4,'0')
m=comp(m)
if(q>=0):
q=dec_bin(q).rjust(4,'0')
elif(q<0):
q=dec_bin(-q).rjust(4,'0')
q=comp(q)
q=q.rjust(4,'0')
m=m.rjust(4,'0')
a='0000'
res=a+q
x='0'
for i in range(0,4):
y=q[-1]
if(x==y):
x=res[-1]
res=right_shift(res)
a=res[0:4]
q=res[4:8]
elif(x=='0' and y=='1'):
x=res[-1]
sub=comp(m)
a=bin_add(a,sub)
res=a+q
res=right_shift(res)
a=res[0:4]
q=res[4:8]
elif(x=='1' and y=='0'):
x=res[-1]
a=bin_add(a,m)
res=a+q
res=right_shift(res)
a=res[0:4]
q=res[4:8]
print(a,q)
if(a=='1111'):
print("-",end='')
print(int(comp(q),2))
else:
print(int(q,2))
OUTPUT:
_____________________________________________________________________________
6.AIM : To perform restoration algorithm.
DESCRIPTION : A division algorithm provides a quotient and a remainder when we divide two
number. ... Here, n-bit dividend is loaded in Q and divisor is loaded in M. Value of Register is
initially kept 0 and this is the register whose value is restored during iteration due to which it is
named Restoring.
Steps to perform restoration :
● Step-1: First the registers are initialized with corresponding values (Q = Dividend, M =
Divisor, A = 0, n = number of bits in dividend)
● Step-2: Then the content of register A and Q is shifted right as if they are a single unit
● Step-3: Then content of register M is subtracted from A and result is stored in A
● Step-4: Then the most significant bit of the A is checked if it is 0 the least significant bit
of Q is set to 1 otherwise if it is 1 the least significant bit of Q is set to 0 and value of
register A is restored i.e the value of A before the subtraction with M
● Step-5: The value of counter n is decremented
● Step-6: If the value of n becomes zero we get of the loop otherwise we repeat from 2
● Step-7: Finally, the register Q contain the quotient and A contain remainder
Below is the flowchart for the restoration algorithm:
PROGRAM :
''' left shift'''
def left_shift(ans):
z=ans[1:]
z=z+'0'
return z;
#2's complement
def comp(str):
n = len(str)
i = n-1
while(i >= 0):
if (str[i] == '1'):
break
i=i-1
k = i-1
while(k >= 0):
if (str[k] == '1'):
str = list(str)
str[k] = '0'
str = ''.join(str)
else:
str = list(str)
str[k] = '1'
str = ''.join(str)
k -= 1
return str
'''binary addition'''
def bin_add(m,n):
c=0
ans=''
for i in range(3,-1,-1):
r=c
if(m[i]=='1'):
r+=1
if(n[i]=='1'):
r+=1
if(r%2==1):
ans='1'+ans
else:
ans='0'+ans
if(r<2):
c=0
else:
c=1
return ans;
q1=int(input("enter dividend:"))
m1=int(input("enter divisor:"))
q=dec_bin(abs(q1)).rjust(4,'0')
m=dec_bin(abs(m1)).rjust(4,'0')
a='0000'
res=a+q
m_c=comp(m)
for i in range(0,4):
res=left_shift(res)
a=bin_add(res[0:4],m_c)
res=a+res[4:8]
if(res[0]=='1'):
a=bin_add(res[0:4],m)
res=a+res[4:8]
else:
res=a+res[4:7]+'1'
print(res)
if(q1<0 and m1>0):
print("quotient",0-int(res[4:8],2))
print("remainder",0-int(res[0:4],2))
elif(q1>=0 and m1<0):
print("quotient",0-int(res[4:8],2))
print("remainder",int(res[0:4],2))
elif(q1<0 and m1<0):
print("quotient",int(res[4:8],2))
print("remainder",0-int(res[0:4],2))
else:
print("quotient",int(res[4:8],2))
print("remainder",int(res[0:4],2))
OUTPUT:
……………………………………………………………………………………………………………………………………………….
_____________________________________________________________________________
#2's complement
def comp(str):
n = len(str)
i = n-1
while(i >= 0):
if (str[i] == '1'):
break
i=i-1
k = i-1
while(k >= 0):
if (str[k] == '1'):
str = list(str)
str[k] = '0'
str = ''.join(str)
else:
str = list(str)
str[k] = '1'
str = ''.join(str)
k -= 1
return str
'''binary addition'''
def bin_add(m,n):
c=0
ans=''
for i in range(3,-1,-1):
r=c
if(m[i]=='1'):
r+=1
if(n[i]=='1'):
r+=1
if(r%2==1):
ans='1'+ans
else:
ans='0'+ans
if(r<2):
c=0
else:
c=1
return ans;
q1=int(input("enter dividend:"))
m1=int(input("enter divisor:"))
q=dec_bin(abs(q1)).rjust(4,'0')
m=dec_bin(abs(m1)).rjust(4,'0')
a='0000'
res=a+q
f=0
m_c=comp(m)
for i in range(0,4):
res=left_shift(res)
if(f==0):
a=bin_add(res[0:4],m_c)
else:
a=bin_add(res[0:4],m)
f=0
res=a+res[4:8]
if(res[0]=='1'):
f=1
else:
res=a+res[4:7]+'1'
print(res)
if(q1<0 and m1>0):
print("quotient",0-int(res[4:8],2))
print("remainder",0-int(res[0:4],2))
elif(q1>=0 and m1<0):
print("quotient",0-int(res[4:8],2))
print("remainder",int(res[0:4],2))
elif(q1<0 and m1<0):
print("quotient",int(res[4:8],2))
print("remainder",0-int(res[0:4],2))
else:
print("quotient",int(res[4:8],2))
OUTPUT:
_____________________________________________________________________________
8.AIM:To demonstrate addressing modes using 8086 emulator
DESCRIPTION: The way of specifying data to be operated by an instruction is known as
addressing modes. This specifies that the given data is an immediate data or an address. It also
specifies whether the given operand is register or register pair.
Types of addressing modes:
1.Register mode – In this type of addressing mode both the operands are registers.
2.Immediate mode – In this type of addressing mode the source operand is a 8 bit or 16 bit
data. Destination operand can never be immediate data.
3.Displacement or direct mode – In this type of addressing mode the effective address is
directly given in the instruction as displacement.
4. Register indirect mode – In this addressing mode the effective address is in SI, DI or BX
5. Based indexed mode – In this the effective address is sum of base register and index
registerBase register: BX, BPIndex register: SI, DI The physical memory address is calculated
according to the base register.
6. Indexed mode – In this type of addressing mode the effective address is sum of index
register and displacement.
7. Based mode – In this the effective address is the sum of base register and displacement
8. Based indexed displacement mode – In this type of addressing mode the effective address is
the sum of index register, base register and displacement.
PROGRAM:
data segment
str db "hello$”
str2 db "corld$"
a db "camp$"
b db "cbit$"
data ends
code segment
assume cs:code ds:data es:extra
START:
MOV AX, data ;1
MOV DS, AX ;2 initializing segment register
MOV ES,AX ;3 initializing segment register
MOV AX, 2000H ;4 immediate mode
MOV CL, 0AH ;5 immediate mode
LEA SI,str2 ;6
LEA DI, b ;7
MOV BX,AX ;8 register mode
MOV Al, [0006h] ;9 direct mode
MOV Ch, [DI] ;10 register indirect mode
MOV BL, [SI] ;11 register indirect mode
MOV DH, [BX] ;12 register indirect mode
MOV AH, [BP+SI] ;13 based indexed mode
MOV AL, [BX+DI] ;14 based indexed mode
MOV BH, [SI+0002h] ; 15 indexed mode
MOV BL, [DI+0003h] ;16 indexed mode
MOV CH, [BP+ 0100] ;17 based mode
MOV CL, [SI+BP+2000] ;18 based indexed displacement mod
end start
code ends
OUTPUT:
After executing 1 TO 8 after executing 9 to 12 after executing 13 to 18
_____________________________________________________________________________
9.AIM: Write a program to execute addition of two 16-bit numbers using 8-bit registers.
DESCRIPTION:ADC is addition with carry. The ADC (Add with Carry) instruction adds the values
in destination and source , together with the carry flag. You can use ADC to synthesize
multiword arithmetic. In certain circumstances, the assembler can substitute one instruction
for another.ADD doen’t consider the carry flag
Syntax: adc dest, src ;dest: memory or register ; src: memory, register, or immediate Action:
dest = dest + src + CF
Two 16 bit numbers can be added using four 8 bit registers with the help of ADC.
The lower nibs of the two numbers are added using ADD instruction.This addition may produce
a carry which will be added to the sum of the higher nibs with the help of ADC instruction
PROGRAM:
code segment
assume cs:code
start:
mov al,0ffh
mov ah,12h
mov bl,01h
mov bh,00h
add al,bl
adc ah,bh
end start
code ends
OUTPUT:
10.AIM:Write a program to execute the addition of two 32-bit numbers using 16-bit register.
DESCRIPTION: ADC is addition with carry. The ADC (Add with Carry) instruction adds the values
in destination and source , together with the carry flag. You can use ADC to synthesize
multiword arithmetic. In certain circumstances, the assembler can substitute one instruction
for another.ADD doen’t consider the carry flag
Syntax: adc dest, src ;dest: memory or register ; src: memory, register, or immediate Action:
dest = dest + src + CF
Two 16 bit numbers can be added using four 8 bit registers with the help of ADC.
The lower nibs of the two numbers are added using ADD instruction.This addition may produce
a carry which will be added to the sum of the higher nibs with the help of ADC instruction
Since there are no 32 bit registers in 8086 ,we store a 32 bit number in two 16 bit registers.
In this case ,the first number is stored in the registers BX,AX(higher nib in BX,lower nib in
AX).The second number is stored in the registers DX,CX(higher nib ijn DX ,lower nib in CX).The
resulting sum is stored in BX,AX.
PROGRAM:
code segment
assume cs: code
start:
mov ax,0ffffh
mov bx,1234h
mov cx,0001h
mov dx,0000h
add ax,cx
adc bx,dx
end start
code ends
OUTPUT:
11. Demonstrate mul, imul, div, idiv
AIM: To demonstrate operations of mul, imul, div, and idiv.
DESCRIPTION:
Multiplication:
There are two instructions for multiplying binary data. The MUL (Multiply) instruction
handles unsigned data and the IMUL (Integer Multiply) handles signed data. Both
instructions affect the Carry and Overflow flag.
The syntax for the MUL/IMUL instructions is as follows −
MUL/IMUL multiplier
Multiplicand in both cases will be in an accumulator, depending upon the size of the
multiplicand and the multiplier and the generated product is also stored in two
registers depending upon the size of the operands.
MUL − Used to multiply unsigned byte by byte/word by word.
IMUL − Used to multiply signed byte by byte/word by word.
Division:
The division operation generates two elements - a quotient and a remainder. In
case of multiplication, overflow does not occur because double-length registers are
used to keep the product. However, in case of division, overflow may occur. The
processor generates an interrupt if overflow occurs.
The DIV (Divide) instruction is used for unsigned data and the IDIV (Integer Divide)
is used for signed data.
The format for the DIV/IDIV instruction −
DIV/IDIV divisor
The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit or
32-bit operands. The operation affects all six status flags.
DIV − Used to divide the unsigned word by byte or unsigned double word by word.
IDIV − Used to divide the signed word by byte or signed double word by word.
PROGRAM:
mul and imul:
code segment
assume cs: code
start:
MOV AL, 10
MOV DL, 25
MUL DL
MOV DL, 0FFH ; DL= -1
MOV AL, 0BEH ; AL = -66
IMUL DL
code ends
end start
code ends
end start
code segment
assume cs: code
start:
code ends
end start
OUTPUT:
12. Packed BCD arithmetic [daa, das]
AIM: To demonstrate arithmetic operations in Packed BCD.
DESCRIPTION:
In packed BCD representation, each digit is stored using four bits. Two decimal digits are packed
into a byte. For example, the number 1234 is stored as −
12 | 34H
There are two instructions for processing these numbers −
DAA − Decimal Adjust A er Addi on
DAS − decimal Adjust A er Subtrac on
There is no support for multiplication and division in packed BCD representation.
DAA − Used to adjust the decimal after the addition/subtraction operation.
DAS − Used to adjust decimal after subtraction.
PROGRAM:
DAA:
code segment
assume cs: code
start:
code ends
end start
DAS:
code segment
assume cs: code
start:
code ends
end start
OUTPUT:
DAA:
DAS:
13. Un-packed BCD arithmetic [aaa,aas,aam,aad]
AIM: To demonstrate the arithmetic operations in Un-packed BCD.
DESCRIPTION:
In unpacked BCD representation, each byte stores the binary equivalent of a decimal digit. For
example, the number 1234 is stored as −
01 02 03 04H
The four ASCII adjust instructions, AAA, AAS, AAM, and AAD, can also be used with unpacked
BCD representation.
AAA − Used to adjust ASCII a er addi on.
AAS − Used to adjust ASCII codes a er subtrac on.
AAM − Used to adjust ASCII codes a er mul plica on.
AAD − Used to adjust ASCII codes a er division.
PROGRAM:
AAA:
code segment
assume cs: code
start:
code ends
end start
AAS:
code segment
assume cs: code
start:
code ends
end start
AAM:
code segment
assume cs: code
start:
MOV AL, 5
MOV BH, 9
MUL BH
AAM
code ends
end start
AAD:
code segment
assume cs: code
start:
code ends
end start
OUTPUT:
AAA:
AAS:
AAM:
AAD:
14. Inputting and outputting a char, string using int21h
AIM: To demonstrate the inputting and outputting a char input/string using INT21H.
DESCRIPTION: INT 21h is the assembly language mnemonic for the Intel CPU command used to
perform a System call .It is a software interrupt which causes the function pointed by the 34th
vector in the interrupt table to be executed which is typically an MS-DOS API call.It is used for
text interface,IO,exiting and more.
INT 21h / AH=1 - read character from standard input, with echo, result is stored in AL. if there is
no character in the keyboard buffer, the function waits until any key is pressed. example: basic
8086 and dos interrupts that are currently supported by the emulator
INT 21h / AH=2 - write character to standard output. entry: DL = character to write, after
execution AL = DL.
INT 21h/ AH=0Ah - input of a string to DS:DX, fist byte is buffer size, second byte is number of
chars actually read. this function does not add '$' in the end of string. to print using INT 21h /
AH=9 you must set dollar character at the end of it
INT 21h / AH=9 - output of a string at DS:DX. String must be terminated by '$'.
PROGRAM:
Code:
code segment
assume cs:code,ds:data
start:
mov ah,01h ;to input
int 21h
mov dl,’b’
mov ah,02h ;to output
int 21h
code ends
end start
Output:
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
OUTPUT:
Input
Output:
15. Demonstrate shift and rotate [ with and without carry] .
Description : As we know that any machine (system) works on machine language, which consists
of binary numbers. In the 8086 microprocessor, we have 16-bit registers to handle our data.
Sometimes, the need to perform some necessary shift and rotate operations on our data may
occur according to the given condition and requirement. So, for that purpose, we have various
Shift and Rotate instructions present in the 8086 microprocessor. Those instructions are:
Program :
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
mov dl, 5
shl dl, 1
mov al,36h
sal al,1
clc
mov dh, 88h
rcl dh, 1
stc
mov ah, 10h
rcr ah, 1
CODE ENDS
END START
Output
16. Demonstrate logical operators , compare and negate
AIM: To demonstrate logical operators, compare and negate.
Description:
Logical instructions are the instructions which perform basic logical operations such as AND, OR,
etc. In 8086 microprocessor, the destination operand need not be the accumulator.
OR D, S D = D OR S OR AX, BX
Program :
DATA SEGMENT
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
Mov AL, 02H
AND AL,03H
Mov BL, 02H
OR BL,03H
Mov CL, 02H
NOT CL
Mov DL, 02H
XOR DL,02H
CODE ENDS
END START
Output:
17. Demonstrate stack operations and flag register instructions PUSH, POP, PUSHF,
POPF, SAHF and LAHF.
AIM: To demonstrate stack operations and flag register instructions PUSH, POP, PUSHF, POPF,
SAHF, LAHF using ASSEMBLY LANGUAGE.
Description:
STACK OPERATIONS:
LAHF − Used to load AH with the low byte of the flag register.
SAHF − Used to store AH register to low byte of the flag register.
PUSHF − Used to copy the flag register at the top of the stack.
POPF − Used to copy a word at the top of the stack to the flag register.
Program :
2) After push ax
3) After push 1 and push 2
4) After pushf
5) After pop
6) After LAHF (flag lower byte in ah)
7) After SAHF
18. Determine maximum of three numbers using int21h
AIM : To write 8086 program(Assembly Language) which determines the largest of
three numbers.
Program :
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AH,1
int 21h
mov bl,al ;Takes 1st input
int 21h
mov bh,al ;Takes 2nd input
int 21h
mov cl,al ;Takes 3rd input
c:
mov ah,2
mov dl,bh ;Largest element i.e here bh is moved to dl
int 21h ;ouput is shown
jmp exit
a:
cmp bl,cl
jge d
mov ah,2
mov dl,cl ;Largest element i.e here cl is moved to dl
int 21h ;ouput is shown
jmp exit
d:
mov ah,2
mov dl,bl ;Largest element i.e here bl is moved to dl
int 21h ;ouput is shown
jmp exit
exit:
mov ah,4ch
int 21h ;exits
ENDS
END START
Output :
Program :
data segment
a dw -5
p dw 'positive$'
n dw 'negative$'
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov bx,a
rcl bx,1
jc negative ;checks for carry flag
mov ah,09h
lea dx,p
int 21h
jmp down
negative: ;if cf=1
mov ah,09h
lea dx,n
int 21h
down:
rcr bx,1
code ends
end start
Output :
20. Determine whether given number is even or odd using int21h
AIM: To write 8086 program (Assembly Language) which determines whether a number is even or
odd.
DESCRIPTION : Using rotate we can determine whether a number is even or odd.A number is said
to be even if and only if the the carry flag is 0 after performing the rotate right with carry(RCR)
operation.It is said to be odd if the carry flag is 1.
PROGRAM :
data segment
a dw 5
e dw 'even$'
o dw 'odd$'
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov bx,a
rcr bx,1
jc odd ;checks for carry flag
mov ah,09h
lea dx,e
int 21h
jmp down
odd: ;if cf=1
mov ah,09h
lea dx,o
int 21h
down:
rcl bx,1
code ends
end start
OUTPUT :
21.AIM: To Write a program for setting and unsetting Trap Flag bit in Flag Register.
DESCRIPTION:
This flag is used for on-chip debugging. If trap flag is set (1), the CPU automatically generates an
internal interrupt after each instruction, allowing a program to be inspected as it executes
instruction by instruction.If trap flag is reset (0), no function is performed.We can change trap
flag bit in flag register by pushf operation by which whole flag register Is pushed into stack and
then pop it out in any register and change the bit of trap flag by ‘OR’ or ‘AND’ operation on
register. Then by popf operation we put it back in flag register,Thus we can set or unset the trap
flag bit in flag register.
data segment
msg1 db "Trap Flag is set$"
msg2 db "Trap flag is unset$"
data ends
code segment
assume cs:code, ds:data
start:
mov bx,data
mov ds,bx
pushf
pop bx
or bh,01H
rcr bh,01
jc down1:
mov ah,09h
mov dx, offset msg2
int 21h
jmp down2
down1:
mov ah,09h
mov dx, offset msg1
int 21h
down2:
rcl bh,01
pop ax
popf
code ends
end start
OUTPUT:
data segment
msg1 db "Trap Flag is set$"
msg2 db "Trap flag is unset$"
data ends
code segment
assume cs:code, ds:data
start:
mov bx,data
mov ds,bx
pushf
pop bx
and bh,00H
rcr bh,01
jc down1:
mov ah,09h
mov dx, offset msg2
int 21h
jmp down2
down1:
mov ah,09h
mov dx, offset msg1
int 21h
down2:
rcl bh,01
pop ax
popf
code ends
end start
OUTPUT:
_____________________________________________________________________________
DESCRIPTION: The process of executing the instructions repeatedly for more than ones is
called looping .There are many kinds of loops in different languages such as C,Python etc.While
lopp,For loop,do while loop etc.
Thus in 8086 also we have loops for executing instructions repeatedly. We have jmp
instruction. JMP instruction can be used for implementing loops .
We have a separate LOOP instruction which takes CX as its default register to decrement or
increment.
PROGRAM:
data segment
msg db "Hello$"
data ends
code segment
assume cs:code ds:data
start:
mov bx,data
mov ds,bx
mov cl,10
l1:
mov ah,09h
mov dx,offset msg
int 21h
dec cl
jnz l1
code ends
end start
OUTPUT:
PROGRAM(using loop):
data segment
msg db "Hello$"
data ends
code segment
assume cs:code ds:data
start:
mov bx,data
mov ds,bx
mov cx,10
l1:
mov ah,09h
mov dx,offset msg
int 21h
loop l1
code ends
end start
OUTPUT:
23.AIM : Write a program to demonstrate string instructions [ all five instructions 8,16 bit ] .
DESCRIPTION:
follows:
● DI and SI are incremented if
DF=0.
● DI and SI are decremented if
DF=1.
● DI is decremented if DF=1
Load LODS / LODSB Loads the AL, AX or EAX registers with LODSB
/ LODSW the content of the memory byte, word AL <- DS:[SI] ;
or double word pointed to by SI relative SI <- SI ± 1
to DS. After the transfer is made, the SI LODSW
register is automatically updated as AL <- DS:[SI] ;
follows: AH <- DS:[SI];
● SI is incremented if DF=0.
SI <- SI ± 2
● SI is decremented if DF=1.
MOVSB Program code :
data segment
a db 'Welcome$'
b db 8 dup('$')
data ends
code segment
assume cs:code, ds:data, es:extra
start:
mov ax,data
mov ds,ax
mov es,ax
lea si,a
lea di,b
mov cx,08h
rep
movsb
mov ah,09h
lea dx,b
int 21h
code ends
end start
Output :
MOVSW Program Code:
data segment
a db 'Welcome$'
b db 8 dup('$')
data ends
code segment
assume cs:code, ds:data, es:extra
start:
mov ax,data
mov ds,ax
mov es,ax
lea si,a
lea di,b
mov cx,4h
rep
movsw
mov ah,09h
lea dx,b
int 21h
code ends
end start
OUTPUT:
code segment
assume cs:code
start:
MOV AX, @DATA ; initialize DS
MOV DS, AX
LEA DX, PROMPT ; load and print the string PROMPT
MOV AH, 9
INT 21H
LEA SI, STRING ; set SI=offset address of variable STRING
MOV CX, 6 ; set CX=6
CLD ; clear direction flag
MOV AH, 2 ; set output function
@OUTPUT_LOOP: ; loop label
LODSB ; set AL=DS:[SI]
MOV DL, AL ; set DL=AL
INT 21H ; print a character
LOOP @OUTPUT_LOOP ; jump to label @OUTPUT_LOOP while CX!=0
MOV AH, 4CH ; return control to DOS
INT 21H
END start
code ends
Output :
data segment
STRING DB "Hello!$"
string2 db 6 dup('$')
data ends
code segment
assume cs:code es:extra ds:data
start:
MOV AX, data
MOV DS, AX
MOV ES,AX
LOOP1:
LODSB
STOSB
LOOP LOOP1
LEA DX,string2
MOV AH,09H
INT 21H
code ends
END start
Output :
STOSW Program Code :
data segment
STRING DB "Hello!$"
string2 db 6 dup('$')
data ends
code segment
assume cs:code es:extra ds:data
Start:
MOV AX, data
MOV DS, AX
MOV ES,AX
Output :
Output :
24.AIM: To Write an assembly language program to transfer a block of bytes from one memory
location to another memory location
DESCRIPTION: In this program ,the value of counter tells the number of bytes to be transferred
The 8-bit data which have to be transfer is stored in continuous memory location starting from
programmer specified address .The data is transferred to a continuous memory location
starting from address specified by programmer.
CLD instruction is used to clear the directional flag, i.e., DF=0.
REP instruction is used to repeat the step until value of CX is not equal to zero and value of CX is
decremented by one at every step, i.e.,
CX=CX-1
MOVSB instruction is used to transfer bytes only from source memory location (MADS) to
destination memory location (MAES).
MADS-->MAES
where MADS=DS*10+SI
MAES=ES*10+DI
Here, value of SI and DI is updated automatically.
PROGRAM:
data segment
a db 'HELLOWORLD$'
b db 8 dup('$')
data ends
code segment
assume cs:code, ds:data, es:extra
start:
mov ax,data
mov ds,ax
mov es,ax
lea si,a
lea di,b
mov cx,08h
rep
movsb
mov ah,09h
lea dx,b
int 21h
code ends
end start
OUTPUT:
______________________________________________________________________________
25.AIM: Write a program for string copy using string instructions.
DESCRIPTION:
String instructions were designed to operate on large data structures.The SI and DI
registers are used as pointers to the data structures being accessed or manipulated.The
operation of the dedicated registers stated above are used to simplify code and minimize its
size.
String instructions are:
LODS
STOS
MOVS
CMPS
SCAS
PROGRAM:
DATA SEGMENT
STR1 DB "ENTER YOUR STRING HERE ->$"
STR2 DB "YOUR STRING IS ->$"
STR3 DB "COPIED STRING IS ->$"
INSTR1 DB 20 DUP("$")
CSTR DB 20 DUP("$")
NEWLINE DB 10,13,"$"
N DB ?
S DB ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA SI,INSTR1
;GET STRING
MOV AH,09H
LEA DX,STR1
INT 21H
MOV AH,0AH
MOV DX,SI
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
;PRINT THE STRING
MOV AH,09H
LEA DX,STR2
INT 21H
MOV AH,09H
LEA DX,INSTR1+2
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
;PRINT THE COPIED STRING
MOV AH,09H
LEA DX,STR3
INT 21H
MOV CL,INSTR1+1
ADD CL,1
ADD SI,2
LEA DI,CSTR
L1:
MOV BL,BYTE PTR[SI]
MOV BYTE PTR[DI],BL
INC SI
INC DI
CMP BYTE PTR[SI],"$"
JNE L1
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,09H
LEA DX,CSTR
INT 21H
MOV AH,09H
LEA DX,NEWLINE
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
______________________________________________________________________________
26.AIM: To write 8086 program(Assembly Language) to reverse a string using string
instructions (or) functions .
DESCRIPTION: 8086 Assembly is a low level programming language.Mnemonics are used in
writing the program.
Reversing a string means the string that will be given by the user to your program in a specific
sequence will get entirely reversed when the reverse of a string algorithm gets implemented on
that particular input string.
Ex: “raw” is input string then “war” is shown by the code upon exceuting program.
In 8086 to implement reversing of string ,we use string functions namely LODSB(loads a byte
from datasegment into AL ; source operand is a memory location whose address is read from
DS:SI ) and STOSB(copies a byte from AL and stores in a memory location whose address is
given by DI:ES).
In additional to the above mentioned we use direction flag which plays vital role in what
direction to be operated.(If DF=1 ,IP decrements else IP increments)
PROGRAM
seg segment
a db '$raw$'
c db '$after reversing : $'
b db 5 dup('$') ;initializes to b1($),b2($),b3($),b4($),b5($)
seg ends
code segment
assume cs:code, ds:seg, es:seg
start:
mov ax,seg
mov ds,ax
mov es,ax
lea si,a
lea di,[b+4] ;loads eff.address of b4
mov cx,04h
UP:
cld ;df=0
lodsb
std ;df=1
stosb
loop UP
mov ah,09h
lea dx,[c+1]
int 21h
mov ah,09h
lea dx,[b+1] ;loads eff.address of b2
int 21h ;b contains '$war$'
code ends
end start
Output:
____________________________________________________________________________
27.AIM: To check whether given string is palindrome or not.
DESCRIPTION:
This program checks whether the word is equal when reversed also. If the word and reversed
word are equal then it is true else false. Here we use the in-built registers of the processor and
find whether the given string is palindrome or not.
PROGRAM:
seg1 segment
a db '$madam$'
b db 7 dup('$')
str1 db 'Given string is a palindrome$'
str2 db 'Given string is not a palindrome$'
seg1 ends
code segment
assume cs:code, ds:seg1, es:seg1
start:
mov ax,seg1
mov ds,ax
mov es,ax
lea si,a
lea di,[b+6]
mov cx,07h
UP:
cld
lodsb
std
stosb
loop UP
lea si,a
lea di,b
mov cx,07h
cld
repe
cmpsb
jz down1
mov ah,09h
lea dx,str2
int 21h
jmp down2
down1:
mov ah,09h
lea dx,str1
int 21h
down2:
code ends
end start
OUTPUT
______________________________________________________________________________
28.AIM: Write a program to implement Bubble Sort.
DESCRIPTION:
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that
repeatedly steps through the list, compares adjacent elements and swaps them if they are in
the wrong order. The pass through the list is repeated until the list is sorted. The algorithm,
which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top
of the list.
PROGRAM:
data segment
array db 2,1,3,6,9
data ends
code segment
assume cs:code, ds:data
start:
mov ax,data
mov ds,ax
lea si,array
mov ch,04h
UP1:
lea si,array
mov cl,ch
UP2:
mov al,[si]
mov ah,[si+1]
cmp al,ah
jz skip
jc skip
mov [si],ah
mov [si+1],al
skip:
inc si
dec cl
jnz UP2
dec ch
jnz UP1
lea si,array
mov cx,05h
back:
mov ah,02h
mov dl,[si]
add dl,30h
int 21h
inc si
loop back
code ends
end start
OUTPUT
______________________________________________________________________________
29.AIM:To write a program to demonstrate procedures using stacks.
DESCRIPTION:A procedure is a set of code that can be branched to and returned from in such a
way that the code is as if it were inserted at the point from which it is branched to. The branch
to procedure is referred to as the call, and the corresponding branch back is known as the
return. The return is always made to the instruction immediately following the call regardless of
where the call is located.The CALL instruction not only branches to the indicated address, but
also pushes the return address onto the stack. The RET instruction simply pops the return
address from the stack. The registers used by the procedure need to be stored before their
contents are changed, and then restored just before their contents are changed, and then
restored just before the procedure is excited.
A CALL may be direct or indirect and intrasegment or intersegment. If the CALL is intersegment,
the return must be intersegment. Intersegment call must push both (IP) and (CS) onto the stack.
The return must correspondingly pop two words from the stack. In the case of intrasegment call,
only the contents of IP will be saved and retrieved when call and return instructions are used.
Procedures are used in the source code by placing a statement of the form at the beginning of
the procedure
Procedure name PROC Attribute and by terminating the procedure with a statement
The attribute that can be used will be either NEAR or FAR. If the attribute is NEAR, the RET
instruction will only pop a word into the IP register, but if it is FAR, it will also pop a word into the
CS register.
2. A code segment that is different from the one containing the statement that calls it, but in the
same source module as the calling statement.
In the first case, the attribute could be NEAR provided that all calls are in the same code segment
as the procedure. For the latter two cases the attribute must be FAR. If the procedure is given a
FAR attribute, then all calls to it must be intersegment calls even if the call is from the same code
segment. For the third case, the procedure name must be declared in EXTRN and PUBLIC
statements.
When both the calling program and procedure share the same set of registers, it is necessary to
save the registers when entering a procedure, and restore them before returning to the calling
program.
PROGRAM:
data segment
a dw 'First arguement-1$'
b dw 'Second arguement-2$'
c dw 'Third arguement-3$'
d dw 'returned from function$'
data ends
code segment
assume cs:code,ds:code
start:
mov ax,data
mov ds,ax
lea bx,a
push bx
lea bx,b
push bx
lea bx,c
push bx
call demo
lea dx,d
mov ah,09h
int 21h
jmp down
demo proc near
pop bx
pop dx
mov ah,09h
int 21h
pop dx
mov ah,09h
int 21h
pop dx
mov ah,09h
int 21h
push bx
ret
down:
code ends
end start
OUTPUT:
______________________________________________________________________________
DESCRIPTION:
A Macro is a set of instructions grouped under a single unit.
It is another method for implementing modular programming in the 8086 microprocessors
The Macro is different from the Procedure in a way that unlike calling and returning the control
as in procedures, the processor generates the code in the program every time whenever and
wherever a call to the Macro is made .A Macro can be defined in a program using the following
assembler directives:
MACRO (used after the name of Macro before starting the body of the Macro) and ENDM (at
the end of the Macro).
All the instructions that belong to the Macro lie within these two assembler directives.
PROGRAM:
demo macro
lea dx,a
mov ah,09h
int 21h
endm
data segment
a db 'Macro executed$'
data ends
code segment
assume cs:code, ds:data
start:
mov ax,data
mov ds,ax
demo
code ends
end start
OUTPUT:
31.AIM: To write a program for SUM, AVG of given array elements in 8086.
DESCRIPTION:
a) In the program of sum of elements in array ,
CX register stores total number of elements in the array .hence we add elemenst throughout
the loop and store the sum in AL .
SI stores the starting address of the array and increments accordingly to access the elements
sequentially.
if the sum of array is single bit we can directly display it by storing it in AL but,
In case of two digit numbers, we need to store both the digit in AH and AL respectively, so that
we can display both digits one by one .
For the case of sum greater than 2 digits, we need to store the array as DW(Data Word) .
b) In the program of average of elements in array,
CX stores the no. of elements in the array as in the program of sum of elements.
SI stores the starting address of the array and increments accordingly to access the elements
sequentially.
Initially AL stores the total sum, then divides the sum by the total no. of elements which is
stores in the BL register.
Next, we convert the hexadecimal value to decimal with appropriate instructions and display
them with the help of INT 21H
PROGRAMS
A) PROGRAM FOR SUM OF ELEMENTS IN ARRAY:
DATA SEGMENT
ARRAY DB 14,12,15,12,4,2,5,5,12
AVG DB ?
MSG DB "SUM = $"
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA SI,ARRAY
LEA DX,MSG
MOV AH,9
INT 21H
MOV AX,00
MOV BL,9
MOV CX,9
LOOP1:
ADD AL,ARRAY[SI]
INC SI
LOOP LOOP1
MOV AH,0
MOV DL,10
DIV DL
ADD AX,3030H
MOV DH,AH
MOV DL,AL
MOV AH,02H
INT 21H
MOV DL,DH
INT 21H
ENDS
END START
OUTPUT:
B) PROGRAM FOR AVERAGE OF ELEMENTS IN ARRAY :
DATA SEGMENT
ARRAY DB 9,1,2,5,2,6,3,5,2
AVG DB ?
MSG DB "AVERAGE = $"
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA SI,ARRAY
LEA DX,MSG
MOV AH,9
INT 21H
MOV AX,00
MOV BL,9
MOV CX,9
LOOP1:
ADD AL,ARRAY[SI]
INC SI
LOOP LOOP1
DIV BL
ADD AL,30H
MOV DL,AL
MOV AH,2
INT 21H
ENDS
END START
OUTPUT: