0% found this document useful (0 votes)
26 views24 pages

EEE 3104 Codes Only

The document contains a series of assembly language programs designed to perform various tasks such as summing numbers, checking if a number is odd/even/prime, sorting numbers, finding the largest number, displaying ASCII characters, reading and reversing a binary number, and reversing a string to check for palindromes. Each program includes macros for reading input and printing strings, along with data segments for storing variables and results. The programs utilize DOS interrupts for input/output operations and demonstrate fundamental programming concepts in assembly language.

Uploaded by

Shakib Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views24 pages

EEE 3104 Codes Only

The document contains a series of assembly language programs designed to perform various tasks such as summing numbers, checking if a number is odd/even/prime, sorting numbers, finding the largest number, displaying ASCII characters, reading and reversing a binary number, and reversing a string to check for palindromes. Each program includes macros for reading input and printing strings, along with data segments for storing variables and results. The programs utilize DOS interrupts for input/output operations and demonstrate fundamental programming concepts in assembly language.

Uploaded by

Shakib Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Exp-01

Write and execute an assembly language program to sum N different numbers.


All N numbers should be read interactively from the keyboard.

readnum macro num

push ax
push bx
mov ah, 01h ; get char from keyboard
int 21h ; call DOS service
sub al, '0' ; ASCII num to decimal
mov bh, 0ah
mul bh ; multiply by 10
mov num, al ; store first digit in NUM
mov ah, 01h ; get char from keyboard
int 21h ; call DOS service
sub al, '0' ; ASCII num to decimal
add num, al ; add second digit
pop bx
pop bx

endm

printstring macro msg ; printstring- the user defined macro


; msg- macro argument

mov ah, 09h ; display string function


mov dx, offset msg ; initialize dx to msg
int 21h ; call DOS service

endm

_DATA segment ; _DATA- name of user defined segment

cr equ 0dh ; ASCII code for carriage return


lf equ 0ah ; ASCII code for line feed
msg1 db 'How many numbers <XX>: ', '$'
msg2 db cr, lf, 'Enter number <XX>: ', '$'
msg3 db cr, lf, 'Sum: ', '$'
ntable db 100 dup(0)
num db ?
temp db ?
result db 20 dup('$')

_DATA ends

_CODE segment

assume cs: _CODE, ds: _DATA ; initialize CS, DS

start:
mov ax, _DATA ; initialize DS to the data segment
mov ds, ax
printstring msg1
readnum num ; read count
mov si, offset ntable ; SI = table address
mov ch, 00
mov cl, num ; CX = num = number of data

nextread:

printstring msg2

readnum temp ; read the number


mov al, temp
mov [si], al ; store number to the ntable
inc si
loop nextread

mov si, offset ntable


mov ah, 00 ; AX = ntable[0]
mov al, [si]
mov cl, 01
nextchk: inc si ; increment index to point next number in ntable
cmp cl, num ; check whether all numbers are added
je nomore

mov bh, 00
mov bl, [si]
add ax, bx ; add number with previous sum
inc cl
jmp nextchk

nomore:

mov si, offset result


call hex2asc
printstring msg3
printstring result
mov ah, 4ch ; program terminate function
mov al, 00h ; return code for error level setting
int 21h ; call DOS service
; Function to convert Hexadecimal number to ASCII
string
; AX – input number
; SI – pointer to result storage area

hex2asc proc near

push ax ; save registers


push bx
push cx
push dx
push si
mov cx, 00h ; counter for intermediate data pushed
mov bx, 0ah ; load 10 in BL

rpt1: mov dx, 00

div bx ; divide num by 10


add dl, '0' ; convert remainder to ASCII
push dx ; store asci digit on to the stack
inc cx ; update counter
cmp ax, 0ah ; is num less than or equal to 10
jge rpt1 ; if yes, perform conversion

add al, '0' ; convert last digit to ASCII


mov [si], al ; store last digit

rpt2:
pop ax ; pop data
inc si ; advance result string pointer
mov [si], al ; store in result
loop rpt2 ; are all data popped out, if not repeat

