ASSEMBLER EMULATOR 8086 LAB
PROGRAMS
=>LAB NO:1<=
*ASSEMBLY LANGUAGE PROGRAM TO FIND
THE LARGEST NUMBER IN AN ARRAY ?
PROGRAM:
DATA SEGMENT
X DW 0002H, 00101H, 0004H, 0005H, 0006H
MES DB 10, 13, 'LARGEST NUMBER AMONG THE SERIES IS $'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV CX, 05H
LEA SI, X
MOV AX, [SI]
DEC CX
UP:
CMP AX, [SI + 2]
JAE CONTINUE
MOV AX, [SI + 2]
CONTINUE:
ADD SI, 2
DEC CX
JNZ UP
MOV BX, AX
ADD BX, 3030H
MOV AH, 09H
LEA DX, MES
INT 21H
MOV DL, BH
MOV AH, 02H
INT 21H
MOV DL, BL
INT 21H
MOV AH, 4CH
INT 21H
CODE ENDS
END START
HLT
output:-
EXPLANATION:-
The data segment is defined, which includes the array X
and the message MES.
The code segment is defined, and the necessary
assumptions are made.
The start label marks the beginning of the code.
The AX register is loaded with the address of the data
segment, and DS is updated accordingly.
The CX register is initialized with the count of numbers in
the series (05H in this case).
The SI register is loaded with the address of the X array.
The first number in the array is loaded into the AX
register, and CX is decremented.
The loop labeled UP begins, which compares the current
number (AX) with the next number in the series ([SI + 2]).
If the current number is less than the next number, the
program jumps to the CONTINUE label. Otherwise, it
updates AX with the larger number.
The CONTINUE label is where execution continues if the
current number is not less than the next number. It
advances the SI register to the next number in the series
and decrements CX. If CX is not zero, the program jumps
back to the UP label to compare the current number with
the next one.
After the loop ends, the largest number is stored in the
AX register. The code then adds 3030H to convert it to its
ASCII representation.
DOS interrupt calls are used to display the message
stored in MES and print the largest number as separate
characters.
Finally, the program terminates using the DOS interrupt
4CH.
=>LAB NO:2<=
*ASSEMBLY LANGUAGE PROGRAM TO FIND
SMALLEST NUMBER IN AN ARRAY ?
PROGRAM:
DATA SEGMENT
X DW 0002H,00101H,0004H,0005H,0006H
MES DB 10,13,'SMALLEST NUMBER AMONG THE SERIES IS $'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV CX,05H
LEA SI,X
MOV AX,[SI]
DEC CX
UP: CMP AX,[SI+2]
JB CONTINUE
MOV AX,[SI+2]
CONTINUE:ADD SI,2
DEC CX
JNZ UP
AAM
ADD AX,3030H
MOV BX,AX
MOV AH,09H
LEA DX,MES
INT 21H
MOV DL,BH
MOV AH,02H
INT 21H
MOV DL,BL
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
EXPLANATION:-
The code you provided is written in x86 assembly
language. Let's go through it step by step to understand
what it does:
Data Segment: This section defines the data variables
used in the program. It starts with the DATA SEGMENT
directive and ends with the DATA ENDS directive.
X is an array of words (16-bit integers) with the
values 0002H, 00101H, 0004H, 0005H, 0006H.
MES is a string that holds the message "SMALLEST
NUMBER AMONG THE SERIES IS $" terminated by carriage
return (CR) and line feed (LF) characters.
Code Segment: This section contains the actual code
instructions of the program. It starts with the CODE
SEGMENT directive and ends with the CODE ENDS
directive.
ASSUME: The ASSUME statement establishes the
association between segment registers and logical
segments. It tells the assembler that CS (Code Segment)
is associated with CODE segment, and DS (Data
Segment) is associated with DATA segment.
START: This is the entry point of the program, marked
by the START label.
MOV AX, DATA moves the offset of the DATA
segment into the AX register.
MOV DS, AX sets the DS (Data Segment) register to
point to the DATA segment, allowing access to the
defined data variables.
MOV CX, 05H: This instruction moves the value 05H
(decimal 5) into the CX register. It is used as a loop
counter to iterate through the array X.
LEA SI, X: The LEA (Load Effective Address) instruction
loads the offset of the array X into the SI register. SI will
be used as the pointer to access the elements of the
array.
MOV AX, [SI]: This instruction loads the value at the
memory location pointed by SI (the first element of X)
into the AX register.
DEC CX: Decrements the value of CX by 1.
UP: This is a loop label that marks the beginning of a
loop.
CMP AX, [SI+2]: Compares the value in AX with the
value at the memory location SI+2 (the next element of
X).
JB CONTINUE: If the result of the comparison (AX <
[SI+2]) indicates a jump below, it proceeds to the
CONTINUE label.
MOV AX, [SI+2]: Moves the value at memory location
SI+2 (the next element of X) into the AX register.
CONTINUE: This label is reached when the current
value in AX is smaller than the previous minimum value.
ADD SI, 2: Adds 2 to the SI register to point to the next
element in the array X.
DEC CX: Decrements the value of CX by 1.
JNZ UP: Jumps back to the UP label if the value of CX is
not zero (i.e., there are more elements in the array to
process).
AAM: Adjusts the AX register after multiplication. This
instruction splits the binary value in AL into two BCD
(Binary Coded Decimal) digits, storing
=>LAB NO:3<=
* Assembly language program for adding to two
arrays ?
PROGRAM:
data segment
; add your data here!
; Memory variables
; Size of the array. Note: arrSize should match
; the number of bytes db'ed (byte-defined) for
; Array1 and Array2
arrSize = 6
; Arrays
Array1 db 10, 12, 14, 20, 13, 7
Array2 db 29, 12, 0, 33, 100, 44
; Array3 is not initialized
Array3 db arrSize dup (?)
; prompts and messages
msg1 db "Array 1: $"
msg2 db "Array 2: $"
msg3 db "Sum : $"
; endl : string of return + new-line
endl db 10, 13, '$'
; a string to keep prompt visible
pkey db "press any key...$"
ends
stack segment
dw 128 dup(0)
ends
code segment
;--------------------
; MAIN PROGRAM
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
;--------------------
; add your code here
; print msg1
lea dx, msg1
mov ah, 9
int 21h
; Use loop to print the values of Array1
mov cx, arrSize
mov bx, 0
L1001:
mov al, Array1 [bx]
; Extend (unsigned) AL to AX (to print)
mov ah, 0
call printd
mov ah, 2
mov dl, 09 ;TAB Character
int 21h
inc bx
loop L1001
; print endl after Array1
lea dx, endl
mov ah, 9
int 21h
; print msg2
lea dx, msg2
mov ah, 9
int 21h
; Use loop to print values of Array2
mov cx, arrSize
mov bx, 0
L1002:
mov al, Array2 [bx]
; Extend (unsigned) AL to AX (to print)
mov ah, 0
call printd
mov ah, 2
mov dl, 09 ;TAB Character
int 21h
inc bx
loop L1002
; print two endl's
lea dx, endl
mov ah, 9
int 21h
lea dx, endl
mov ah, 9
int 21h
; print msg3
lea dx, msg3
mov ah, 9
int 21h
; calculate sum's element by elements
; and print each sum using a loop
mov cx, arrSize
mov bx, 0
L1003:
mov al, Array1 [bx]
add al, Array2 [bx]
mov Array3 [bx], al
; Extend (unsigned) AL to AX (to print)
mov ah, 0
call printd
; print TAB
mov ah, 2
mov dl, 09 ;TAB Character
int 21h
inc bx
loop L1003
; print endl
lea dx, endl
mov ah, 9
int 21h
;--------------------
lea dx, pkey
mov ah, 9
int 21h ; output string at ds:dx
; wait for any key....
mov ah, 1
int 21h
mov ax, 4c00h ; exit to operating system.
int 21h
;--------------------
; Function printd
; prints the value of AX register in signed
; decimal format.
;
; This function uses a recursive algorithm to print
; The value in AX register. For example, to print the
; value 3187, this function call itself (printd)
; for 318, then prints 7.
; If the value to print is less than 10, then it is
; printed and recursion terminates.
; If the value is negative, a - is printed then
; printd is called for the negate of value.
printd proc
; preserve used registers
push ax
push bx
push cx
push dx
; if negative value, print - and call again with -value
cmp ax, 0
jge L1
mov bx, ax
; print -
mov dl, '-'
mov ah, 2
int 21h
; call with -AX
mov ax, bx
neg ax
call printd
jmp L3
L1:
; divide ax by 10
; ( (dx=0:)ax / cx(= 10) )
mov dx, 0
mov cx, 10
div cx
; if quotient is zero, then print remainder
cmp ax, 0
jne L2
; DX contains the remainder, but since DX < 10;
; actually DL contains it. In order to print it
; In ASCII format, we should add '0' to it.
; For example, the ascii code of 5 is 53,
; and the ascii code of '0' is 48. In order to
; print 5, we add '0' to it to make it '5'.
add dl, '0'
mov ah, 2
int 21h
jmp L3
L2:
; if the quotient is not zero, we first call
; printd again for the quotient, and then we
; print the remainder.
; call printd for quotient:
call printd
; print the remainder
add dl, '0'
mov ah, 2
int 21h
L3:
; recover used registers
pop dx
pop cx
pop bx
pop ax
ret
printd endp
;--------------------
; Function printud
; prints the value of AX register in unsigned
; decimal format.
;
; This function uses a recursive algorithm to print
; The value in AX register. For example, to print the
; value 3187, this function call itself (printud)
; for 318, then prints 7.
; If the value to print is less than 10, then it is
; printed and recursion terminates.
; The comments are exactly like printd. We just dropped
; the code for negative case. There is no negative case
; for unsigned integer: -1 is assumed 65535.
printud proc
push ax
push bx
push cx
push dx
mov dx, 0
mov cx, 10
div cx
cmp ax, 0
jne L4
add dl, '0'
mov ah, 2
int 21h
jmp L5
L4:
call printud
add dl, '0'
mov ah, 2
int 21h
L5:
pop dx
pop cx
pop bx
pop ax
ret
printud endp
;--------------------
ends
end start ; set entry point and stop the assembler.
OUTPUT:
=>LAB NO:4<=
* ASSEMBLY PROGRAM TO SEPERATE
ODD OR EVEN FROM AN ARRAY ?
PROGRAM:
; check whether a number is even or odd
data segment
msg1 db 10,13,'enter number here :- $'
msg2 db 10,13,'entered value is even$'
msg3 db 10,13,'entered value is odd$'
data ends
display macro msg
mov ah,9
lea dx,msg
int 21h
endm
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
display msg1
mov ah,1
int 21h
mov ah,0
check: mov dl,2
div dl
cmp ah,0
jne odd
even:
display msg2
jmp done
odd:
display msg3
done:
mov ah,4ch
int 21h
code ends
end start
OUTPUT:
enter number here:-5
Entered value is odd
EXPLANATION:-
The data segment section defines the data used by the
program.
msg1 is a string that prompts the user to enter a number.
msg2 is a string that will be displayed if the entered value
is even.
msg3 is a string that will be displayed if the entered value
is odd.
data ends marks the end of the data segment.
The display macro is defined to simplify displaying
messages. It uses the int 21h interrupt with ah=9 to
display a string pointed to by the dx register.
The code segment section contains the actual code
instructions.
The assume directive sets up the segment registers.
mov ax, data loads the starting address of the data
segment into the ax register.
mov ds, ax sets the data segment register ds to the value
in ax.
display msg1 displays the message prompting the user to
enter a number.
mov ah, 1 sets the ah register to 1, indicating that we
want to read a character from the keyboard.
int 21h performs the interrupt, reading a character from
the keyboard and storing it in al.
mov ah, 0 clears the high byte of ax in preparation for
division.
check is a label marking the start of the check for
even/odd.
mov dl, 2 sets the divisor to 2.
div dl divides ax by dl (divisor), storing the quotient in al
and the remainder in ah.
cmp ah, 0 compares the remainder (ah) with zero to
check if the number is even.
jne odd jumps to the odd label if the remainder is not zero
(i.e., the number is odd).
even is a label marking the block of instructions for even
numbers.
display msg2 displays the message indicating that the
entered value is even.
jmp done jumps to the done label to exit the program
after displaying the appropriate message.
odd is a label marking the block of instructions for odd
numbers.
display msg3 displays the message indicating that the
entered value is odd.
done is a label marking the end of the program.
mov ah, 4Ch sets ah to 4Ch, indicating an exit program
function.
int 21h performs the interrupt, terminating the program.
The last two lines, code ends and end start, mark the end
of the code section and the entry point of the program,
respectively
=>LAB NO:5<=
* Assembly language Program to find prime
numbers between a given range
PROGRAM:
ORG 0100H
.MODEL SMALL
.DATA
VAl1 DB ?
NL1 DB 0AH,0DH,'ENTER NO:','$'
NL2 DB 0AH,0DH,'IT IS NOT PRIME','$'
NL3 DB 0AH,0DH,'IT IS PRIME','$'
.CODE
MAIN:
MOV AX,@DATA
MOV DS,AX
MOV DX,OFFSET NL1
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
SUB AL,30H
MOV VAL1,AL
MOV AH,00
MOV CL,2
DIV CL
MOV CL,AL
LBL1:
MOV AH,00
MOV AL,VAL1
DIV CL
CMP AH,00
JZ LBL2
DEC CL
CMP CL,1
JNE LBL1
JMP LBL3
LBL2:
MOV AH,09H
MOV DX,OFFSET NL2
INT 21H
JMP EXIT
LBL3:
MOV AH,09H
MOV DX, OFFSET NL3
INT 21H
EXIT:
MOV AH,4CH
INT 21H
END MAIN
OUTPUT:
EXPLANATION:-
The program starts with the declaration of the ORG
directive, which sets the origin of the program to the
address 0100H.
The MODEL directive specifies the memory model as
SMALL, indicating a 16-bit segmented memory model.
The DATA segment is defined using the .DATA directive.
The VAL1, NL1, NL2, NL3 variables are declared without
any initial values.
The DB (Define Byte) directive is used to define the ASCII
characters and messages. It defines '?' as a single byte
placeholder, followed by a series of bytes representing
strings such as 'ENTER NO:', 'IT IS NOT PRIME', and 'IT IS
PRIME'. The 0AH and 0DH bytes represent line feed and
carriage return characters for line breaks.
The CODE segment is defined using the .CODE directive.
The MAIN label marks the entry point of the program.
The MOV instructions are used to set up the data
segment register (DS) and to initialize the DX register
with the offset of NL1, which contains a message prompt.
The first interrupt (INT 21H) with the AH value of 09H is
used to display the message prompt NL1 on the screen.
The next interrupt (INT 21H) with the AH value of 01H
reads a single character from the keyboard input and
stores it in the AL register.
The ASCII value of the input character is then converted
to a numeric value by subtracting 30H (the ASCII value of
'0').
The numeric value is stored in the VAL1 variable.
The DIV instruction is used to divide VAL1 by 2
(represented by CL). The result is stored in AX (quotient)
and AH (remainder).
The quotient (in AH) is compared to 0 to check if there is
a remainder.
If the remainder is 0, the number is not prime, and the
program jumps to LBL2 to display the corresponding
message.
If there is a remainder, the program decrements the
value of CL and checks if CL is equal to 1. If not, it repeats
the division process (jumping to LBL1).
If CL is equal to 1, it means the number is prime, and the
program jumps to LBL3 to display the corresponding
message.
The messages NL2 and NL3 are displayed on the screen
using the same interrupt (INT 21H) with AH set to 09H.
Finally, the program reaches the EXIT label, which
terminates the program by using the INT 21H interrupt
with AH set to 4CH.
The END directive marks the end of the programm
=>LAB NO:6<=
*Assembly language program to find factorial of
the given number.
PROGRAM:
org 100h
INCLUDE "EMU8086.INC"
MOV DX, OFFSET MAIN
MOV AH, 9
INT 21H
CALL SCAN_NUM
MOV NUM, CX
MOV BX, 1
MOV AX, 1
JMP LABEL1
LABEL1:
MUL BX
INC BX
CMP BX,NUM
JA EXIT
JMP LABEL1
EXIT:
LEA SI, MSG
CALL PRINT_STRING
CALL PRINT_NUM
RET
NUM DW ?
MAIN DB "FACTORIAL CALCULATOR. NUM: $"
MSG DB 13,10, "THE RESULT IS: ", 0
DEFINE_PRINT_NUM
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM_UNS
DEFINE_PRINT_STRING
Ret
OUTPUT:
FACTORIAL CALCULATOR NUM:5
THE RESULT IS :120
EXPLANATION:-
The program starts with the declaration of the ORG directive,
which sets the origin of the program to the address 0100H.
The MODEL directive specifies the memory model as SMALL,
indicating a 16-bit segmented memory model.
The DATA segment is defined using the .DATA directive.
The VAL1, NL1, NL2, NL3 variables are declared without any
initial values.
The DB (Define Byte) directive is used to define the ASCII
characters and messages. It defines '?' as a single byte
placeholder, followed by a series of bytes representing strings
such as 'ENTER NO:', 'IT IS NOT PRIME', and 'IT IS PRIME'. The
0AH and 0DH bytes represent line feed and carriage return
characters for line breaks.
The CODE segment is defined using the .CODE directive.
The MAIN label marks the e…
org 100h: This directive sets the origin of the program to
memory address 100h (256 in decimal). It is the typical
starting point for assembly programs in the 8086
architecture.
INCLUDE "EMU8086.INC": This line includes a file named
"EMU8086.INC" which likely contains predefined macros and
constants specific to the EMU8086 emulator.
MOV DX, OFFSET MAIN: This instruction loads the offset of the
string labeled "MAIN" into the DX register. The string is
probably a prompt for the user to enter a number.
MOV AH, 9: This sets the value 9 in the AH register, indicating
that a string will be printed to the screen.
INT 21H: This triggers an interrupt 21h, which is a general-
purpose interrupt for various input/output operations in MS-
DOS. In this case, it is used to print the string specified by the
DX register.
CALL SCAN_NUM: This is a subroutine call to a function
named "SCAN_NUM." It likely reads an integer input from the
user and stores it in the CX register.
MOV NUM, CX: This instruction moves the value in the CX
register to a memory location labeled "NUM."
MOV BX, 1: This sets the value 1 in the BX register. It will be
used as a counter to calculate the factorial.
MOV AX, 1: This sets the value 1 in the AX register. It will hold
the result of the factorial calculation.
JMP LABEL1: This unconditional jump transfers control to the
label "LABEL1."
LABEL1: This is a label that marks the start of a loop.
MUL BX: This instruction multiplies the value in the AX
register (initially 1) by the value in the BX register (starts with
1). The result is stored in the AX register.
INC BX: This increments the value in the BX register by 1. It
increments the counter for the next iteration.
CMP BX, NUM: This instruction compares the value in the BX
register (the current counter) with the value stored in the
memory location labeled "NUM" (the user-provided number).
It determines if the counter has reached or exceeded the
input number.
JA EXIT: If the comparison result is "above" (greater than), the
jump is taken to the "EXIT" label, which ends the loop.
JMP LABEL1: If the comparison result is not "above," it means
the counter is still less than or equal to the input number.
Therefore, the loop continues, and control jumps back to the
"LABEL1" label.
EXIT: This label marks the end of the loop. The program flow
continues from here.
LEA SI, MSG: This instruction loads the effective address
(offset) of the string labeled "MSG" into the SI register. It
prepares for printing the result.
CALL PRINT_STRING: This is a subroutine call to a function
named "PRINT_STRING." It likely prints the string specified by
the SI register.
CALL PRINT_NUM: This is a subroutine call to a function
named "PRINT_NUM." It probably prints the value stored in
the AX register (the factorial result).
RET: This instruction is used to return from a subroutine,
completing the execution of the program.
=>LAB NO:7<=
* Assembly language program to find LCM.
PROGRAM:
DATA SEGMENT
NUM DW 04,04
LCM DW 2 DUP(0)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV DX,0H
MOV AX,NUM
MOV BX,NUM+2
UP: PUSH AX
PUSH DX
DIV BX
CMP DX,0
JE EXIT
POP DX
POP AX
ADD AX,NUM
JNC DOWN
INC DX
DOWN: JMP UP
EXIT: POP LCM+2
POP LCM
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
4
NOTE:LCM OF 4,4=4
EXPLANATION:-
The DATA SEGMENT section defines the data used in the
program. NUM is declared as an array of two words (04, 04),
and LCM is declared as an array of two words initialized to 0.
The CODE SEGMENT section contains the main code of the
program.
The ASSUME statement specifies the segment registers used
in the program.
The START label marks the beginning of the program
execution.
The MOV AX, DATA instruction moves the offset of the DATA
segment to the AX register.
The MOV DS, AX instruction sets the DS (data segment)
register to the value in AX, effectively pointing DS to the
DATA segment.
The MOV DX, 0H instruction sets the DX register to 0.
The MOV AX, NUM instruction loads the first number from the
NUM array into the AX register.
The MOV BX, NUM+2 instruction loads the second number
from the NUM array into the BX register. The +2 is used to
access the second word in the array.
The UP label marks the beginning of a loop.
The PUSH AX instruction pushes the value of AX onto the
stack.
The PUSH DX instruction pushes the value of DX onto the
stack.
The DIV BX instruction divides the value in AX by the value in
BX, storing the quotient in AX and the remainder in DX.
The CMP DX, 0 instruction compares the value in DX
(remainder) with 0.
The JE EXIT instruction jumps to the EXIT label if the previous
comparison resulted in equality (i.e., DX is zero).
The POP DX instruction pops the value from the top of the
stack into DX.
The POP AX instruction pops the value from the top of the
stack into AX.
The ADD AX, NUM instruction adds the value in NUM to AX.
The JNC DOWN instruction jumps to the DOWN label if there
was no carry (no overflow) after the addition.
The INC DX instruction increments the value in DX.
The DOWN label marks the point where the program jumps to
if there was a carry (overflow) after the addition.
The JMP UP instruction jumps back to the UP label to continue
the loop.
The EXIT label marks the end of the loop.
The POP LCM+2 instruction pops the top of the stack and
stores it in the second word of the LCM array.
The POP LCM instruction pops the top of the stack and stores
it in the first word of the LCM array.
The MOV AH, 4CH instruction sets the AH register to 4C,
which is the function number for program termination.
The INT 21H instruction triggers the software interrupt to
terminate the program.
The CODE ENDS and END START directives mark the end of
the program.
=>LAB NO:8<=
*Assembly language program to find GCD.
PROGRAM:
DATA SEGMENT
NUM1 DW 00016H
NUM2 DW 00010H
GCD DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA ;Load the Data to AX.
MOV DS,AX ;Move the Data AX to DS.
MOV AX,NUM1 ;Move the first number to AX.
MOV BX,NUM2 ;Move the second number to BX.
UP: CMP AX,BX ;Compare the two numbers.
JE EXIT ;If equal, go to EXIT label.
JB EXCG ;If first number is below than second,
;go to EXCG label.
UP1: MOV DX,0H ;Initialize the DX.
DIV BX ;Divide the first number by second number.
CMP DX,0 ;Compare remainder is zero or not.
JE EXIT ;If zero, jump to EXIT label.
MOV AX,DX ;If non-zero, move remainder to AX.
JMP UP ;Jump to UP label.
EXCG:XCHG AX,BX ;Exchange the remainder and quotient.
JMP UP1 ;Jump to UP1.
EXIT:MOV GCD,BX ;Store the result in GCD.
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
2
NOTE:GCD OF 32 AND 10 IS 2
=>LAB NO:9<=
* Assembly language program to search an
element using linear search.
PROGRAM:
DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
MSG2 DB "NOT FOUND$ "
SE DB 33H
DATA ENDS
PRINT MACRO MSG
MOV AH, 09H
LEA DX, MSG
INT 21H
INT 3
ENDM
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AL, SE
LEA SI, STRING1
MOV CX, 04H
UP:
MOV BL,[SI]
CMP AL, BL
JZ FO
INC SI
DEC CX
JNZ UP
PRINT MSG2
JMP END1
FO:
PRINT MSG1
END1:
INT 3
CODE ENDS
END START
OUTPUT:
FOUND
EXPLANTION:-
org 100h: This directive sets the origin of the program to
memory address 100h (256 in decimal). It is the typical
starting point for assembly programs in the 8086
architecture.
INCLUDE "EMU8086.INC": This line includes a file named
"EMU8086.INC" which likely contains predefined macros and
constants specific to the EMU8086 emulator.
MOV DX, OFFSET MAIN: This instruction loads the offset of the
string labeled "MAIN" into the DX register. The string is
probably a prompt for the user to enter a number.
MOV AH, 9: This sets the value 9 in the AH register, indicating
that a string will be printed to the screen.
INT 21H: This triggers an interrupt 21h, which is a general-
purpose interrupt for various input/output operations in MS-
DOS. In this case, it is used …
The DATA SEGMENT section defines the data used in the
program. NUM is declared as an array of two words (04, 04),
and LCM is declared as an array of two words initialized to 0.
The CODE SEGMENT section contains the main code of the
program.
The ASSUME statement specifies the segment registers used
in the program.
The START label marks the beginning of the program
execution.
The MOV AX, DATA instruction moves the offset of the DATA
segment to the AX register.
The MOV DS, AX instruction sets the DS (data segment)
register to the value in AX, effectively pointing DS to the
DATA segment.
The MOV DX, 0H instruction sets the DX register to 0.
The MOV AX, NUM instruction loads the first number from the
NUM array into the AX register.
The MOV BX, NUM+2 instr…
To search for a specific value (SE) in an array (STRING1)...The
program defines a data segment where it declares variables
STRING1, MSG1, MSG2, and SE. STRING1 is an array of bytes
(hexadecimal values), MSG1 and MSG2 are strings
(terminated with a dollar sign), and SE is a single byte.
The program defines a macro called PRINT which is used to
display a message. It uses the INT 21H interrupt function with
AH=09H to print the message stored in the DX register.
The program defines a code segment and assumes the code
and data segment registers.
The program moves the value of DATAMOV to the AX register
and then moves it to the DS register. This sets the data
segment to the starting address of the data segment.
The program moves the value of SE to the AL register.
The program loads the address of STRING1 into the SI
register using the LEA instruction.
The program sets the loop counter CX to 04H.
The program enters a loop labeled as UP:
It moves the value at the memory location pointed by SI
(contents of STRING1) into the BL register.
It compares the value in AL (SE) with the value in BL (byte
from STRING1).
If they are equal, it jumps to the label FO.
Otherwise, it increments SI to point to the next byte in
STRING1 and decrements the loop counter CX.
If the loop counter is not zero, it jumps back to the UP loop.
If the loop finishes without finding a match, it calls the PRINT
macro with the argument MSG2 to display the message "NOT
FOUND."
It jumps to the label END1 to exit the program.
If a match is found (at label FO), it calls the PRINT macro with
the argument MSG1 to display the message "FOUND."
It jumps to the label END1 to exit the program.
The program ends with the INT 3 instruction, which is a
software breakpoint.
Overall, the program searches for the value stored in SE
within the array STRING1 and displays a corresponding
message based on whether the value is found or not.
=>LAB NO:10<=
*Assembly language program to search an
element using binary search
PROGRAM:
DATA SEGMENT
ARR DW 05H,0111H,2161H,4541H,7161H,8231H
SR EQU 4541H
MSG1 DB 'ELEMENT FOUND AT '
RES DB ' RD POSITION','$'
MSG2 DB 'ELEMENT NOT FOUND','$'
DATA ENDS
ASSUME CS:CODE,DS:DATA
CODE SEGMENT
START: MOV AX,DATA
MOV DS,AX
MOV BX,00H
MOV CX,SR
MOV DX,05H
LP: CMP BX,DX
JA FAILURE
MOV AX,BX
ADD AX,DX
SHR AX,01
MOV SI,AX
ADD SI,SI
CMP CX,ARR[SI]
JAE BIGGER
DEC AX
MOV DX,AX
JMP LP
BIGGER: JE SUCCESS
INC AX
MOV BX,AX
JMP LP
SUCCESS:ADD AL,01H
ADD AL,2FH
MOV RES,AL
LEA DX,MSG1
JMP DISPLAY
FAILURE: LEA DX,MSG2
DISPLAY: MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
ELEMENT FOUND AT 3RD POSTITION
=>LAB NO:11<=
*Assembly language program to sort
numbers using bubble sort ?
Program:-
org 100h
jmp start
data_segment:
array db 24, 56, 48, 35
ary_cnt equ 04
code_segment:
start:
mov ax, data_segment ; Initialize
mov ds, ax ; Set data segment
mov bx, ary_cnt-1 ; Number of passes
nxtchk:
mov cx, bx ; Number of checks
mov si, 00h ; si = 0
nxtpas:
mov al, [array+si] ; al = 1st element
inc si
cmp al, [array+si] ; compare 1st and 2nd element
jbe nxtemt ; if below, jump to nxtemt
xchg al, [array+si] ; otherwise, exchange the elements
mov [array+si-1], al ; exchange the elements
nxtemt:
loop nxtpas ; proceed with next pass
dec bx ; bx = bx - 1
jnz nxtchk ; continue with next check
mov ah, 4ch ; Terminate the program
int 21h
end start
ret
output:-
EXPLANATION:-
org 100h: This sets the origin of the program to the memory
address 100h (which is typically the start of the program's
code segment).
jmp start: This jumps to the label "start" to begin the
execution of the program.
data_segment:: This label defines the start of the data
segment.
array db 24, 56, 48, 35: This defines an array named "array"
with four elements: 24, 56, 48, and 35. The elements are
represented as bytes (db).
ary_cnt equ 04: This defines a constant named "ary_cnt" with
the value 04, representing the number of elements in the
"array" (which is 4 in this case).
code_segment:: This label defines the start of the code
segment.
start:: This label marks the entry point of the program's
execution.
Memory initialization:
mov ax, data_segment: Moves the offset address of the
"data_segment" to the AX register.
mov ds, ax: Sets the data segment register (DS) to the value
in AX, which sets the base address for accessing data.
Sorting algorithm:
mov bx, ary_cnt-1: Moves the value of "ary_cnt-1" (3 in this
case) to the BX register, representing the number of passes
to perform.
nxtchk:: This label marks the beginning of the outer loop for
each pass.
mov cx, bx: Moves the value in BX to CX, representing the
number of checks to perform in each pass.
mov si, 00h: Clears the SI register, which will be used as an
index for accessing array elements.
Inner loop for comparisons and swaps:
nxtpas:: This label marks the beginning of the inner loop for
each check.
mov al, [array+si]: Moves the value of the current element
(at [array+si]) to the AL register.
inc si: Increments the SI register to access the next element
in the array.
cmp al, [array+si]: Compares the current and next elements.
jbe nxtemt: Jumps to "nxtemt" label if the current element is
below or equal to the next element.
xchg al, [array+si]: Exchanges the values of the current and
next elements.
mov [array+si-1], al: Moves the swapped value back to the
previous position.
nxtemt:: This label marks the end of the inner loop.
loop nxtpas: Decrements the CX register and jumps back to
"nxtpas" if CX is not zero (performs the next check).
Continuing with the next pass:
dec bx: Decrements the BX register.
jnz nxtchk: Jumps back to "nxtchk" if BX is not zero (performs
the next pass).
Program termination:
mov ah, 4ch: Moves the value 4Ch (the DOS exit function) to
the AH register.
int 21h: Triggers a software interrupt to terminate the
program using the DOS interrupt service routine.
end start: Marks the end of the program, specifying