0% found this document useful (0 votes)
11 views

Assembly_Lab_fianl (1)

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

Assembly_Lab_fianl (1)

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

printing from 0 to 9

/////////////////////////////////////////////////////////
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0AH, 0DH, 'The numbers from 0 to 9 are: $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH, 09H
LEA DX, MSG
INT 21H
MOV CX, 10
MOV AL, '0'
PRINT_LOOP:
MOV AH, 02H
MOV DL, AL
INT 21H
INC AL
LOOP PRINT_LOOP
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////
Largest among 2 numbers
////////////////////////////////////////////////////////////////////////////////
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0AH, 0DH, 'The smallest number is: '
MSG1 DB 0AH, 0DH, 'The largest number is: '

num1 db 1
num2 db 2
.CODE
MAIN PROC
mov ax, @data
mov ds, ax

mov al, num1 ; Load num1 into AL


mov bl, num2 ; Load num2 into BL

cmp al, bl ; Compare AL (num1) and BL (num2)


jl con1 ; Jump to con1 if AL < BL
jg print ; Jump to print if AL > BL

con1:
xchg al,bl
print:

add al, '0' ; Convert AL to ASCII


mov ah, 2 ; Function to print character
mov dl, al ; Move the ASCII character to DL
int 21h ; Print the character

add bl, '0' ; Convert BL to ASCII


mov dl, bl ; Move the ASCII character to DL
mov ah, 2 ; Function to print character
int 21h ; Print the character

end:
mov ah, 4ch ; Terminate program
int 21h

MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////////
///
smallest middle largest
///////////////////////////////////////////////////////////////////////////////////
//
.MODEL SMALL
.STACK 100H
.DATA
num1 DB 3 ; Example single-digit numbers
num2 DB 5
num3 DB 2
.CODE
MAIN PROC
mov ax, @data
mov ds, ax

mov al, num1 ; Load num1 into AL


mov bl, num2 ; Load num2 into BL
mov cl, num3 ; Load num3 into CL

; Ensure AL contains the smallest value


cmp al, bl
jle continue1
xchg al, bl ; Swap if num1 > num2
continue1:
cmp al, cl
jle continue2
xchg al, cl ; Swap if num1 > num3
continue2:
; Now AL is the smallest, compare BL and CL
cmp bl, cl
jle done_swap
xchg bl, cl ; Swap if num2 > num3

done_swap:
; AL contains smallest, BL contains middle, CL contains largest
add al, '0' ; Convert AL to ASCII
mov dl, al ; Move the ASCII character to DL
mov ah, 2 ; DOS function to print character
int 21h ; Print the character
; Print the middle number
add bl, '0' ; Convert BL to ASCII
mov dl, bl ; Move the ASCII character to DL
mov ah, 2 ; DOS function to print character
int 21h ; Print the character
; Print the largest number
add cl, '0' ; Convert CL to ASCII
mov dl, cl ; Move the ASCII character to DL
mov ah, 2 ; DOS function to print character
int 21h ; Print the character
; Exit program
mov ah, 4ch
int 21h
MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////////
//
integer to word
///////////////////////////////////////////////////////////////////////////////////
/
.MODEL SMALL
.STACK 100H
.DATA
msg DB 0AH, 0DH, 'Enter a single digit (0-9): $'
numbers DB
'Zero$','One$','Two$','Three$','Four$','Five$','Six$','Seven$','Eight$','Nine$'

.CODE
MAIN PROC
; Initialize data segment
mov ax, @data
mov ds, ax

; Prompt user for input


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

; Read a character from the keyboard


mov ah, 01h
int 21h
sub al, '0' ; Convert ASCII to index (0-9)

; Check if input is within the valid range


cmp al, 0
jb exit ; If less than 0, exit
cmp al, 9
ja exit ; If greater than 9, exit

; Load the address of the numbers array


mov si, offset numbers
; Multiply the index by length of each string
mov cx, 5 ; Length of each string (including '$')
mul cl ; AL = AL * CX (index * 5)
add si, ax ; SI points to the correct string

; Print the corresponding word


mov ah, 09h
lea dx, [si] ; Load the address of the string to DX
int 21h ; Print the string

exit:
; Exit program
mov ah, 4ch
int 21h

MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////////
//////////
frequency of a digit
///////////////////////////////////////////////////////////////////////////////////
///////////
.model small
.stack 100h
.data
string db 'stresses$'
msg db 0ah,0dh,'The number of times ''s'' appeared is:$'
.code
main proc
mov ax,@data
mov ds,ax
mov es,ax

lea si,string
mov cx,0
count:
mov al,[si]
cmp al,'$'
je done
cmp al,'s'
jne next
inc cx
next:
inc si
jmp count
done:
mov al,cl
add al,30h
mov ah,2
mov dl,al
int 21h

mov ah,4ch
int 21h
end main
////////////////////////////////////////////////////////////////////////////////
print the nth elemnt in a stack
//////////////////////////////////////////////////////////////////////////////////
.MODEL SMALL
.STACK 100H ; Define a stack segment with 256 bytes of space
.DATA
MSG DB 0AH, 0DH, 'The eighth element on the stack is: $'
NUMBERS DB 8, 7, 6, 5, 4, 3, 2, 1 ; Define 8 single-digit numbers
.CODE
MAIN PROC
MOV AX, @DATA ; Initialize data segment
MOV DS, AX

; Push eight single-digit numbers onto the stack using a loop


