0% found this document useful (0 votes)
9 views54 pages

sam mp code

The document contains multiple practical assignments by Sam Meher, focusing on writing X86/64 assembly language programs. Each practical includes source code for tasks such as accepting hexadecimal numbers, counting string lengths, finding the largest number in an array, performing arithmetic operations, and counting positive and negative numbers in an array. The output of each program is also provided, demonstrating the functionality of the code.
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)
9 views54 pages

sam mp code

The document contains multiple practical assignments by Sam Meher, focusing on writing X86/64 assembly language programs. Each practical includes source code for tasks such as accepting hexadecimal numbers, counting string lengths, finding the largest number in an array, performing arithmetic operations, and counting positive and negative numbers in an array. The output of each program is also provided, demonstrating the functionality of the code.
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/ 54

Practical No.

1
Name: Sam Meher

Roll no: B-69.


Seat no.: S400260351
Program: Write X86/64 ALP to accept five 64-bit Hexadecimal number from user and
store them in an array and display the accepted numbers

Source Code:

section .data msg1 db 10,13,"Enter five 64 bit Hexadecimal


numbers:" len1 equ $-msg1 msg2 db 10,13,"Entered five 64 bit
Hexadecimal numbers:" len2 equ $-msg2

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

;Enter five 64 bit Hexadecimal numbers:12


;42
;61
;78
;93

;Entered five 64 bit Hexadecimal numbers:12


;42
;61
;78
;93
Practical No.2

Name: : Sam Meher Roll


no.: B-69.
Seat no.: S400260351
Program: Write X86/64 ALP to accept a string and to display its length.

Source Code:
section .data
msg1 db 10,13,"Enter a string:"
len1 equ $-msg1

section .bss

str1 resb 200 ; string

declaraton result resb 16 section

.text global _start

_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

;exit system call


mov Rax,60
mov Rdi,0
syscall

%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:

;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 MP2
MP2.o ;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ./MP2

;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

section .bss cnt resb


1
result resb 16
%macro dispmsg
2 mov Rax,1 mov
Rdi,1 mov rsi,%1
mov rdx,%2
syscall
%endmacro

section .text global


_start _start:
mov
byte[cnt],5
mov rsi,array
mov al,0
LP:cmp al,[rsi] jg skip
xchg
al,[rsi] skip: inc
rsi dec
byte[cnt] jnz
LP

;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:

;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ nasm -f elf64


MP3.asm
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ld -o MP3
MP3.o ;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ./MP3

;Largest no in an array is : 0000000000000064student@student-OptiPlex-390:~/nasm-


2.17rc0-20230220/MP practicals$
Practical No.4
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351
Program: Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal
arithmetic operation (+,-,*,/) using suitable macros. Define procedure for each
operation.

Source Code:

section .data
menumsg db

10,'******MENU******', db

10,'1:Addition' db 10,'2:Subtraction'

db 10,'3:Multiplication' db

10,'4:Division' db 10,10,'Enter Your

Choice : '

menumsg_len: equ $-menumsg

addmsg db 10,'Welcome to addition',10


addmsg_len equ $-addmsg

submsg db 10,'Welcome to addition',10


submsg_len equ $-submsg

mulmsg db 10,'Welcome to Multiplication',10


mulmsg_len equ $-mulmsg
divmsg db 10,'Welcome to Division',10
divmsg_len equ $-divmsg
wrchmsg db 10,10,'You entered wrong choice...!',10
wrchmsg_len equ $-wrchmsg

no1 dq 08h no2


dq 02h

nummsg db 10
result dq 0

resmsg db 10,'Result is : '


resmsg_len equ $-resmsg

qmsg db 10,'Quotient : '


qmsg_len equ $-qmsg
rmsg db 10,'Remainder : '
rmsg_len equ $-rmsg

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

