SM Lab 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Exercise 8: Write a program to display a "?

", read two capital letters,


and display them on the next line in alphabetical order.

Solution:
.model small
.stack 100h
.code
main proc

mov ah,2
mov dl,'?'
int 21h

mov ah,1
int 21h
mov bl,al

int 21h
mov cl,al

mov ah,2
mov dl,10
int 21h
mov dl,13
int 21h

cmp bl,cl
jg next

mov dl,bl
int 21h
mov dl,cl
int 21h
jmp exit

next:
mov dl,cl
int 21h
mov dl,bl
int 21h

exit:
mov ah,4ch
int 21h
main endp
end main
Exercise 9: Write a program to display the extended ASCII characters
(ASCII codes 80h to FFh). Display 10 characters per line, separated by
blanks. Stop after the extended characters have been displayed once.

Solution:
.model small
.stack 100h
.data
.code
main proc

mov cx,128
mov bh,80h
mov bl,0
top:
mov ah,2
mov dl,bh
int 21h
mov dl,32
int 21h
inc bh
inc bl
cmp bl,10
je line
back:
loop top
cmp cx,0
je exit
line:
mov ah,2
mov dl,10
int 21h
mov dl,13
int 21h
mov bl,0
jmp back:
exit:
mov ah,4ch
int 21h
main endp
end main

Exercise 10: Write a program that will prompt the user to enter a hex
digit character ("0"· ... "9" or "A" ... "F"), display it on the next line in
decimal, and ask the user if he or she wants to do it again.
If the user types "y" or "Y", the program repeats; If the user types
anything else, the program terminates. If the user enters an illegal
character, prompt the user to try again.

Solution:

.model small
.stack 100h
.data
msg1 db 10,13,'Enter a hax digit $'
msg2 db 10,13,'In decimal is it $'
msg3 db 10,13,'Do you want to do it again $'
msg4 db 10,13,'Illegal character. enter 0-9 or A-F $'
.code
main proc

mov ax,@data
mov ds,ax
again:
mov ah,9
lea dx,msg1
int 21h
mov ah,1
int 21h
mov bl,al
jmp go

go:
cmp bl,'9'
jg hex
je num
jl num

hex:
cmp bl,'F'
jg illegal
mov ah,9
lea dx,msg2
int 21h
mov dl,49
mov ah,2
int 21h
mov ah,2
sub bl,17
mov dl,bl
int 21h
jmp inp

inp:
mov ah,9
lea dx,msg3
int 21h
mov ah,1
int 21h
mov cl,al
cmp cl,'Y'
je again
cmp cl,'y'
je again
jmp exit
num:
cmp bl,'0'
je illegal
lea dx,msg2
int 21h
mov ah,9
int 21h

mov dl,bl
mov ah,2
int 21h
jmp inp

illegal:
lea dx,msg4
mov ah,9
int 21h
mov ah,1
int 21h
mov bl,al
jmp go

exit:
mov ah,4ch
int 21h
main endp
end main

Exercise 11: Do programming execise 10, except that if the user

fails to enter a hex-digit character in three tries, display a message

and terminate the program.

Solution:

.model small
.stack 100h
.data
msg1 db 10,13,'ENTER A HEX DIGIT:$'
msg2 db 10,13,'IN DECIMAL IS IT:$'
msg3 db 10,13,'DO YOU WANT TO DO IT AGAIN?$'
msg4 db 10,13,'ILLEGAL CHARACTER- ENTER 0-9 OR A-F:$'
msg5 db 10,13,'Too Many Try$'
.code
main proc
mov ax,@data
mov ds,ax

again:
mov cx,0
lea dx,msg1
mov ah,9
int 21h
mov ah,1
int 21h
mov bl,al
jmp go

go:
cmp bl,'9'
ja hex
jb num
je num

hex:
cmp bl,'F'
ja illegal
lea dx,msg2
mov ah,9
int 21h
mov dl,49d
mov ah,2
int 21h
sub bl,17d
mov dl,bl
mov ah,2
int 21h
jmp inp

inp:
lea dx,msg3
mov ah,9
int 21h
mov ah,1
int 21h
mov cl,al
cmp cl,'y'
je again
cmp cl,'Y'
je again
jmp exit

num:
cmp bl,'0'
jb illegal
lea dx,msg2
mov ah,9
int 21h
mov dl,bl
mov ah,2
int 21h
jmp inp

illegal:
inc cx
cmp cx,3
je i2
lea dx,msg4
mov ah,9
int 21h
mov ah,1
int 21h
mov bl,al
jmp go

i2:
lea dx,msg5
mov ah,9
int 21h
jmp exit

exit:
mov ah,4ch
int 21h
main endp
end main

EXERCISE - 12: Write a program that reads a string of capital letters,

ending with a carriage return, and displays the longest sequence of

consecutive alphabetically increasing capital letters read.

Solution:

.model small
.stack 100h
.data
a db 'ENTER A STRING OF CAPITAL LETTERS : $'
b db 0ah,0dh,'THE LONGEST CONSECUTIVE INCREASING STRING IS : $'

.code
main proc
mov ax,@data
mov ds,ax
mov ah,9
lea dx,a
int 21h

mov ah,1
int 21h
mov ch,1
mov cl,1
mov bl,al
mov dh,al
mov bh,al

second:
mov ah,1
int 21h
cmp al,0dh
je final

inc bl

cmp bl,al
je equal
jmp check
equal:
inc cl
mov bl,al
jmp second
check:
cmp cl,ch
jg increase
mov bh,al
mov bl,al
mov cl,1
jmp second
increase:
mov dh,bh
mov bh,al
mov ch,cl
mov cl,1
mov bl,al
jmp second

increaseagain:
mov ch,cl
mov dh,bh
jmp end
final:
cmp ch,cl
jl increaseagain

end:

mov ah,2
mov dl,10
int 21h

mov bh,dh

lea dx,b
mov ah,9
int 21h
top:
mov ah,2
mov dl,bh
int 21h
inc bh
dec ch
cmp ch,0
je exit
jmp top
exit:
mov ah,4ch
int 21h
main endp
end main

You might also like