sam mp code
sam mp code
1
Name: Sam Meher
Source Code:
section .bss
array resd 200
counter resb 1
section .text
global _start _start:
;display mov
Rax,1 mov
Rdi,1 mov
Rsi,msg1
mov Rdx,len1
syscall ;accept
mov byte[counter],05
mov rbx,00
loop1:
mov rax,0 ; 0 for read
mov rdi,0 ; 0 for
keyboard
mov rsi, array ;move pointer to
add rsi,rbx start of array
mov rdx,17
syscall
add rbx,17 ;to move counter
dec byte[counter] JNZ
loop1
;display mov
Rax,1
mov Rdi,1 mov
Rsi,msg2
mov
Rdx,len2
syscall
;display mov
byte[counter],05
mov rbx,00
loop2:
mov rax,1 ;1 for write
mov rdi, 1 mov ;1 for monitor
rsi, array add
rsi,rbx
mov rdx,17 ;16 bit +1 for enter
syscall
add rbx,17
dec
byte[counter]
JNZ loop2
;exit system
call mov rax ,60
mov rdi,0
syscall
;OUTPUT:
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ nasm -f elf64
MP2.asm
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ld -o MP1
MP1.o
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ./MP1
Source Code:
section .data
msg1 db 10,13,"Enter a string:"
len1 equ $-msg1
section .bss
_start:
;display
mov
Rax,1 mov
Rdi,1 mov
Rsi,msg1
mov
Rdx,len1
syscall
;store string
mov rax,0
mov rdi,0
mov rsi,str1 mov
rdx,200
syscall
call display
%macro dispmsg2
mov Rax,1
mov Rdi,1
mov Rsi,%1
mov Rdx,%2
syscall
%endmacro
display:
mov rbx,rax ;store no in rbx
mov rdi,result ;point rdi to result variable
mov cx,16 ;load count of rotation
up1:
rol rbx,04 ;rotate no of left by four bits
mov al,bl ;move lower byte in al and
al,0fh ;get only LBS
cmp al,09h ;compare with 39h
jg add_37 ;if greater than 39h skip add 37
add al,30h jmp skip ;else add 30 add_37:
add al,37h
skip:
mov [rdi],al ;store ascii code in result variable
inc rdi ;point ti next byte dec
cx ;decreament counter jnz up1 ;if not
zero jump to repeat
dispmsg result,16 ;call to macro ret
;OUTPUT:
;Enter a string:PDEACOEM
;0000000000000009student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP
practicals$
Practical No.3
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351
Program: Write X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers.
Source Code:
section .data
array db 12h,5h,33h,42h,64h msg1 db
10,13,"Largest no in an array is : "
len1 equ $-msg1
;display al call
display
;display message
mov Rax,1
mov Rdi,1
mov
Rsi,msg1
mov
Rdx,len1
syscall
dispmsg
result,16 ;call
to macro
;exit system call mov
Rax,60 mov
Rdi,0 syscall
display:
mov rbx,rax mov
rdi,result
mov cx,16
up1:
rol rbx,04
mov al,bl
and al,0fh
cmp
al,09h jg
add_37
add al,30h
jmp skip1
add_37:
add al,37h
skip1: mov[rdi],al
inc rdi
dec cx
jnz up1 ret
;OUTPUT:
Source Code:
section .data
menumsg db
10,'******MENU******', db
10,'1:Addition' db 10,'2:Subtraction'
db 10,'3:Multiplication' db
Choice : '
nummsg db 10
result dq 0
nwmsg db 10
resh dq 0
resl dq 0
section .bss
choice resb 2
dispbuff resb 16
%macro scall 4
mov rax,%1 mov
rdi,%2 mov
rsi,%3 mov
rdx,%4
syscall
%endmacro
section .text
global _start
_start: up:
scall 1,1,menumsg,menumsg_len
scall 0,0,choice,2
case1:cmp byte[choice],'1'
jne case2
call add_proc jmp
up case2:
cmp
byte[choice],'2' jne
case3 call
sub_proc jump up
case3: cmp
byte[choice],'3' jne
case4 call
mul_proc jmp up
case4:
cmpbyte[choice],'4
' jne caseinv call
div_proc jmp up
caseinv:
scall 1,1,wrchmsg,wrchmsg_len
exit:
mov
eax,01
mov ebx,0
int 80h
mul_proc:
scall 1,1,mulmsg,mulmsg_len
mov rax,[no1]
mov rbx,[no2]
mul rbx mov
[resh],rdx
mov [resl],rax
scall
1,1,resmsg,resmsg_le
n mov rbx,[resh] call
disp64num mov
rbx,[resl]
call disp64num
scall 1,1,nwmsg,1
ret
div_proc:
scall 1,1,divmsg,divmsg_len
mov rax,[no1]
mov rdx,0 mov
rbx,[no2] div rbx mov
[resh],rdx mov
[resl],rax scall
1,1,rmsg,rmsg_len mov
rbx,[resh] call
disp64num scall
1,1,qmsg,qmsg_len
mov rbx,[resl]
call disp64num
scall 1,1,nwmsg,1
ret
disp64num:
OUTPUT
;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division
;Result is : 000000000000000A
;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division
;Result is : 0000000000000006
;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division
;Welcome to Multiplication
;Result is : 00000000000000000000000000000010
;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division
;Welcome to Division
;Remainder : 0000000000000000
;Quotient : 0000000000000004
Practical No.5
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351
Program: Write an X86/64 ALP to count number of positive and negative numbers from the
array.
Source Code:
section .data
welmsg db 10,'Welcome to count positive and negative numbers in array',10 welmsg_len
equ $-welmsg
nwline db 10 array dw
8505h,90ffh,87h,88h,8a9fh,0adh,02h,8507h
arrent equ 8
pcnt db 0
ncnt db 0
section .bss
dispbuff resb 2
%macro print 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
section .text
global _start
_start:
print welmsg,welmsg_len
mov esi,array
mov ecx,arrent
up1:
bt word[esi],15
jnc pnxt
pnxt:
incbyte[pcnt]
pskip:inc esi inc
esi loop up1
print
pmsg,pmsg_len
mov bl,[pcnt] call
disp8num print
nmsg,nmsg_len
mov bl,[ncnt] call
disp8num print
nwline,1 exit:
mov
eax,01
mov ebx,0
int 80h
disp8num: mov
ecx,2
mov
edi,dispbuff
dup1:
rol bl,4 mov
al,bl and al,0fh
cmp al,09 jbe
dskip
dskip:
add al,30h
mov[edi],al
inc edi
loop dup1
print dispbuff,2
ret
;OUTPUT
Program: Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD
number and 5-digit ;BCD number into its equivalent HEX number. Make your program
user friendly to accept the ;choice from user for: (a) HEX to BCD b) BCD to HEX (c)
EXIT.
section .bss
arr resb 06
dispbuff resb 08
ans resb 01
%macro disp 2
mov rax,01
rsi,%1 mov
rdx,%2
syscall
%endmacro
%macro accept 2 mov
rax,0 mov
rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
menu:
disp msg1,msg1length
accept arr,2
hex2bcd_proc: disp
msg2,msg2length
accept arr,5 call
conversion
mov rcx,0
mov ax,bx
mov bx,10
ret
bcd2hex_proc: disp
msg4,msg4length
msg6,msg6length
mov rsi,arr
mov rcx,05
mov rax,0
mov
ebx,0ah
jnz l55
mov ebx,eax
call disp32_num
ret
conversion:
mov bx,0 mov
ecx,04
mov esi,arr
up1:
rol bx,04
mov
al,[esi]
cmp al,39h
jbe l22 sub
al,07h
l22: sub al,30h
add bl,al
inc esi
loop up1
ret disp32_num:
mov rdi,dispbuff mov
rcx,08
l77:
rol ebx,4 mov
dl,bl and dl,0fh
add dl,30h cmp
dl,39h jbe l66 add
dl,07h l66:
mov [rdi],dl inc rdi dec
;OUTPUT
;Enter Choice: 1
;Enter Choice: 1
;Enter 4 digit HEX number: 00FF
;BCD Equivalent: 255
;Enter Choice: 2
;Enter Choice: 2
;Enter Choice: 3
gdt resd 1
resw 1 ldt
resw 1 idt
resd 1 resw 1
tr resw 1
cr0_data resd 1
dnum_buff resb 04
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
mov bx,[gdt+4]
call print_num mov
bx,[gdt+2] call print_num print
colmsg, 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
mov
bx,[cr0_data]
call print_num
print nwline,1
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
;if less than 39h skip adding 07
jbe skip1
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::00077000:007F
LDT Contents are::0000
IDT Contents are::00000000:0FFF
Task Register Contents are::0040
Machine Status Word::8005FFFF
(base)
Practical No.8
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351
Program: Write an X86/64 ALP to perform non-overlapped block transfer without string
specific instruction. Block containing data can be defined in the data segment.
Source Code: section .data
sourceBlock db12h,45h,87h,24h,97h
count equ 05
msg db "ALP for non overlapped block transfer using string instructions : ",10
msg_len equ $ - msg
section .bss
destBlock resb5
result resb 4
%macro write 2
mov rax,1
mov rdi,1 mov
rsi,%1 mov
rdx,%2 syscall
%endmacro
section .text
global _start
_start:
mov rsi,sourceBlock
mov rdi,destBlock
mov rax,60
mov rdi,0
syscall
dispBlock:
mov rbp,count
next:mov al,[rsi]
push rsi call
disp pop rsi
inc rsi dec rbp
jnz next
ret
write result , 4
ret
Output:
ALP for non overlapped block transfer using string instructions :
Before Block Transfer :
The source block contains the elements :
1245872497
The destination block contains the elements :
0000000000 After
Block Transfer :
The source block contains the elements :
1245872497
The destination block contains the elements : 1245872497(base)
Practical No.9
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351
Program: Write an X86/64 ALP to perform overlapped block transfer with string specific
instruction. Block containing data can be defined in the data segment.
space ""
ano_len equ $-ano
db equ
sblock
dblock times 5 db 0
; :" section
.bss
char_ans resB 2 ;char_ans is of 2 byte because we have 2 byte nos
%macro Exit 0
Print nline,nline_len
MOV RAX,60
MOV RDI,0
syscall
%endmacro
;
section .text
global _start
_start:
Print ano,ano_len
Print smsg,smsg_len
mov rsi,sblock ;As rsi is used to point source as well as destination block
call disp_block ;assign source and destination block separately before call
Print dmsg,dmsg_len
mov rsi,dblock call disp_block call BT_NO
Exit
;
BT_NO:
mov
rsi,sblock
mov
rdi,dblock
Print amsg,amsg_len ;Block values mov rcx,5
after transfer
Print
smsg,smsg_len
mov rsi,sblock call
disp_block
mov
[rdi],al ;(memory-memory transfer is not allowed)
inc
inc
rsi
dec
jnz rdi
RET
;
rcx back
disp_bloc
:
next_num:
mov al,[rsi] ;moves 1 value to rsi
push rsi ;push rsi on stack as it get modified in Disp_8
dec rbp
jnz next_num
RET
;
Disp_8:
MOV RSI,char_ans+1
MOV RCX,2 ;counter
MOV RBX,16 ;Hex no
next_digit:
XOR RDX,RDX
DIV RBX
CMP DL,9
JBE add30
ADD DL,07H
add30 :
ADD DL,30H
MOV [RSI],DL
DEC RSI
DEC RCX
JNZ next_digit
Print char_ans,2
ret
Output:
:11 22 33 44 55
Before Transfer:: :00 00 00 00 00
Source Block
Destination Block
After Transfer::
Source Block 11 22 33 44 55
Destination Block :11 22 33 44 55
(base)
Practical No.10
Source Code:
section .text
global
_start
_start: xor
rax,rax xor
rbx,rbx xor
rcx,rcx xor
rdx,rdx
mov byte[result],0
mov byte[num],0
mov
byte[num1],0
mov rax,1
mov rdi,1 mov
rsi,choice mov
rdx,choice_len
syscall
mov rax,0
mov rdi,0 mov
rsi,cho mov
rdx,2
syscall cmp
byte[cho],31h
je a
cmpbyte[cho],32h
je b
jmp exit
a:call
Succe_addition jmp
_start b: call
Add_shift jmp _start
exit:
mov rax,60
mov rdi,0
syscall
convert:
xor rbx,rbx
xor rcx,rcx
xor rax,rax
mov rcx,02
mov rsi,num
up1: rol
bl,04
mov al,[rsi] cmp
al,39h
jbe p1
sub al,07h
jmp p2 p1:
subal,30h
p2: add bl,al
inc rsi
loop up1
ret
display:
mov rcx,4
mov
rdi,result
dup1: rol
bx,4 mov
al,bl and
al,0fh cmp
al,09h jbe
p3 add al,07h jmp
p4
p3: add al,30h
p4:
mov[rdi],al inc
rdi loop dup1
mov rax,1
mov rdi,1
mov
rsi,result
mov rdx,4
syscall
ret
Succe_addition:
mov rax,1
mov rdi,1
mov rsi,msg
mov rdx,msg_len
syscall
mov rax,0
mov rdi,0 mov
rsi,num
mov rdx,3
syscall
call convert
mov [num1],bl
mov rax,1
mov rdi,1
mov rsi,msg
mov rdx,msg_len
syscall mov rax,0
mov rdi,0
movrsi,num
mov rdx,3
syscall
repet: add
rcx,rax dec bl
jnz repet mov
[result],rcx
mov rax,1
mov rdi,1 mov
rsi,res
movrdx,res_le
n syscall
movrbx,[result
] call display
ret
Add_shift:
mov rax,1
mov rdi,1 mov
rsi,msg mov
rdx,msg_len
syscall
mov rax,0
mov rdi,0
movrsi,num
mov rdx,3
syscall
call convert
mov
[num1],bl
mov rax,1
mov rax,0
mov rdi,0
movrsi,num
mov rdx,3
syscall
call convert
mov [num],bl
xor rbx,rbx
xor rcx,rcx
xor rdx,rdx
xor rax,rax
mov dl,08
moval,[num1]
mov bl,[num]
p11:
shr bx,01
jnc p add
cx,ax p:
shl ax,01
dec dl jnz p11
mov[result],rc
mov rax,1
mov rdi,1
mov rsi,res
movrdx,res_len
syscall
movrbx,[result
] call display
ret
Source Code:
section .data
msg : db "File does not exist ",0AH len :
equ $-msg msg1 : db "File successfully
copied",0AH len1 : equ $-msg1 msg2 : db
"File successfully deleted!!!!",0AH
len2 : equ $-msg2 msg3 : db "Enter the data to be
typed in the file",0AH
len3 : equ $-msg3
buffer : times 1000 db ' '
filelen : dq 0
section .bss
filedes_1 : resq 1
filedes_2 : resq 1
filename_1 :
resb16 filename_2
: resb16 choice :
resb 8
section .txt
global _start
%macro print2
mov rax,1 mov
rdi,1
mov rsi,%1 mov
rdx,%2
syscall
%endmacro
%macro read 2
mov rax,0 mov
rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
_start:
pop rbx ; REMOVE THE NUMMBER OF ARGUMENS FROM THE STACK pop
rbx ;REMOVE EXECUTABLE PROGRAM NAME FROM THE STACK
inc rsi
cmpbyte[rbx],0H
jne up_2
mov rax,0
mov rdi,qword[filedes_1]
mov rsi,buffer
mov rdx,100
syscall
movrdi,qword[filedes_2]
mov rsi,buffer
mov
rdi,filedes_2
syscall
;PRINTING
MESSAGE
print msg1,len1
jmp exit
inc rbx
cmp
byte[rbx],0H
jne up_3
;PRINTING THE
MESSAGE print msg2,len2
jmp exit
inc rbx
cmp
byte[rbx],0H
jne up_4
;CHECKING IF FILE
EXIST cmp rax,1 je NoFile
mov [filedes_1],rax
;READING THE INPUT FROM THE
SCREEN print msg3,len3 read buffer,1000
syscall
jmp exit
;OUTPUT:
Program: Write X86/64 ALP to find the factorial of a given integer number on a command
line by using recursion. Explicit stack manipulation is expected in the code.
Source Code:
%macro scall4
mov rax,%1 mov
rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
section .data
num db 00h msg db
"Factorial is : "
msglen equ $-msg
msg1 db"*****Program to find Factorial of anumber*****",0Ah
db"Enter the number : ",
msg1len equ $-msg1
zerofact db "00000001"
zerofactlen equ $-zerofact
section .bss
dispnum resb16
result resb
4 temp resb 3
section .text
global
_start
_start:
scall1,1,msg1,msg1len
scall 0,0,temp,3
call convert
mov[num],dl scall
1,1,msg,msglen
rax,0 moval,[num]
cmp al,01h
jbe endfact
mov rbx,0 mov bl,01h call
factr call display call exit
endfact: scall
1,1,zerofact,zerofactlen
call exit
factr:
cmprax,01h
je retcon
push rax
dec rax
call
factr
retcon:
pop rbx
jmp
retcon
endpr:
ret
display:
mov rsi,dispnum+15
mov rcx,0
mov cl,16
count:
mov rdx,0
mov rbx,0
mov bl,10h
div ebx
cmp dl,09h
jbe skip
add dl,07h
skip:
add dl,30h
mov[rsi],d
l dec rsi
loop count
scall
1,1,dispnum,16 ret
convert:
mov rsi,temp
mov cl,02h
mov rax,0
mov rdx,0
contc: rol
dl,04h
moval,[rsi]
cmp al,39h
jbe skipc
sub al,07h
skipc:
dec cl
jnz contc
ret
exit:
movrax,60
mov rdi,0 syscall
ret
;OUTPUT