add_proc: mov rax,[no1 adc


rax,[no2] mov [result],rax
scall 1,1,resmsg,resmsg_len
mov rbx,[result]
call disp64num
scall 1,1,nummsg,1
ret

sub_proc: mov rax,[no1] sub


rax,[no2] mov [result],rax
scall 1,1,resmsg,resmsg_len
mov rbx,[result]
call disp64num
scall 1,1,nummsg,1
ret

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:

mov ecx,16 mov


edi,dispbuff
dup1:

rol rbx,4 mov al,bl and


al,0fh cmp al,09 jbe dskip
add al,07h dskip: add
al,30h mov[edi],al inc edi
loop dup1 scall
1,1,dispbuff,16
ret

OUTPUT

;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ nasm -f elf64 MP4.asm


;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ ld -o MP4 MP4.o
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ ./MP4

;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division

;Enter Your Choice : 1

;Result is : 000000000000000A
;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division

;Enter Your Choice : 2

;Result is : 0000000000000006

;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division

;Enter Your Choice : 3

;Welcome to Multiplication
;Result is : 00000000000000000000000000000010

;******MENU******
;1:Addition
;2:Subtraction
;3:Multiplication
;4:Division

;Enter Your Choice : 4

;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

pmsg db 10,'Count of +ve numbers: '

pmsg_len equ $-pmsg

nmsg db 10,'Count of -ve numbers: '


nmsg_len equ $-nmsg

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

inc byte[ncnt] jmp


pskip

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

;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ nasm -f elf64 MP5.asm


;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ ld -o MP5 MP5.o
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ ./MP5
;Welcome to count positive and negative numbers in array

;Count of +ve numbers: 04


;Count of -ve numbers: 04
Practical No.6

Name: : Sam Meher Roll


no.: B-69.
Seat no.: S400260351

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.

Source Code: section


.data
msg1 db 10,10,'##### MENU FOR CODE CONVERSION
#####' db 10,'1:HEX to BCD' db 10,'2:BCD to HEX' db
10,'3:EXIT' db 10,10,'Enter Choice: ' msg1length equ $-msg1

msg2 db 10,10,'Enter 4 digit HEX number: '


msg2length equ $-msg2

msg3 db 10,10,'BCD Equivalent: '


msg3length equ $-msg3

msg4 db 10,10,'Enter 5 digit BCD number: '

msg4length equ $-msg4 msg5 db 10,10,'Thanks for

using the program!!',10,10 msg5length equ $-msg5

msg6 db 10,10,'HEX Equivalent:


' msg6length equ $-msg6 cnt db
0

section .bss

arr resb 06

dispbuff resb 08

ans resb 01

%macro disp 2

mov rax,01

mov rdi,01 mov

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

cmp byte [arr],'1' jne


l1
call hex2bcd_proc
jmp menu

l1: cmp byte [arr],'2' jne l2


call bcd2hex_proc
jmp menu

l2: cmp byte [arr],'3' jne


exit
disp msg5,msg5length
jmp menu

exit: mov rax,60 mov


rbx,0 syscall

hex2bcd_proc: disp
msg2,msg2length
accept arr,5 call
conversion
mov rcx,0
mov ax,bx
mov bx,10

l33: mov dx,0


div bx
push rdx
inc rcx
inc byte[cnt]
cmp ax,0
jne l33
disp msg3,msg3length
l44: pop rdx
add dl,30h
mov[ans],dl
disp ans,1 dec
byte[cnt] jnz l44

ret

bcd2hex_proc: disp

msg4,msg4length

accept arr,6 disp

msg6,msg6length

mov rsi,arr
mov rcx,05
mov rax,0
mov
ebx,0ah

l55: mov rdx,0


mul ebx
mov
dl,[rsi]
sub dl,30h
add rax,rdx
inc rsi
dec rcx

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

rcx jnz l77

disp dispbuff+3,5 ret

;OUTPUT

;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ nasm -f elf64


MP6.asm
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ld -o MP6
MP6.o ;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ./MP6

;##### MENU FOR CODE CONVERSION #####


;1:HEX to BCD
;2:BCD to HEX
;3:EXIT

;Enter Choice: 1

;Enter 4 digit HEX number: FFFF

;BCD Equivalent: 65535

;##### MENU FOR CODE CONVERSION #####


;1:HEX to BCD
;2:BCD to HEX
;3:EXIT

;Enter Choice: 1
;Enter 4 digit HEX number: 00FF
;BCD Equivalent: 255

;##### MENU FOR CODE CONVERSION #####


;1:HEX to BCD
;2:BCD to HEX
;3:EXIT

;Enter Choice: 2

;Enter 5 digit BCD number: 65535

;HEX Equivalent: 0FFFF

;##### MENU FOR CODE CONVERSION #####


;1:HEX to BCD
;2:BCD to HEX
;3:EXIT

;Enter Choice: 2

;Enter 5 digit BCD number: 00255

;HEX Equivalent: 000FF

;##### MENU FOR CODE CONVERSION #####


;1:HEX to BCD
;2:BCD to HEX
;3:EXIT

;Enter Choice: 3

;Thanks for using the program!!


Practical No.7
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351
Program: Write an X86/64 ALP to detect protected mode and display the value of GDTR,
LDTR, IDTR, TR and MSW Register also identify CPU type using CPUID instruction.
Source Code:
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 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

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, 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

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
;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

msgSource db 10,"The source block contains the elements : ",10


msgSource_len equ $ - msgSource

msgDest db 10,10,"The destination block contains the elements : ",10


msgDest_len equ $ - msgDest

bef db 10, "Before Block Transfer : ",10


beflen equ $ - bef

aft db 10,10 ,"After Block Transfer : ",10


aftlen equ $ - aft

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:

write msg , msg_len


write bef , beflen

write msgSource , msgSource_len


mov rsi,sourceBlock call dispBlock

write msgDest , msgDest_len


mov rsi,destBlock call dispBlock

mov rsi,sourceBlock
mov rdi,destBlock

mov rcx, count


cld rep movsb

write aft , aftlen

write msgSource , msgSource_len


mov rsi,sourceBlock call dispBlock

write msgDest , msgDest_len


mov rsi,destBlock call dispBlock

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

disp: mov bl,al ;store number in bl


mov rdi, result ;point rdi to result variable
mov cx,02 ;load count of rotation in cl up1:

rol bl,04 ;rotate number left by four


bits mov al,bl ;move lower byte in dl and
al,0fh ; get only LSB cmp al,09h ;compare
with 39h jg add_37 ;if grater
than 39h skip add 37 add al,30h jmp
skip1 ;else add 30 add_37: add al,37h
skip1: mov [rdi],al ;store ascii code in result variable
inc rdi ;point to next byte dec cx ;decrement the
count of digits to display jnz up1 ;if not zero jump
to repeat

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.

Source Code: section


.data

nline db equ 10,10


nline_len $-nline
db

space ""
ano_len equ $-ano
db equ

bmsg 10,"Before Transfer::" $-bmsg


db equ
bmsg_len
10,"After Transfer::" $-amsg
db equ
amsg
amsg_len 10," Source Block $-smsg
db equ
db
smsg 10," Destination Block $-dmsg smsg_len
11h,22h,33h,44h,55h
dmsg
dmsg_len
:"

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 Print 2 MOV RAX,1


MOV RDI,1
MOV RSI,%1
MOV RDX,%2 syscall
%endmacro
%macro Read 2
MOV RAX,0
MOV RDI,0
MOV RSI,%1
MOV RDX,%2 syscall
%endmacro

%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 bmsg,bmsg_len ;Block values before transfer

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

;call for actual block transfer

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

Print dmsg,dmsg_len mov


rsi,dblock
call disp_block
back:
mov al,[rsi] ;moves 1 value from rsi to rdi

mov
[rdi],al ;(memory-memory transfer is not allowed)

inc

inc

rsi

dec
jnz rdi
RET
;
rcx back

disp_bloc
:

mov rbp,5 ;counter as 5 values

next_num:
mov al,[rsi] ;moves 1 value to rsi
push rsi ;push rsi on stack as it get modified in Disp_8

call Disp_8 Print


space,1

pop rsi ;again pop rsi that pushed on stack


inc rsi

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

Name: : Sam Meher Roll


no.: B-69.
Seat no.: S400260351
Program: Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal
number. Use successive addition and add and shift method. (use of 64-bit register is
expected).

Source Code:

section .data msg db'Enter two digit


number: ',0xa msg_len equ $-msg res db
10,'Multiplication of elements is: ' res_len equ
$-res choice db 'Enter your choice:',0xa
db'1.Successive Addition',0xa db'2.Add and
Shift method',0xa
db'3.Exit',0xa
choice_len equ $-choice
section .bss
num resb 03 num1
resb 01 result resb 04
cho resb 2

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

call convert xor


rcx,rcx xor
rax,rax mov
rax,[num1]

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 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 [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

;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ nasm -f elf64


MP10.asm
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ld -o MP10
MP10.o
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ./MP10
;Enter your choice: ;1.Successive Addition
;2.Add and Shift method
;3.Exit ;1
;Enter two digit number:
;25
;Enter two digit number:
;50

;Multiplication of elements is: 090

;Enter your choice:


;1.Successive Addition
;2.Add and Shift method
;3.Exit ;2
;Enter two digit number:
;25
;Enter two digit number:
;50
;Multiplication of elements is: 090
;Enter your choice: ;1.Successive
Addition
;2.Add and Shift method
;3.Exit
;3
Practical No.11
Name: : Sam Meher Roll
no.: B-69.
Seat no.: S400260351

Program: Write X86 Assembly Language Program (ALP) to implement following OS


commands. i)COPY, ii) TYPE Using file operations. User is supposed to provide
command line arguments.

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