inc si
mov al, '$'
mov [si], al ; append end of string
pop si ; restore the registers
pop dx
pop cx
pop bx
pop ax
ret

hex2asc endp

_CODE ends ; end of segment

end start ; end of program

Exp-02
Write and execute an assembly language program that reads a number and
checks whether it is odd or even or prime.

readnum macro num

mov ah, 01h


int 21h
sub al, '0'
mov bh, 0ah
mul bh

mov num, al
mov ah, 01h
int 21h
sub al, '0'
add num, al

endm

printstring macro msg


mov ah, 09h
mov dx, offset msg
int 21h

endm

_DATA segment

cr equ 0dh
lf equ 0ah
msg1 db 'Enter number <XX>: ', '$'
msg2 db cr, lf, 'Even number.', '$'
msg3 db cr, lf, 'Odd number.', '$'
msg4 db cr, lf, 'Prime number.', '$'
msg5 db cr, lf, 'Not prime number.', '$'
num db ?

_DATA ends

_CODE segment

assume cs: _CODE, ds: _DATA

start: mov ax, _DATA

mov ds, ax
printstring msg1
readnum num
mov ah, 00
mov al, num
mov bl, 02
div bl
mov bh, 00
mov bl, al
cmp ah, 00
je evennum
printstring msg3
jmp checkprime

evennum: printstring msg2

checkprime:

and dx, 00
mov ah, 00
mov al, num
cmp al, 01
jle notprime
cmp al, 02
je isprime
mov ch, 00
mov cl, 02

chkprm2:

div cx
cmp dx, 00
je notprime
and dx, 00
mov ah, 00
mov al, num
inc cx
cmp bx, cx
jge chkprm2

isprime:

printstring msg4
jmp skip

notprime:

printstring msg5

skip:

mov ah, 4ch


mov al, 00h
int 21h

_CODE ends

end start

Exp-03
Write and execute an ALP that reads N numbers from the keyboard and sorts
these N numbers in ascending/descending order.

readnum macro num


push ax
push bx
mov ah, 01h
int 21h
sub al,'0'
mov bh, 0ah
mul bh
mov num, al
mov ah, 01h
int 21h
sub al,'0'
add num, al
pop bx
pop ax
endm

printstring macro msg


mov ah, 09h
mov dx, offset msg
int 21h
endm

_DATA segment
cr equ 0dh
lf equ 0ah
msg1 db 'Number of elements <XX>: $'
msg2 db cr, lf, 'Enter element <XX>: $'
msg3 db cr, lf, 'Elements in ascending order...$'
count db ?
tabl db 20 dup(0)
tnum db ?
resdisp db 4 dup(0)
_DATA ends

_CODE segment
assume cs: _CODE, ds: _DATA
start: mov ax, _DATA
mov ds, ax
printstring msg1
readnum count
mov ch, 00h
mov cl, count
mov bx, 01

rdnxt:
printstring msg2
readnum tnum
mov al, tnum
mov tabl[bx], al
inc bx
loop rdnxt

mov ch, 00
mov cl, count
cmp cx,01
je done
nextpass:
mov dl, 00
mov bx, 01
nextj:
mov al, tabl[bx]
mov ah, tabl[bx+1]
cmp al, ah
jle skip
mov dl,01
mov tabl[bx], ah
mov tabl[bx+1], al

skip:
inc bx
cmp bx, cx
jl nextj
dec cx
jz done
cmp dl, 01
je nextpass

done:
mov ch, 00h
mov cl, count
mov bx, 01
mov si, offset resdisp
printstring msg3
prnxt:
mov ah, 00
mov al, tabl[bx]
call hex2asc

printstring resdisp
inc bx
loop prnxt

mov ah, 4ch


mov al, 00h
int 21h

hex2asc proc near


push ax
push bx
push cx
push dx
push si
mov [si],cr
inc si
mov [si],lf
inc si

mov cx, 00h


mov bx, 0ah
rpt1: mov dx, 00
div bx
add dl,'0'
push dx
inc cx
cmp ax, 0ah
jge rpt1
add al,'0'
mov [si], al

rpt2: pop ax
inc si
mov [si], al
loop rpt2
inc si
mov al,'$'
mov [si], al
pop si
pop dx
pop cx
pop bx
pop ax
ret
hex2asc endp
_CODE ends
end start

Exp-04
Write and execute an assembly language program to calculate the largest number
from N numbers. All N numbers should be read interactively from the keyboard.

readnum macro num


push ax
push bx

mov ah, 01h


int 21h
sub al, '0'
mov bh, 0ah
mul bh
mov num, al
mov ah, 01h
int 21h
sub al, '0'
add num, al

pop bx
pop ax

endm

printstring macro msg

mov ah, 09h


mov dx, offset msg
int 21h

endm

_DATA segment

cr equ 0dh
lf equ 0ah
msg1 db 'How many numbers <XX>: $'
msg2 db cr, lf, 'Enter number: $'
msg3 db cr, lf, 'Largest number is: $'
count db ?
tnum db ?
tabl db 100 dup(0)
result db 4 dup('$')

_DATA ends

_CODE segment

assume cs:_CODE, ds:_DATA

start:

mov ax, _DATA


mov ds, ax
printstring msg1
readnum count
mov ch, 00
mov cl, count
mov bx, 00

readnext:

printstring msg2
readnum tnum
mov al, tnum
mov tabl[bx], al
inc bx
loop readnext

mov ch, 00
mov cl, count
mov bx, 00
mov dx, 00

checknext:

mov al, tabl[bx]


cmp dl, al
jge skip
mov dl, al

skip:

inc bx
loop checknext
mov ax, dx
mov si, offset result
call hex2asc

printstring msg3
printstring result
mov ah, 4ch
mov al, 00h
int 21h

hex2asc proc near

push ax
push bx
push cx
push dx
push si
mov cx, 00h
mov bx, 0ah

rpt1:

mov dx, 00
div bx
add dl, '0'
push dx
inc cx
cmp ax, 0ah
jge rpt1

add al, '0'


mov [si], al

rpt2:

pop ax
inc si
mov [si], al
loop rpt2

inc si
mov al, '$'
mov [si], al
pop si
pop dx
pop cx
pop bx
pop ax
ret

hex2asc endp

_CODE ends

end start

Exp-05
Write and execute an ALP that displays any 10 decimal numbers and its
corresponding ASCII characters.

printstring macro msg

mov ah, 09h


mov dx, offset msg
int 21h

endm

_DATA segment

cr equ 0dh
lf equ 0ah
tab equ 09h
msg1 db 'ASCII table set display...$'
msg2 db cr, lf, 'Decimal', tab, 'Character$'
newline db cr, lf, '$'
asc_char db 48
result db 20 dup('$')

_DATA ends

_CODE segment

assume cs: _CODE, ds: _DATA ; initialize CS, DS

start:

mov ax, _DATA


mov ds, ax
printstring msg1
printstring msg2
mov cx, 10

nextchar:

mov ah, 00
mov al, asc_char
mov si, offset result
call hex2asc

printstring newline
printstring result

mov ah, 02
mov dl, tab
int 21h

mov ah, 02
mov dl, asc_char
int 21h

inc asc_char
loop nextchar

mov ah, 4ch


mov al, 00h
int 21h

hex2asc proc near

push ax
push bx
push cx
push dx
push si

mov cx, 00h


mov bx, 0ah

rpt1:

mov dx, 00
div bx
add dl, '0'
push dx
inc cx
cmp ax, 0ah
jge rpt1
add al, '0'
mov [si], al

rpt2:
pop ax
inc si
mov [si], al
loop rpt2

inc si
mov al, '$'
mov [si], al
pop si
pop dx
pop cx
pop bx
pop ax
ret

hex2asc endp

_CODE ends

end start

Exp-06
Write and execute an ALP to read an 8-bit binary number from the keyboard,
store it in BX and display the number in reverse order in the next line.

printstring macro msg

mov ah,09h
mov dx,offset msg
int 21h

endm

_DATA segment

cr equ 0dh
lf equ 0ah
msg1 db 'Enter the 8-bit number:','$'
msg2 db cr,lf,'Binary number in reverse order:','$'
msg3 db cr,lf,'Illegal Input','$'

