0% found this document useful (0 votes)
7 views4 pages

Micro Lab

This document contains assembly code for a program that reads, sorts, and displays a list of three-digit numbers. It includes macros for reading numbers from the keyboard, printing strings, and converting numbers to ASCII for display. The program uses a bubble sort algorithm to arrange the numbers in descending order before outputting the results.

Uploaded by

Ashin Roy
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)
7 views4 pages

Micro Lab

This document contains assembly code for a program that reads, sorts, and displays a list of three-digit numbers. It includes macros for reading numbers from the keyboard, printing strings, and converting numbers to ASCII for display. The program uses a bubble sort algorithm to arrange the numbers in descending order before outputting the results.

Uploaded by

Ashin Roy
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/ 4

; Sorting : 3 digit number

; The location of this template is c:\emu8086\inc\0_com_template.txt

readnum macro num


; Macro to read a 3-digit number from the keyboard
push ax ; Save AX register
push bx ; Save BX register

mov ah, 01h ; Function to read a character from the keyboard


int 21h ; DOS interrupt
sub al, ’0’ ; Convert ASCII to decimal
mov bh, 64h
mov ah,00h ; Multiply by 100 for hundreds place
mul bh
mov num, ax ; Store hundreds place in num

mov ah, 01h ; Read second character


int 21h
sub al, ’0’ ; Convert ASCII to decimal
mov bh, 0ah
mov ah,00h ; Multiply by 10 for tens place
mul bh
add num, ax ; Add tens place

mov ah, 01h ; Read third character


int 21h
sub al, ’0’
mov ah,00h ; Convert ASCII to decimal
add num, ax ; Add units place

pop bx ; Restore BX register


pop ax ; Restore AX register
endm

printstring macro msg


; Macro to print a string
mov ah, 09h ; Function to display string
lea dx, msg ; Load the address of the message
int 21h ; DOS interrupt
endm

_DATA segment
cr equ 0dh ; Carriage return
lf equ 0ah ; Line feed
msg1 db ’Number of elements <XX>: $’ ; Message to prompt for number of elements
msg2 db cr, lf, ’Enter element <XX>: $’ ; Message to prompt for each element
msg3 db cr, lf, ’Elements in descending order...$’ ; Message for sorted output
count db ? ; Variable to store the count of numbers
tabl dw 20 dup(0) ; Array to store the numbers (max 20, word-sized for 3 digits)
tnum dw ? ; Temporary variable for input
resdisp db 6 dup(0) ; Buffer for displaying results
_DATA ends
_CODE segment
assume cs: _CODE, ds: _DATA

start:
; Initialize data segment
mov ax, _DATA
mov ds, ax

; Prompt user for the number of elements

mov count,05h

; Read N numbers from the keyboard


mov cx,00h
mov cl, count
mov bx, 0000h ; Start storing numbers at tabl[0]
rdnxt:
printstring msg2 ; Prompt for next number
readnum tnum ; Read the number (ensure proper size override)
mov ax, tnum
; Load the value of tnum into AX
mov tabl[bx], ax ; Store the number in tabl
add bx, 2
loop rdnxt ; Move to next index (word size)
; Repeat for all numbers

; Sort the numbers in ascending order using bubble sort

dec count
mov bx, 0000h
mov ch, count

back2: mov cl,count


mov si , offset tabl

back1:
mov ax,0000h
mov bx,0000h
mov ax, [si]
mov bx, [si+2]
cmp ax, bx
jge skip ; NOTICE : Jodi assending order e sorting korte chaw tahole ei jaygay ’jle skip’ likhlei hobe

mov [si], bx ; Swap elements


mov [si+2], ax
skip:
add si,02h
dec cl
jnz back1 ; Move to next pair

dec ch
jnz back2

inc count
mov cx,00h
mov cl, count
mov bx, 0 ; Start displaying from tabl[0]
mov si, offset resdisp
printstring msg3
prnxt:
mov ax, tabl[bx] ; Get the number
call hex2asc ; Convert to ASCII and store in resdisp
printstring resdisp ; Display the number
add bx, 2 ; Move to next number
loop prnxt ; Repeat for all numbers

; Exit program
mov ah, 4ch
mov al, 00h
int 21h

hex2asc proc near


; Convert a single byte in AL to an ASCII string
push ax
push bx
push cx
push dx
push si
mov [si], cr ; Add carriage return
inc si
mov [si], lf ; Add line feed
inc si
mov cx, 00h
mov bx, 0ah ; Base 10 for decimal conversion
rpt1:
mov dx, 00
div bx ; Divide by 10
add dx, ’0’ ; Convert remainder to ASCII
push dx ; Store digit on stack
inc cx
cmp ax, 0ah ; Check if more digits remain
jge rpt1
add al, ’0’
mov [si], al ; Store the first digit
rpt2:
pop ax
inc si
mov [si], al ; Store remaining digits
loop rpt2
inc si
mov al, ’$’ ; Terminate the string
mov [si], al
pop si
pop dx
pop cx
pop bx
pop ax
ret
hex2asc endp

int 03h
_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