pop rbx ; GET THE FIRST ARGUMENT

;READ THE CHOICE i.e COPY OR DELETE OR TYPE


mov [choice],rbx
mov
rsi,qword[choice]
cmp byte[rsi],43H je
copy cmp
byte[rsi],44H je
Delete jmp type

;---------------COPY FROM ONE FILE TO ANOTHER FILE -------------------------


> copy: pop rbx mov rsi,filename_1
up_1: mov al,byte[rbx]
mov byte[rsi],al
inc rsi inc rbx
cmpbyte[rbx],0
H jne up_1

pop rbx mov


rsi,filename_2
up_2: mov
al,byte[rbx] mov
byte[rsi],al
inc rbx

inc rsi

cmpbyte[rbx],0H
jne up_2

;OPENING FIRST FILE


mov rax,2
mov rdi,filename_1 ;FIRST FILE NAME
mov rsi,2 ; WR MODE
mov rdx,0777 ; Permissions given to the file user,Owner,group is read and write and
execute
syscall
;CHECKING
IF FILE
EXIST bt
rax,63 jc
NoFile mov
[filedes_1],ra
x ; if exists
the saving hte
file descriptor
;OPENING SECOND FILE
mov rax,2
mov rdi,filename_2 ;SECOND FILE NAME
mov rsi,2 ; WR MODE
mov rdx,0777 ; Permissions given to the file user,Owner,group is read and write and
execute
syscall

;CHECKING IF FILE EXIST

cmp rax,0 jle NoFile mov [filedes_2],rax ; if exists the

saving the file descriptor

;READING FIRST FILE

mov rax,0

mov rdi,qword[filedes_1]
mov rsi,buffer

mov rdx,100

syscall

;SAVING THE LENGHT OF FIRST FILE


mov qword[filelen],rax

;WRITING TO SECOND FILE


mov rax,1

movrdi,qword[filedes_2]
mov rsi,buffer

mov rdx,qword[filelen] syscall

;CLOSING THE FILES


mov rax,3 mov
rdi,filedes_1 syscall
mov rax,3

mov
rdi,filedes_2
syscall

;PRINTING
MESSAGE

print msg1,len1

jmp exit

;< DELETING FILE >


Delete : pop rbx
mov
rsi,filename_1
up_3: mov al,byte[rbx]
mov byte[rsi],al
inc rsi

inc rbx

cmp
byte[rbx],0H

jne up_3

;DELETE SYSTEM CALL


mov rax,87
mov rdi,filename_1
syscall

;PRINTING THE
MESSAGE print msg2,len2
jmp exit

;< TYPE IN THE FILE >


type :
;SAVING FILE NAME
pop rbx
mov rsi,filename_1
up_4: mov
al,byte[rbx] mov
byte[rsi],al
inc rsi

inc rbx

cmp

byte[rbx],0H

jne up_4

;OPENING THE FILE


mov rax,2
mov rdi,filename_1 ;FIRST FILE NAME mov rsi,2 ; APPEND MODE mov rdx,0777 ;
Permissions given to the file user,Owner,group is read and write and execute syscall

;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

;WRITING TO THE FILE


mov rax,1
movrdi,qword[filedes_1
] mov rsi,buffer mov
rdx,1000

syscall

;CLOSING THE FILES


mov rax,3
mov rdi,filedes_1
syscall

jmp exit

;< PRINT FILE >


NoFile :
print msg,len
jmp exit

;<--------------TERMINATING THE PROGRAM --------------- > exit:


movrax,60
mov rdi,0
syscall

;OUTPUT:

;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ nasm -f elf64


MP11.asm
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ld -o MP11
MP11.o
;student@student-OptiPlex-390:~/nasm-2.17rc0-20230220/MP practicals$ ./MP11
; ./MP11 COPY a.txt b.txt ; ./MP11 TYPE a.txt
; ./MP11 DELETE c.txt
Practical No.12

Name: : Sam Meher Roll


no.: B-69.
Seat no.: S400260351

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

mov rdx,0 mov

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:

sub al,30h add


dl,al
inc rsi

dec cl

jnz contc

ret

exit:

movrax,60
mov rdi,0 syscall
ret
;OUTPUT

student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ nasm -f elf64 MP12.asm


student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ ld -o MP12 MP12.o
student@student-OptiPlex-390:~/nasm-2.17rc0-20230220$ ./MP12
*****Program to find Factorial of a number*****
Enter the number : 5
Factorial is : 00000000000120

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