_Data ends

_CODE segment

assume cs:_CODE,ds:_DATA

start:

mov ax,_DATA
mov ds,ax
printstring msg1
xor bx,bx
mov cx,8
mov ah,01h

input:

int 21h
cmp al,'0'
je continue
cmp al,'1'
jne errormsg

continue:

sub al,'0'
shl bx,1
or bl,al
loop input

printstring msg2
mov cx,8
mov ah,02h

output:

sar bx,1
jnc zero
mov dl,31h
jmp display

zero:

mov dl,30h

display:

int 21h
loop output
jmp finish

errormsg:

printstring msg3

finish:

mov ah,4ch
mov al,00h
int 21h

_CODE ends

end start
Exp-07
Write and execute an Assembly Language Program (ALP) to 8086 processor to
reverse the given string and verify whether it is a palindrome.

printstring macro msg

mov ah,09h
mov dx,offset msg
int 21h

endm

_DATA segment

dos equ 21h


cr equ 0dh
lf equ 0ah
buff db 80 dup(0)
revbuff db 80 dup(0)
strlen dw ?
msg1 db 'Enter the string: ', '$'
msg2 db cr, lf, 'Reverse of string: ', '$'
msg3 db cr, lf, 'Input string is a Palindrome.$'
msg4 db cr, lf, 'Input string is NOT a Palindrome.$'

_DATA ends

_CODE segment

assume cs:_CODE,ds:_DATA

start:

mov ax,_DATA
mov ds,ax
printstring msg1
mov si, offset buff
mov bx, 00

rdchar:

mov ah, 01h


int dos
mov [si], al
inc si
inc bx
cmp al, cr
jne rdchar

dec bx
mov strlen, bx
mov si, offset buff
add si, bx
mov di, offset revbuff
mov cx, bx

nxtchar1:

dec si
mov al, [si]
mov [di], al
inc di
loop nxtchar1

mov al, '$'


mov [di], al
printstring msg2
printstring revbuff

mov si, offset buff


mov di, offset revbuff
mov cx, strlen

nxtchar2:

mov al, [si]


cmp al, [di]
jne notpali

inc si
inc di
loop nxtchar2

palindrome:

printstring msg3
jmp skip

notpali:

printstring msg4

skip:

mov ah, 4ch


mov al, 00h
int dos

_CODE ends

end start

Exp-08
Write and execute an ALP to read an 8-bit binary number from the keyboard,
save it in DH register, convert it in Hexadecimal code and display the result in
the next line.

printstring macro msg

mov ah, 09h


mov dx, offset msg
int 21h

endm

_DATA segment

cr equ 0dh
lf equ 0ah
msg1 db 'Enter the 8-bit binary number: ', '$'
msg2 db cr, lf, 'Hexadecimal code of the binary number: ', '$'
msg3 db cr, lf, 'Illegal input.', '$'

_DATA ends

_CODE segment

assume cs:_CODE, ds:_DATA

start:

mov ax, _DATA


mov ds, ax
printstring msg1
xor bx, bx
mov cx, 8
mov ah, 01h

input:

int 21h
cmp al, '0'
je continue
cmp al, '1'
jne errormsg

continue:

sub al, '0'


shl bh, 1
or bh, al
loop input

done:

mov cx, 00h


printstring msg2
mov ah, 02h

output:

cmp cx, 2
je finish
inc cx
mov dl, bh
push cx
mov cx, 4

shift1:

shr dl, 1
loop shift1
pop cx
cmp dl, 0ah
jl digit
add dl, '7'
jmp next

digit:

add dl, '0'

next:

int 21h
push cx
mov cx, 4

shift2:

rol bx, 1
loop shift2
pop cx
jmp output

errormsg:

printstring msg3

finish:

mov ah, 4ch


mov al, 00h
int 21h

_CODE ends

end start
Exp-09
Write and execute an ALP to 8086 processor to add, subtract and multiply two
16 bit unsigned numbers. Store the result in extra segment.

printstring macro msg


mov ah, 09h
mov dx, offset msg
int 21h
endm

_DATA segment
cr equ 0dh
lf equ 0ah

msg1 db 'Addition: $'


msg2 db cr, lf, 'Subtraction: $'
msg3 db cr, lf, 'Multiplication: $'

num1 dw 6532h
num2 dw 3452h
_DATA ends

_EXTRA segment
sum dw 0
subt dw 0
multl dw 0
multh dw 0
_EXTRA ends

_CODE segment
assume cs:_CODE, ds:_DATA, es:_EXTRA
start:
mov ax, _DATA
mov ds, ax
mov ax, _EXTRA
mov es, ax

mov ax, num1


add ax, num2
mov es:sum, ax

mov ax, num1


sub ax, num2
mov es:subt, ax

mov ax, num1


mul num2
mov es:multl, ax
mov es:multh, dx

printstring msg1
mov bx, es:sum
call printhex
mov bh, bl
call printhex

printstring msg2
mov bx, es:subt
call printhex
mov bh, bl
call printhex

printstring msg3
mov bx, es:multh
call printhex
mov bh, bl
call printhex

mov bx, es:multl


call printhex
mov bh, bl
call printhex

mov ah, 4ch


mov al, 00h
int 21h

;PROC takes value in BH and prints its HEX value


proc printhex near
push ax
push bx
push cx
push dx

mov ah, 02h


mov cx, 2

mov dl, bh
shr dl, 4
hexout:
cmp dl, 10
jge alt
add dl, '0'
jmp fin

alt:
add dl, '7'

fin:
int 21h

mov dl, bh
and dl, 0fh
loop hexout

pop dx
pop cx
pop bx
pop ax
ret
_CODE ends
end start

Exp-10
Write and execute an ALP to 8086 processor to find the length of a given string.

printstring macro msg


mov ah,09h
mov dx,offset msg
int 21h
endm

DATA segment
cr equ 0dh
lf equ 0ah
msg1 db 'Enter string: $'
msg2 db cr,lf,'String length= $'
strlen dw 0
result dw 20 dup(0)
DATA ends

CODE segment
assume cs:CODE,ds:DATA

start:
mov ax,DATA
mov ds,ax
printstring msg1
mov ah,01h

string:
int 21h
cmp al,cr
je finish
inc strlen
loop string

finish:
printstring msg2
mov si,offset result
mov ax,00
mov ax,strlen
call hex2asc
printstring result
mov ah,4ch
mov ah,00h
int 21h

hex2asc proc near


push ax
push bx
push cx
push dx
push si

mov cx,00
mov bx,0ah

rpt:
mov dx,00
div bx
add dl,'0'
push dx
inc cx
cmp ax,0ah
jge rpt
add al,'0'
mov [si],al

rpt2:
pop ax
inc si
mov [si],al
loop rpt2

inc si
mov al,'$'
mov [si],al
pop si
pop dx
pop cx
pop bx
pop ax
ret

hex2asc endp
CODE ends
end start

Exp-11
Write and execute an ALP to 8086 processor to verify the password.

printstring macro msg


mov ah,09h
mov dx,offset msg
int 21h
endm
DATA segment
cr equ 0dh
lf equ 0ah
msg1 db 'Enter password(XXXXXXX): $'
msg2 db cr,lf,'Incorrect Password!$'
msg3 db cr,lf,'Correct$'
pass dw 'riham4321$'
count db 7
given dw 100 dup(0)
DATA ends

CODE segment
assume cs:CODE,ds:DATA
start: mov ax,DATA
mov ds,ax
mov ch,00
mov cl,count
printstring msg1
mov si,offset given

read:
mov ah,07
int 21h
cmp al,cr
je next
mov [si],al
mov ah,02
mov dl,'*'
int 21h

inc si
jmp read

next: mov al,'$'


mov [si],al
mov ch,00
mov cl,count
mov si,offset pass
mov di,offset given

compare:
mov al,[si]
mov bl,[di]
cmp al,bl
jne inco
inc si
inc di
loop compare
jmp cor

inco:
printstring msg2
jmp finish

cor:
printstring msg3

finish:
mov ah,4ch
mov ah,00h
int 21h

CODE ends
end start

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy