Name: Ajit Bhagwan Bedwal Roll No: C-04
Practical N0:01
.MODEL SMALL
.DATA
len DB ?
NEWLINE DB 10, 13, '$'
INPUT DB "ENTER A STRING:
$" STRING DB 20, 20 DUP('$')
OUTPUT DB "Length of string is :$"
.CODE
START:
MOV AX, @data
MOV DS, AX
MOV DX, OFFSET INPUT
MOV AH, 09H
INT 21H
MOV DX, OFFSET STRING
MOV AH, 0AH
INT 21H
MOV CX, 0
MOV SI, OFFSET STRING+1
count_loop:
LODSB
CMP AL, '$'
JE done_counting
INC CX
JMP count_loop
done_counting:
DEC CX
DEC CX
ADD CX, '0'
MOV len, CL
MOV DX, OFFSET OUTPUT
MOV AH, 09H
INT 21H
MOV DL, len
MOV AH, 02H
INT 21H
MOV DX, OFFSET NEWLINE
MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H
END START
Input:
:
Output:
Name: Ajit Bhagwan Bedwal Roll No: C-04
Practical N0:02
Data Segment
msg db 0dh, 0ah,"Please enter the length of the array:$"
msg1 db 0dh, 0ah,"Enter a number:$"
new1 db 0dh, 0ah,"$"
res db 0dh, 0ah,"The maximum is:$"
len db ?
max db ?
Data ends
Code segment
assume CS:Code, DS:Data
Start:
mov ax, Data
mov DS, ax
mov dx, offset msg
mov ah, 09h
int 21h
call Accept
mov len, bl
mov cl, bl
mov ch, 00h
mov di, 1000h
back:
mov dx, offset msg1
mov ah, 09h
int 21h
call Accept
mov [di], bl
inc di
loop back
mov di,
1000h mov al,
[di] mov max,
al inc di
dec cl
Chk:
cmp cl, 0
je DisplayResult
mov al, [di]
cmp al, max
jg UpdateMax
jmp Next
UpdateMax:
mov max, al
Next:
inc di
dec cl
jmp Chk
DisplayResult:
mov dx, offset
res mov ah,09h
int 21h
mov bl, max
call DispNum
mov ah, 4ch
int 21h
Accept proc
mov ah, 01h
int 21h
call
AsciiToHex ret
Accept endp
DispNum proc
mov dl, bl
call HexToAscii
mov ah, 02h
int 21h
ret
DispNum endp
AsciiToHex proc
cmp al, 41h
jc sk
sub al, 07h
sk:
sub al, 30h
mov bl,al
ret
AsciiToHex endp
HexToAscii proc
cmp dl, 0ah
jc sk2
add dl, 07h
sk2:
add dl, 30h
ret
HexToAscii endp
Code ends
end Start?
Input:
Output:
Name: Ajit Bhagwan Bedwal Roll No: C-04
Practical N0:03
.model small
.stack 100h
.data
msg_length db 13,10,"Please enter the length of the array: $"
msg_number db 13,10,"Enter element: $"
positive_count db ?
negative_count db ?
arr db 100 dup(?)
len db ?
positive_msg db 13,10,"Number of positive number: $"
negative_msg db 13,10,"Number of negative number: $"
newline db 13,10,'$'
.code
main proc
mov ax,@data
mov ds,ax
mov dx,offset msg_length
mov ah,09h
int 21h
call read_byte
mov len,al
mov cl,len
mov di,offset arr
input_loop:
mov dx,offset msg_number
mov ah,09h
int 21
call read_byte
mov[di],al
inc di
loop input_loop
mov si,offset arr
mov cl,len
xor bh,bh
xor bl,bl
count_loop:
mov al,[si]
test al,80h
jnz positive
inc bh
jmp continue_count
positive:
inc bl
continue_count:
inc si
loop count_loop
mov dx,offset positive_msg
call print_string
mov dl,bh
call print_num
mov dx,offset newline
call print_string
mov dx,offset negative_msg
call print_string
mov dl,bl
call print_num
mov ah,4ch
int 21h
main endp
read_byte proc
mov ah,01h
int 21h
cmp al,'-'
je read_sign
sub al,'0'
ret
read_sign:
mov ah,01h
int 21h
sub al,'0'
neg al
ret
read_byte endp
print_string proc
mov ah,09h
int 21h
ret
print_string endp
print_num proc
add dl,'0'
mov ah,02h
mov bh,00h
int 21h
ret
print_num endp
end main
Input:
Output:
Name: Ajit Bhagwan Bedwal Roll No: C-04
Practical N0:04
.model small
.stack 100h
.data
msg_menu db 13, 10, "Choose an option:", 13, 10, "a) HEX to BCD", 13, 10, "b) BCD to
HEX", 13, 10, "c) EXIT", 13, 10, "$"
msg_hex db 13, 10, "Enter a 4-digit Hex number: $"
msg_bcd db 13, 10, "Enter a 5-digit BCD number: $"
msg_invalid db 13, 10, "Invalid input! Please try again.", 13, 10, "$"
msg_result db 13, 10, "Result: $"
hex_input db 6, 0, 6 dup('$')
bcd_input db 6, 0, 6 dup('$')
result_buffer db 20 dup('$') ; Increased buffer size for binary output
.code
main proc
mov ax, @data
mov ds, ax
menu_loop:
mov dx, offset msg_menu
call print_string
mov ah, 01h
int 21h
cmp al, 'a'
je hex_to_bcd
cmp al, 'b'
je bcd_to_hex
cmp al, 'c'
je exit_program
mov dx, offset msg_invalid
call print_string
jmp menu_loop
hex_to_bcd:
mov dx, offset msg_hex
call print_string
mov dx, offset hex_input
call read_string
call hex_to_bcd_conversion
mov dx, offset msg_result
call print_string
mov dx, offset result_buffer
call print_string
jmp menu_loop
bcd_to_hex:
mov dx, offset msg_bcd
call print_string
mov dx, offset bcd_input
call read_string
call bcd_to_hex_conversion
mov dx, offset msg_result
call print_string
mov dx, offset result_buffer
call print_string
jmp menu_loop
exit_program:
mov ah, 4Ch
int 21h
main endp
read_string proc
mov ah, 0Ah
int 21h
ret
read_string endp
print_string proc
mov ah, 09h
int 21h
ret
print_string endp
hex_to_bcd_conversion proc
mov si, offset hex_input + 2
mov ax, 0
mov cx, 4
hex_to_bcd_loop:
shl ax, 4
mov dl, [si]
cmp dl, '9'
jbe numeric_digit
sub dl, 7
numeric_digit:
sub dl, '0'
or ax, dx
inc si
loop hex_to_bcd_loop
mov di, offset
result_buffer
mov cx, 16 ; 16 bits in a 16-bit number
binary_convert:
mov dx, ax ; Save the original value
and ax, 1 ; Get the least significant bit
cmp ax, 1
je one_bit
mov al, '0'
jmp store_bit
one_bit:
mov al, '1'
store_bit:
mov [di], al
inc di
mov ax, dx ; Restore the original
value shr ax, 1 ; Shift right to get the
next bit loop binary_convert
mov byte ptr [di], '$'
ret
hex_to_bcd_conversion endp
bcd_to_hex_conversion proc
mov si, offset bcd_input + 2
mov di, offset result_buffer
mov cx, 4
bcd_to_hex_loop:
mov al, [si]
and al, 0Fh
cmp al, 10
jae letter_digit
add al, '0'
jmp store_hex
letter_digit:
add al, 'A' - 10
store_hex:
mov [di], al
inc di
inc si
loop bcd_to_hex_loop
mov byte ptr [di], '$'
ret
bcd_to_hex_conversion endp
end main
Output:
Name: Ajit Bhagwan Bedwal Roll No: C-04
Practical N0:05
.model small
.stack 100h
.data
msg1 db 13,10,"Enter First Hexadecimal number:$"
msg2 db 13,10,"Enter Second Hexadecimal number:$"
result_msg db 13,10, "Result:$",13,10,"$", 13,10,"$"
.code
main proc
mov ax, @data
mov ds, ax
mov ah, 09h
lea dx, msg1
int 21h
mov ah, 01h
int 21h
mov bl, al
mov dl, 13
mov ah, 02h
int 21h
mov dl, 10
mov ah, 02h
int 21h
mov ah, 09h
lea dx, msg2
int 21h
mov ah, 01h
int 21h
mov cl, al
sub bl, '0'
sub cl, '0'
mov al, bl
mul cl
mov bl, al
mov ah, 09h
lea dx, result_msg
int 21h
add bl, '0'
mov dl, bl
mov ah, 02h
int 21h
mov dl, 13
mov ah, 02h
int 21h
mov dl, 10
mov ah, 02h
int 21h
mov ah, 4ch
int 21h
main endp
end main
Input:
Output:
Name: Ajit Bhagwan Bedwal Roll No:C-04
Practical N0:06
;Problem Statement:Write an x86/64 ALP to detect mode and display the
;values of GDTR,LDTR,IDTR,TR and MSW Registers also identify CPU type
;using CPUID instruction.
; .data section
section .data
rmodemsg db 10,'Processor is in Real Mode'
rmsg_len:equ $-rmodemsg
pmodemsg db 10,'Processor is in Protected Mode'
pmsg_len:equ $-pmodemsg
gdtmsg db 10,'GDT Contents are::'
gmsg_len:equ $-gdtmsg
ldtmsg db 10,'LDT Contents are::'
lmsg_len:equ $-ldtmsg
idtmsg db 10,'IDT Contents are::'
imsg_len:equ $-idtmsg
trmsg db 10,'Task Register Contents are::'
tmsg_len: equ $-trmsg
mswmsg db 10,'Machine Status Word::'
mmsg_len:equ $-mswmsg
colmsg db ':'
nwline db 10
; .bss section
section .bss
gdt resd 1
resw 1
ldt resw 1
idt resd 1
resw 1
tr resw 1
cr0_data resd 1
dnum_buff resb 04
%macro print 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
;.text section
section .text
global _start
_start:
smsw eax ;Reading CR0. As MSW is 32-bit cannot use RAX register.
mov [cr0_data],rax
bt rax,1 ;Checking PE bit, if 1=Protected Mode, else Real Mode
jc prmode
print rmodemsg,rmsg_len
jmp nxt1
prmode: print pmodemsg,pmsg_len
nxt1: sgdt [gdt]
sldt [ldt]
sidt [idt]
str [tr]
print gdtmsg,gmsg_len
mov bx,[gdt+4]
call print_num
mov bx,[gdt+2]
call print_num
print colmsg,1
mov bx,[gdt]
call print_num
print ldtmsg,lmsg_len
mov bx,[ldt]
call print_num
print idtmsg,imsg_len
mov bx,[idt+4]
call print_num
mov bx,[idt+2]
call print_num
print colmsg,1
mov bx,[idt]
call print_num
print trmsg,tmsg_len
mov bx,[tr]
call print_num
print mswmsg,mmsg_len
mov bx,[cr0_data+2]
call print_num
mov bx,[cr0_data]
call print_num
print nwline,1
exit: mov rax,60
xor rdi,rdi
syscall
print_num:
mov rsi,dnum_buff ;point esi to buffer
mov rcx,04 ;load number of digits to printlay
up1:
rol bx,4 ;rotate number left by four bits
mov dl,bl ;move lower byte in dl
and dl,0fh ;mask upper digit of byte in dl
add dl,30h ;add 30h to calculate ASCII
code cmp dl,39h ;compare with 39h
jbe skip1 ;if less than 39h skip adding 07 more
add dl,07h ;else add 07
skip1:
mov [rsi],dl ;store ASCII code in buffer
inc rsi ;point to next byte
loop up1 ;decrement the count of digits to printlay
;if not zero jump to repeat
print dnum_buff,4 ;printlay the number from buffer
ret
Output:
Processor is in Protected
Mode GDT Contents
are::00001000:007F LDT
Contents are::0000
IDT Contents
are::00000000:0FFF Task
Register Contents are::0040
Machine Status Word::8005FFFF