MOV CX, 8 ; Set loop counter to 8 (number of elements)
LEA SI, NUMBERS ; Load address of NUMBERS array into SI
PUSH_LOOP:
MOV AL, [SI] ; Load the current number from NUMBERS array
MOV AH, 0 ; Clear AH (we're dealing with single-digit numbers)
PUSH AX ; Push the number onto the stack
INC SI ; Move to the next number in the array
LOOP PUSH_LOOP ; Decrement CX and loop until CX = 0

; Print the message


MOV AH, 9
LEA DX, MSG
INT 21H

; Pop seven values to reach the eighth element using a loop


MOV CX, 7 ; We need to pop 7 numbers to reach the 8th element
POP_LOOP:
POP AX ; Pop the value (we don't need to use it)
LOOP POP_LOOP ; Decrement CX and loop until CX = 0

; Now the 8th element (first pushed number) is on top of the stack
POP AX ; Pop the 8th element into AX
ADD AL, '0' ; Convert the number to ASCII
MOV DL, AL
MOV AH, 2
INT 21H ; Print the character

; Exit the program


MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////////
////////////
delete nth elemnt from the stack
///////////////////////////////////////////////////////////////////////////////////
/////////////
.MODEL SMALL
.STACK 100H
.DATA
; Sample data for demonstration
values DB 1, 2, 3, 4, 5 ; Values to push onto the stack
msg_before DB 0AH, 0DH, 'Stack before deletion: $'
msg_after DB 0AH, 0DH, 'Stack after deletion: $'
newline DB 0AH, 0DH, '$' ; Newline for printing

.CODE
MAIN PROC
mov ax, @data
mov ds, ax

; Push values onto the stack using a loop


mov si, 0 ; Index for values array
mov cx, 5 ; Number of elements to push
push_loop:
mov al, [values + si] ; Load the value from the array
push ax ; Push the value onto the stack
inc si ; Move to the next element
loop push_loop ; Repeat for all elements

; Print stack contents before deletion


mov ah, 9
lea dx, msg_before
int 21h

; Print each element in the stack


mov cx, 5 ; Number of elements to print
print_before:
pop ax ; Pop the top element
add al, '0' ; Convert to ASCII
mov dl, al ; Move the ASCII character to DL
mov ah, 2 ; DOS function to print character
int 21h ; Print the character
; Print newline
mov ah, 9
lea dx, newline
int 21h
loop print_before ; Repeat for all elements

; Push elements back onto the stack


mov si, 0 ; Reset index for values array
mov cx, 5 ; Number of elements to push back
push_back_loop:
mov al, [values + si] ; Load the value from the array
push ax ; Push the value back onto the stack
inc si ; Move to the next element
loop push_back_loop ; Repeat for all elements

; At this point, the stack looks like:


; 5
; 4
; 3 <- This is the 3rd element
; 2
; 1

; Now delete the third element


pop ax ; Pop 5 (top element)
pop bx ; Pop 4 (second element)
pop cx ; Pop 3 (third element, now deleted)
push bx ; Push back 4
push ax ; Push back 5

; Print stack contents after deletion


mov ah, 9
lea dx, msg_after
int 21h

; Print each element in the stack after deletion


mov cx, 4 ; Number of elements remaining to print
print_after:
pop ax ; Pop the top element
add al, '0' ; Convert to ASCII
mov dl, al ; Move the ASCII character to DL
mov ah, 2 ; DOS function to print character
int 21h ; Print the character
; Print newline
mov ah, 9
lea dx, newline
int 21h
loop print_after ; Repeat for remaining elements

; Exit program
mov ax, 4c00h
int 21h
MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////////
//////////////////
dec to binary
///////////////////////////////////////////////////////////////////////////////////
/////////////////
.MODEL SMALL
.STACK 100H
.DATA
Num DB 8 ; Example decimal number to convert
binaryNum DB 8 DUP(0) ; Space for 8 bits binary result
msg DB 0AH, 0DH, 'Binary number is: $'
newline DB 0AH, 0DH, '$' ; Newline for printing

.CODE
MAIN PROC
mov ax, @data
mov ds, ax

; Load the decimal number into AL


mov al,Num
xor cx, cx ; Bit counter
xor di, di ; DI will be used to store binary digits

convert_loop:
; Check if AL is zero
test al, al
jz print_binary ; If zero, go to print

; Divide AL by 2
mov bl, 2 ; Divisor
xor ah, ah ; Clear AH
div bl ; AL = AL / 2, AH = AL % 2 (remainder)

; Store remainder as binary digit


mov binaryNum[di], ah ; Store the remainder (0 or 1)
inc di ; Move to the next position
inc cx ; Increment bit counter
jmp convert_loop ; Repeat until AL is zero

print_binary:
dec di ; Adjust DI to the last digit index
print_loop:
mov al, binaryNum[di] ; Load binary digit
add al, '0' ; Convert to ASCII
mov dl, al ; Move to DL for printing
mov ah, 2 ; DOS function to print character
int 21h ; Print character
dec di ; Move to the previous bit
loop print_loop ; Repeat until all bits printed

; Print newline
mov ah, 9
lea dx, newline
int 21h

; Exit program
mov ax, 4c00h
int 21h

MAIN ENDP
END MAIN
///////////////////////////////////////////////////////////////////////////////////
//////////////////////
binary to dec
///////////////////////////////////////////////////////////////////////////////////
////////////////////

You might also like