Assembler Emulator Lab Programs With Explanation PDF
Assembler Emulator Lab Programs With Explanation PDF
PROGRAMS
=>LAB NO:1<=
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.
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
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:
; Memory variables
; Arrays
Array1 db 10, 12, 14, 20, 13, 7
Array2 db 29, 12, 0, 33, 100, 44
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
L1001:
mov ah, 2
mov dl, 09 ;TAB Character
int 21h
inc bx
loop L1001
; print msg2
lea dx, msg2
mov ah, 9
int 21h
L1002:
inc bx
loop L1002
; print msg3
lea dx, msg3
mov ah, 9
int 21h
L1003:
mov al, Array1 [bx]
add al, Array2 [bx]
mov Array3 [bx], al
; 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
;--------------------
; 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
mov bx, ax
; print -
mov dl, '-'
mov ah, 2
int 21h
L1:
; divide ax by 10
; ( (dx=0:)ax / cx(= 10) )
mov dx, 0
mov cx, 10
div cx
L2:
; if the quotient is not zero, we first call
; printd again for the quotient, and then we
; print the remainder.
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.
mov dx, 0
mov cx, 10
div cx
cmp ax, 0
jne L4
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
OUTPUT:
=>LAB NO:4<=
PROGRAM:
.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 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.
INCLUDE "EMU8086.INC"
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 VAL1, NL1, NL2, NL3 variables are declared without any
initial values.
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.
MOV AX, 1: This sets the value 1 in the AX register. It will hold
the result of the factorial calculation.
EXIT: This label marks the end of the loop. The program flow
continues from here.
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 MOV AX, DATA instruction moves the offset of the DATA
segment to the AX register.
The MOV AX, NUM instruction loads the first number from the
NUM array into the AX register.
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 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 CODE ENDS and END START directives mark the end of
the program.
=>LAB NO:8<=
OUTPUT:
2
NOTE:GCD OF 32 AND 10 IS 2
=>LAB NO:9<=
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.
The MOV AX, DATA instruction moves the offset of the DATA
segment to the AX register.
The MOV AX, NUM instruction loads the first number from the
NUM array into the AX register.
If a match is found (at label FO), it calls the PRINT macro with
the argument MSG1 to display the message "FOUND."
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
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
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
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
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).
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).
Memory initialization:
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: