MIC All Experiment
MIC All Experiment
DATA SEGMENT
SOURCE DB 10H, 20H, 30H, 40H, 50H ; Source block (Example Data)
DEST DB 5 DUP(?) ; Destination block (Uninitialized)
LENGTH EQU 5 ; Number of bytes to transfer DATA
ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Initialize DS with Data Segment Address
MOV ES, AX ; Initialize ES (same segment for simplicity)
CODE ENDS
END START
DATA SEGMENT
SOURCE DB 10H, 20H, 30H, 40H, 50H ; Source block (Example Data)
DEST DB 5 DUP(?) ; Destination block (Uninitialized)
LENGTH EQU 5 ; Number of bytes to transfer DATA
ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load data segment in DS
MOV ES, AX; Load extra segment in ES (same segment for simplicity)
TRANSFER_LOOP:
MOV AL, [SI] ; Load byte from source
MOV [DI], AL ; Store byte to destination
INC SI ; Move to next source byte
INC DI ; Move to next destination byte
LOOP TRANSFER_LOOP ; Repeat until CX becomes 0
CODE ENDS
END START
DATA SEGMENT
NUMBERS DB 05H, 0AH, 0FH, 02H, 08H ; Example hex numbers (5 bytes)
COUNT DB 05H ; Number of elements in the series
SUM DW 0000H ; Variable to store sum (word size) DATA
ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load data segment
CODE ENDS
END START
2.Write an assembly language program to find sum of series of n BCD
numbers.
DATA SEGMENT
NUMBERS DB 12H, 25H, 37H, 19H, 45H ; Example BCD numbers
COUNT DB 05H ; Number of elements in the series
SUM DB 00H ; Variable to store the sum (BCD) DATA
ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load data segment
SUM_LOOP:
ADD AL, [SI] ; Add BCD number to AL
DAA ; Adjust AL for valid BCD sum
INC SI ; Move to next number in the list LOOP
SUM_LOOP ; Repeat until CX = 0
CODE ENDS
END START
Exp. 9 ALP to find smallest and largest number from array of numbers
DATA SEGMENT
N DB 06H ; Number of elements in the array
NUMBERS DB 25H, 63H, 12H, 89H, 07H, 45H ; Array of numbers
LARGEST DB 00H ; Variable to store the largest number
SMALLEST DB 00H ; Variable to store the smallest number
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load data segment
; Load count of numbers
MOV CX, N into CX
MOV SI, OFFSET NUMBERS ; SI points to the first element
MOV AL, [SI] ; Load first element into AL
(Largest)
MOV BL, [SI] ; Load first element into BL
(Smallest)
INC SI ; Move SI to the next element
DEC CX ; Decrement CX as first element is already considered
FIND_LOOP:
MOV DL, [SI] ; Load the current number into DL
SKIP_LARGEST:
; Check for Smallest
CMP BL, DL ; Compare BL (Smallest so far) with DL
JBE SKIP_SMALLEST ; If BL <= DL, skip updating smallest
MOV BL, DL ; Else, update BL as new smallest
SKIP_SMALLEST:
INC SI ; Move to the next number
LOOP FIND_LOOP ; Repeat until CX = 0
CODE ENDS
END START
DATA SEGMENT
N DB 06H ; Number of elements in the array
NUMBERS DB 25H, 63H, 12H, 89H, 07H, 45H ; Array of
numbers DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
; CX = Total elements (Outer loop
MOV CX, N counter)
DEC CX ; Outer loop runs N-1 times
ITERATIVE_SORT:
MOV SI, OFFSET NUMBERS ; SI points to the first element
MOV DX, CX ; DX = CX (Inner loop counter)
COMPARE_SWAP:
MOV AL, [SI] ; Load current element into AL
MOV BL, [SI+1] ; Load next element into BL
NO_SWAP:
INC SI ; Move SI to next pair
DEC DX ; Decrement inner loop counter
JNZ COMPARE_SWAP ; Repeat inner loop if DX != 0
CODE ENDS
END START
Exp 11.ALP to find the length of string and concatanate two strings
DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string (Null-terminated using '$')
STR2 DB ' WORLD$', 0 ; Second string (Null-terminated using '$')
LENGTH DB 00H ; Variable to store string length
CONCAT DB 20 DUP('$') ; Buffer for concatenated string (Large
enough) DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
FIND_LENGTH:
CMP BYTE PTR [SI], '$' ; Check for end of string
JE STORE_LENGTH ; If '$' found, store length
INC SI ; Move to next character
INC CX ; Increment length count
JMP FIND_LENGTH ; Repeat
STORE_LENGTH:
MOV LENGTH, CL ; Store length of STR1
CODE ENDS
END START
Exp12.ALP for string operations such as string reverse and string copy
DATA SEGMENT
STR1 DB 'HELLO$', 0; Input string (Null-terminated using '$')
STR_COPY DB 20 DUP('$') ; Buffer to store copied string
STR_REV DB 20 DUP('$') ; Buffer to store reversed
string DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
COPY_LOOP:
MOV AL, [SI] ; Load character from
STR1
MOV [DI], AL ; Store it in STR_COPY
INC SI ; Move to next character
INC DI ; Move to next position
CMP AL, '$' ; Check for end of string
JNE COPY_LOOP ; If not '$', continue copying
FIND_LENGTH:
CMP BYTE PTR [SI], '$' ; Check for end of string
JE STORE_LENGTH ; If found, store length
INC SI ; Move to next character
INC CX ; Increment length count JMP
FIND_LENGTH ; Repeat
STORE_LENGTH:
DEC CX ; Adjust length (to exclude '$')
MOV SI, OFFSET STR1 ; Point to STR1 (Start)
ADD SI, CX ; Move SI to last character of STR1
MOV DI, OFFSET STR_REV ; Destination for reversed string
MOV BYTE PTR [DI], '$' ; Add null-terminator ('$') at end of STR_REV
CODE ENDS
END START
Exp 13. ALP to compare two strings
Compare Two Strings (Without String Instructions)
DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string (Null-terminated with '$')
STR2 DB 'HELLO$', 0 ; Second string (Modify to test differences)
MSG_EQUAL DB 'STRINGS ARE EQUAL$', 0
MSG_NOT_EQUAL DB 'STRINGS ARE NOT EQUAL$',
0 DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
EQUAL:
MOV DX, OFFSET MSG_EQUAL ; Load message "STRINGS ARE
EQUAL" JMP DISPLAY_MSG
NOT_EQUAL:
MOV DX, OFFSET MSG_NOT_EQUAL ; Load message "STRINGS ARE NOT
EQUAL"
DISPLAY_MSG:
; Simulate printing the message (for emulator)
HLT ; Halt execution
CODE ENDS
END START
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
MOV ES, AX ; Load Extra Segment (for STR2)
COMPARE_LOOP:
LODSB ; Load AL with byte from STR1 (SI increments)
SCASB ; Compare AL with byte in STR2 (DI increments)
EQUAL:
MOV DX, OFFSET MSG_EQUAL ; Load message "STRINGS ARE
EQUAL" JMP DISPLAY_MSG
NOT_EQUAL:
MOV DX, OFFSET MSG_NOT_EQUAL ; Load message "STRINGS ARE NOT
EQUAL"
DISPLAY_MSG:
; Simulate printing the message (for emulator)
HLT ; Halt execution
CODE ENDS
END START
DATA SEGMENT
NUM DB 5 ; Change this value to test different numbers
MSG_EVEN DB 'NUMBER IS EVEN$', 0
MSG_ODD DB 'NUMBER IS ODD$', 0
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX ; Load Data Segment
DISPLAY_MSG:
; Simulate printing the message (for emulator)
HLT ; Halt execution
CODE ENDS
END START
is_positive:
lea dx,
msg_pos mov
ah, 09h int
21h jmp exit
is_negative:
lea dx,
msg_neg mov
ah, 09h int
21h
exit:
mov ah, 4ch
int 21h
end main
; Check sign of AL
test al, al ; Sets flags based on AL (including Sign flag)
jns is_positive ; Jump if Sign Flag = 0 (i.e., number is
positive)
is_negative:
lea dx,
msg_neg mov
ah, 09h int
21h jmp exit
is_positive:
lea dx,
msg_pos mov
ah, 09h int
21h
exit:
mov ah, 4ch
int 21h
end main
16. ALP to count number of' ‘0' and '1's in a given number
.model small
.stack 100h
.data
ones_msg db 'Number of 1s: $'
zeros_msg db 'Number of 0s: $'
.code
main:
mov ax, @data mov ds, ax mov al, 0F3h ; Example
number (11110011b), change as needed
mov bl, 8 ; Counter for 8 bits
mov ch, 0 ; Count of 1's
mov cl, 0 ; Count of 0's
check_bit:
ror al, 1 ; Rotate right, LSB -
> CF
jc count_one ; If CF=1, it's a '1'
inc cl ; Count 0s
jmp next_bit
count_one:
inc ch ; Count 1s
next_bit: dec
bl jnz
check_bit
; Print number of
1s lea dx,
ones_msg mov
ah, 09h int 21h
; Print number of
0s lea dx,
zeros_msg mov
ah, 09h int 21h
; Exit
mov ah, 4ch
int 21h
end main
17.ALP to Perform Arithmetic Operations Using Procedures
.model small
.stack 100h
.data
num1 db 10
num2 db 5
.code
main: mov ax,
@data mov
ds, ax
call addition
call
print_result
call subtraction
call print_result
call multiplication
call print_result
call division
call
print_result
; Exit
program
mov ah,
4ch int 21h
; =============================
; Procedures
; =============================
; --- Division --
division proc
mov al,
num1 mov
ah, 0
mov bl,
num2
div bl ; AL = AL / BL, remainder in
AH mov bl, al
ret
division endp
; --- Print Result --
print_result proc
mov dl, bl add dl, 30h ;
Convert to ASCII mov ah,
02h
int 21h
mov dl, 10 ; New line
mov ah, 02h int 21h
mov dl, 13 int 21h
ret
print_result endp
end main
; ================================
; MACROS
; ================================
ADD_MACRO MACRO
a, b mov al, a add
al, b
mov bl, al
ENDM
SUB_MACRO MACRO
a, b mov al, a sub
al, b mov bl, al
ENDM
MUL_MACRO MACRO
a, b mov al, a mov
bl, b
mul bl ; AL = AL * BL
mov bl, al
ENDM
DIV_MACRO MACRO a, b mov
al, a mov ah, 0 mov bl, b
div bl ; AL = AL / BL, result in AL
mov bl, al
ENDM
; New
line mov
dl, 10 int
21h mov
dl, 13 int
21h
ENDM
main:
mov ax, @data
mov ds, ax