COMPUTER ORGANIZATION AND ARCHITECTURE
LA- 2 PROGRAMMING ASSIGNMENT
MURAGESH ARABALLI
USN-1NT23IS411-T
ISE ‘C’
1. Write an assembly language program to search a key element in a list of ‘n’16-bit
numbers using the Binary search algorithm.
Code:
.model small
.stack 100h
.data
arr dw 1, 3, 4, 7, 8
key equ 7 ; Change the key to an element that exists in the array
len dw ($-arr)/2
msg1 db "Key is found at position $"
res db 6, 10, 13, "$"
msg2 db 'Key is not found!!!.$'
.code
main proc
start:
mov ax, @data
mov ds, ax
mov bx, 0
mov dx, [len]
mov cx, [key]
again:
cmp bx, dx
jae fail
mov ax, bx
add ax, dx
shr ax, 1
mov si, ax
add si, si
cmp cx, arr[si]
jae big
dec ax
mov dx, ax
jmp again
big:
je success
inc ax
mov bx, ax
jmp again
success:
add al, 30h
lea si, res
mov [si], al
lea dx, msg1
jmp disp
fail:
lea dx, msg2
disp:
mov ah, 09h
int 21h
mov ah, 4ch
int 21h
end main
2. Write an assembly language program to sort a given set of ‘n’ numbers in ascending
and descending orders using the bubble sort algorithm.
org 100h
Code:
.data
str db 10,13,"Enter Values: $"
str1 db 0dh,0ah,"Bubble Sorted: $"
array db 10dup(0)
.code
mov ah, 9
lea dx, str
int 21h
mov cx, 10
mov bx, offset array
mov ah, 1
inputs:
int 21h
mov [bx], al
inc bx
Loop inputs
mov cx, 10
dec cx
nextscan:
mov bx, cx
mov si, 0
nextcomp:
mov al, array[si]
mov dl, array[si + 1]
cmp al, dl
jc noswap
mov array[si], dl
mov array[si + 1], al
noswap:
inc si
dec bx
jnz nextcomp
loop nextscan
mov ah, 9
lea dx, str1
int 21h
mov cx, 10
mov bx, offset array
print:
mov ah, 02h
mov dl, [bx]
int 21h
mov ah, 02h
mov dl, ' '
int 21h
inc bx
loop print
ret
3. Write an assembly language program to read an alphanumeric character and display its
equivalent ASCII code at the centre of the screen.
Code:
.MODEL SMALL
GETCHAR MACRO
MOV AH,01H
INT 21H
ENDM
PUTCHAR MACRO CHAR
MOV AH,02H
MOV DL,CHAR
INT 21H
ENDM
PRINTF MACRO MSG
MOV AH,09H
LEA DX,MSG
INT 21H
ENDM
.DATA
MSG1 DB "ENTER A CHARACTER:",10,13,"$"
MSG2 DB "ASCII VALUE--?$" ; no 10,13 for this
X DB 12
Y DB 34
.CODE
MOV AX,@DATA
MOV DS,AX
PRINTF MSG1
GETCHAR
MOV BH,AL ; taking a copy of al into bh and bl
MOV BL,AL
AND BL,0FH ; extracting lower nibble
CMP BL,0AH
JL L1
ADD BL,07H
L1: ADD BL,30H ; converting to ascii value
AND BH,0F0H ; extracting upper nibble
MOV CL,04
SHR BH,CL
CMP BH,0AH
JL L2
ADD BH,07H
L2:ADD BH,30H ; converting to ascii
PUSH BX ; save bh, bl values
CALL CLS ; calling the clear screen procedure
MOV DH,X
MOV DL,Y ; setting the cursor
MOV AH,02H
INT 10H ; its 10h not 21h (don’t confuse)
PRINTF MSG2
POP BX ; get saved value
PUTCHAR BH ; print BH & BL
PUTCHAR BL
MOV AH,4CH ; terminate
INT 21H
CLS PROC NEAR
MOV AH,0FH ; get the current mode
INT 10H
MOV AH,00H ; clear that current mode
INT 10H
RET
CLS ENDP
END
4.Write an assembly language program to reverse a given string and check whether it
is a palindrome or not
Code:
DATA SEGMENT
MSG1 DB 10,13,'ENTER ANY STRING :- $'
MSG2 DB 10,13,'ENTERED STRING IS :- $'
MSG3 DB 10,13,'LENGTH OF STRING IS :- $'
MSG4 DB 10,13,'NO, GIVEN STRING IS NOT A PALINDROME $'
MSG5 DB 10,13,'THE GIVEN STRING IS A PALINDROME $'
MSG6 DB 10,13,'REVERSE OF ENTERED STRING IS :- $'
P1 LABEL BYTE
M1 DB 0FFH
L1 DB ?
P11 DB 0FFH DUP ('$')
P22 DB 0FFH DUP ('$')
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
LEA DX,P1
MOV AH,0AH
INT 21H
DISPLAY MSG2
DISPLAY P11
DISPLAY MSG3
MOV DL,L1
ADD DL,30H
MOV AH,2
INT 21H
DISPLAY MSG6
LEA SI,P11
LEA DI,P22
MOV DL,L1
DEC DL
MOV DH,0
ADD SI,DX
MOV CL,L1
MOV CH,0
REVERSE:
MOV AL,[SI]
MOV [DI],AL
INC DI
DEC SI
LOOP REVERSE
DISPLAY P22
LEA SI,P11
LEA DI,P22
MOV CL,L1
MOV CH,0
CHECK:
MOV AL,[SI]
CMP [DI],AL
JNE NOTPALIN
INC DI
INC SI
LOOP CHECK
DISPLAY MSG5
JMP EXIT
NOTPALIN:
DISPLAY MSG4
EXIT: MOV AH,4CH
INT 21H
CODE ENDS
END START
5.Write an assembly language program to find out whether a given sub-string is present or
not in a main string of characters.
Code:
DATA SEGMENT
STR DB 'AXYBCSDEF$'
SUBSTR DB 'BCS$'
LEN1 DB 0
LEN2 DB 0
MSG1 DB 10,13,'STRING IS : $'
MSG2 DB 10,13,'SUBSTRING IS : $'
MSG3 DB 10,13,'SUBSTRING IS FOUND AT POSITION : $'
POS DB -1
RTN DB '-1$'
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
DISPLAY STR
DISPLAY MSG2
DISPLAY SUBSTR
LEA SI,STR
NXT1:
CMP [SI],'$'
JE DONE1
INC LEN1
INC SI
JMP NXT1
DONE1:
LEA DI,SUBSTR
NXT2:
CMP [DI],'$'
JE DONE2
INC LEN2
INC DI
JMP NXT2
DONE2:
DISPLAY MSG3
LEA SI,STR
MOV AL,LEN1
SUB AL,LEN2
MOV CL,AL
MOV CH,0
FIRST:
INC POS
MOV AL,[SI]
CMP AL,SUBSTR[0]
JE CMPR
INC SI
LOOP FIRST
CMPR: INC SI
MOV AL,[SI]
CMP AL,SUBSTR[1]
JNE NOTEQUAL
INC SI
MOV AL,[SI]
CMP AL,SUBSTR[2]
JE EQUAL
NOTEQUAL:
MOV POS,-1
DISPLAY RTN
JMP EXIT
EQUAL:
MOV DL,POS
ADD DL,30H
MOV AH,2
INT 21H
EXIT: MOV AH,4CH
INT 21H
CODE ENDS
END START