MPL 7 To 11
MPL 7 To 11
MPL 7 To 11
TITLE : Write X86/64 Assembly language program (ALP) to perform non-overlapped block
transfer.
CODE :
%macro scall 4 ;macro to
take input and output
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
Section .data
title: db 0x0A,"-------
BLock Transfer -----------
",0x0A
db "Non Overlapped
without string", 0x0A
t_len: equ $-title
copy: db 0x0A,0x0A,"
Copied data",
copy_len: equ $-copy
newline: db 0x0A
colon:db " : "
colon_len: equ $-colon
cnt_a: db 05H
cnt_a2:db 05H
cnt :db 05H
cnt2:db 05H
array: db
10H,20H,30H,40H,50H;d
ata to be transferred
;------------- BSS Section -
-------------------------
Section .bss
address: resb 16
val: resb 2
copied: resb 5
choice: resb 2
;------------- MAIN CODE
Section ---------------------
-----
Section .text
global _start
_start:
scall 1,1,title,t_len
scall 0,0,choice,2 ;read
choice
cmp byte[choice],'5' ;if
choice==5 then exit
je EXIT
;------------- Print Source
Array ADDRESS:
VALUE ---------------
mov byte[cnt_a],05h
mov rsi,array
label1:
push rsi
mov rbx,rsi
mov rdi,address
call HtoA_address
scall 1,1,newline,1
scall 1,1,address,16
scall 1,1,colon,colon_len
pop rsi
mov bl,byte[rsi]
push rsi
mov rdi,val
call HtoA_value
scall 1,1,val,2
pop rsi
inc rsi
dec byte[cnt_a]
jnz label1
;------------- CHOOSE
OPTION -------------------
-------
;compare choice here
cmp byte[choice],'1'
JE NONOVERLAPPED
;------Non overlapped
copying without string
instruction------
NONOVERLAPPED:
;---- Initializaion of
starting addresses
mov byte[cnt_a2],5H
mov rsi,array
mov rdi,array+20H
label2:
mov cl,00H
mov cl,byte[rsi]
mov byte[rdi],cl
inc rsi
inc rdi
dec byte[cnt_a2]
jnz label2
jmp OUTPUT
;------OUTPUT of Non-
Overlapped ----------------
OUTPUT:
scall 1,1,copy,copy_len
mov byte[cnt_a],05H
mov rsi,array+20H
jmp label3
;------Printig
ADDRESS:VALUE OF
COPIED DATA -----------
-----
label3:
push rsi
mov rbx,rsi
mov rdi,address
call HtoA_address
scall 1,1,newline,1
scall 1,1,address,16
scall 1,1,colon,colon_len
pop rsi
mov bl,byte[rsi]
push rsi
mov rdi,val
call HtoA_value
scall 1,1,val,2
pop rsi
inc rsi
dec byte[cnt_a]
jnz label3
;jmp to start of program
jmp _start
EXIT:
mov rax,60
mov rdi,0
syscall
;------HEX TO ASCII
CONVERSION
METHOD FOR
ADDRESS ----------------
HtoA_address: ;hex_no to
be converted is in ebx
//result is stored in
rdi/user defined variable
mov byte[cnt2],10H
aup:
rol rbx,04
mov cl,bl
and cl,0FH
cmp cl,09H
jbe ANEXT
ADD cl,07H
ANEXT:
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[cnt2]
JNZ aup
ret
;------HEX TO ASCII
CONVERSION
METHOD FOR
VALUE(2 DIGIT) --------
--------
HtoA_value: ;hex_no to
be converted is in ebx
//result is stored in
rdi/user defined variable
mov byte[cnt2],02H
aup1:
rol bl,04
mov cl,bl
and cl,0FH
CMP CL,09H
jbe ANEXT1
ADD cl,07H
ANEXT1:
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[cnt2]
JNZ aup1
ret
OUTPUT:
Assignment 8
TITLE : Write X86/64 Assembly language program (ALP) to perform overlapped block transfer.
CODE:
%macro scall 4 ;macro to take input and output
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
Section .data
title: db 0x0A,"------- BLock Transfer -----------",0x0A
db " Overlapped with String Instruction",0x0A
t_len: equ $-title
copy: db 0x0A,0x0A," Copied data",
copy_len: equ $-copy
newline: db 0x0A
colon:db " : "
colon_len: equ $-colon
cnt_a: db 05H
cnt_a2:db 05H
cnt :db 05H
cnt2:db 05H
array: db 10H,20H,30H,40H,50H;data to be transferred
;------------- BSS Section --------------------------
Section .bss
address: resb 16
val: resb 2
copied: resb 5
choice: resb 2
;------------- MAIN CODE Section --------------------------
Section .text
global _start
_start:
scall 1,1,title,t_len
scall 0,0,choice,2 ;read choice
cmp byte[choice],'5' ;if choice==5 then exit
je EXIT
;------------- Print Source Array ADDRESS: VALUE ---------------
mov byte[cnt_a],05h
mov rsi,array
label1:
push rsi
mov rbx,rsi
mov rdi,address
call HtoA_address
scall 1,1,newline,1
scall 1,1,address,16
scall 1,1,colon,colon_len
pop rsi
mov bl,byte[rsi]
push rsi
mov rdi,val
call HtoA_value
scall 1,1,val,2
pop rsi
inc rsi
dec byte[cnt_a]
jnz label1
;------------- CHOOSE OPTION --------------------------
;compare choice here
cmp byte[choice],'4'
je OVERLAPPED_STR
;------overlapped with string instruction----------------
OVERLAPPED_STR:
mov byte[cnt_a2],05H
mov rsi,array+04H
mov rdi,array+07H
STD
label6:
MOVSB
dec byte[cnt_a2]
jnz label6
jmp OUTPUT1
;------OUTPUT of Overlapped ----------------
OUTPUT1:
mov cl,byte[array+4H]
mov byte[array+7H],cl
scall 1,1,copy,copy_len
mov byte[cnt_a],08H
mov rsi,array
;------Printig ADDRESS:VALUE OF COPIED DATA ----------------
label3:
push rsi
mov rbx,rsi
mov rdi,address
call HtoA_address
scall 1,1,newline,1
scall 1,1,address,16
scall 1,1,colon,colon_len
pop rsi
mov bl,byte[rsi]
push rsi
mov rdi,val
call HtoA_value
scall 1,1,val,2
pop rsi
inc rsi
dec byte[cnt_a]
jnz label3
;jmp to start of program
jmp _start
EXIT:
mov rax,60
mov rdi,0
syscall
;------HEX TO ASCII CONVERSION METHOD FOR ADDRESS ----------------
HtoA_address: ;hex_no to be converted is in ebx //result is stored in rdi/user defined variable
mov byte[cnt2],10H
aup:
rol rbx,04
mov cl,bl
and cl,0FH
cmp cl,09H
jbe ANEXT
ADD cl,07H
ANEXT:
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[cnt2]
JNZ aup
ret
HtoA_value: ;hex_no to be converted is in ebx //result is stored in rdi/user defined variable
mov byte[cnt2],02H
aup1:
rol bl,04
mov cl,bl
and cl,0FH
CMP CL,09H
jbe ANEXT1
ADD cl,07H
ANEXT1:
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[cnt2]
JNZ aup1
Ret
OUTPUT:
Assignment 9
CODE:
global _start
_start:
section .text
%macro accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro disp 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
menu_label:
disp menu,menulen
disp cho,lenc
accept choice,02
mov al,byte[choice]
cmp al,31h
je successive_add
cmp al,32h
je add_shift
cmp al,33h
je exit
successive_add:
disp msg1,len1
accept num,03
call convert
mov [no1],al ;storing converted first no
in variable
disp msg2,len2
accept num,03
call convert
cmp al,00 ; multiplier=0 EX. 3*0
je m2 ;00 present in al i.e.lower bits of
ax
mov [no2],al
mov bx,0000h
mov [result],bx
mov bx,[no1]
m1:
add [result],bx
dec byte[no2]
jnz m1
disp res,lres ;Result:
m2:
mov ax,[result]
call display
disp msg,len ;new line
jmp menu_label
add_shift:
disp msg1,len1
accept num,03
call convert
mov [no1],al
disp msg2,len2
accept num,03
call convert
mov [no2],al
disp res,lres
mov bx,0000h
mov [res],bx
mov ax,[no1]
mov bx,[no2]
as3:
shr bx,01
jnc as1
add [res],ax
as1:
shl ax,01
cmp ax,00
jz as2
cmp bx,00
jnz as3
as2:
mov ax,[res]
call display
disp msg,len
jmp menu_label
exit:
mov rax,60
mov rdi,0
syscall
display:
mov rsi, disparr+03
mov rcx,04
l4:
mov rdx,0
mov rbx,10h
div rbx
cmp dl,09h
jbe add30
add dl,07h
add30:
add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz l4
mov rax,1
mov rdx,1
mov rsi,disparr
mov rdx,04
syscall
ret
convert:
mov rsi,num
mov al,[rsi]
cmp al,39h
jle a1
sub al,07h
a1:
sub al,30h
rol al,04
mov bl,al
inc rsi
mov al,[rsi]
cmp al,39h ;to get the second number
jle a2
sub al,07h
a2:
sub al,30h
add al,bl
ret
section .data
menu: db "MENU for multiplication:
",10
db "1. Add and shift method",10
db "2. Exit",10
menulen: equ $-menu
cho: db "Enter your choice: "
lenc: equ $-cho
msg: db " ",10
len: equ $-msg
msg1: db "Enter 1st number: "
len1: equ $-msg1
msg2: db "Enter 2nd number: "
len2: equ $-msg2
res: db "Result: "
lres: equ $-res
section .bss
disparr resb 02
choice resb 02
num resb 03
no1 resb 02
no2 resb 02
result resb 04
OUTPUT:
Assignment 10
TITLE : Implement x86 program for File Operation.
CODE:
%macro scall 4 mov
rax,%1 mov rdi,%2
mov rsi,%3 mov
rdx,%4 syscall
%endmacro Section
.data title: db
0x0A,"----
Commands -----",
0x0A db "1. Copy
",0x0A db "2. Type
",0x0A db "Enter
Your choice", 0x0A
title_len: equ $-title
openmsg: db "File
Opened
Successfully",0x0A
openmsg_len: equ $-
openmsg closemsg:
db "File Closed
Successfully",0x0A
closemsg_len: equ $-
closemsg errormsg:
db "Failed to open
file", 0x0A
errormsg_len: equ $-
errormsg delmsg: db
"Deleted File", 0x0A
delmsg_len: equ $-
delmsg typemsg: db
"=-----File Contents -
---=",0x0A
typemsg_len: equ $-
typemsg ;f1name: db
'file1.txt', 0 ;f2name:
db 'file2.txt', 0
;f3name: db
'file3.txt',0 filenmsg:
db "ENter File name:
" filen_len: equ $-
filenmsg Section .bss
buffer: resb 200
bufferlen:resb 8
cnt1:resb 8 fdis:resb
8 choice: resb 2
f1name: resb 20
f2name: resb 20
f3name: resb 20
Section .text global
main main: scall
1,1,title,title_len
scall 0,0,choice,2 ;---
---------- CHOOSE
OPTION --------------
------------ ;compare
choice here cmp
byte[choice],'1' ;if
choice is to display
content je COPY
cmp byte[choice],'2'
je TYPE COPY:
scall
1,1,filenmsg,filen_le
n scall 0,0, f1name,
20 dec rax mov
byte[f1name+rax],0
scall 2,f1name,2,777
;Opening file mov
qword[fdis],rax
;RAX contains file
descriptor value bt
rax,63 ;63rd bit is
+ve(0) if file is
successfull opened
else it is -ve (1) jc
next scall
1,1,openmsg,openms
g_len jmp next1
next: scall
1,1,errormsg,errorms
g_len jmp EXIT
next1: scall
0,[fdis],buffer,200
;reading contents of
file in buffer ;rax
contains actual
number of bytes read
mov
qword[bufferlen],rax
mov qword[cnt1],rax
;Closing file1 mov
rax,3 mov
rdi,f1name syscall
scall
1,1,closemsg,closem
sg_len ;----------------
---FILE 2 -------------
----- scall
1,1,filenmsg,filen_le
n scall 0,0, f2name,
20 dec rax mov
byte[f2name+rax],0
scall 2,f2name,2,777
mov qword[fdis],rax
;RAX contains file
descriptor value bt
rax,63 ;63rd bit is
+ve(0) if file is
successfull opened
else it is -ve (1) jc
next3 scall
1,1,openmsg,openms
g_len jmp next21
next3: scall
1,1,errormsg,errorms
g_len jmp EXIT
next21: scall
1,qword[fdis],buffer,
qword[bufferlen]
;writing to file2.txt
mov rax,3 mov
rdi,f2name syscall
scall
1,1,closemsg,closem
sg_len jmp main
TYPE:scall
1,1,filenmsg,filen_le
n scall 0,0, f2name,
20 dec rax mov
byte[f2name+rax],0
scall 2,f2name,2,777
;Opening file mov
qword[fdis],rax
;RAX contains file
descriptor value bt
rax,63 ;63rd bit is
+ve(0) if file is
successfull opened
else it is -ve (1) jc
tnext scall
1,1,openmsg,openms
g_len jmp tnext1
tnext: scall
1,1,errormsg,errorms
g_len jmp EXIT
tnext1: scall
0,[fdis],buffer,200
;reading contents of
file in buffer mov
qword[bufferlen],rax
scall 1,1,
typemsg,typemsg_le
n scall 1,1,
buffer,qword[bufferl
en] ;Closing file2
mov rax,3 mov
rdi,f2name syscall
scall
1,1,closemsg,closem
sg_len JMP main
EXIT: mov rax,60
mov rdi,0 syscall
OUTPUT:
Assignment 11
CODE:
%macro scall 4 ;macro for read/write system
call mov rax,%1 mov rdi,%2 mov rsi,%3 mov
rdx,%4 syscall
%endmacro
;--------------- DATA SECTION ---------------------
-Section .data title:db "------ Factorial Program ---
---",0x0A
db "Enter Number : ",0x0A
title_len: equ $-title factMsg: db
"Factorial is :", 0x0A
factMsg_len: equ $-factMsg cnt:
db 00H cnt2:db 02H num_cnt:
db 00H
;--------------- BSS SECTION ------------------------
Section .bss number:resb 2 factorial:resb 8
;--------------- TEXT SECTION ------------------------
Section .text global _start _start:
scall 1,1,title,title_len scall 0,0,number,2
mov rsi,number ;convert no.from ascii to
hex call AtoH ;converted number is stored
in "bl" mov rax,rbx FACTORIAL: cmp
rax,01H jbe exit;;code to complete call
fact_proc mov rbx,rax mov rdi,factorial call
HtoA_value
scall 1,1,factorial,8
;Exit System call
exit:
mov rax,60
mov rdi,0
syscall
;------------ FACT PROCEDURE -----
fact_proc:
cmp bl,01H
jne do_calc
mov ax,1 ret
do_calc:
push rbx dec
bl call
fact_proc
pop rbx mul
bl
ret
;------------- ASCII to HEX Conversion Procedure -------------------
-AtoH: ;result hex no is in bl mov byte[cnt],02H mov bx,00H hup:
rol bl,04 mov al,byte[rsi] cmp al,39H JBE HNEXT SUB al,07H
HNEXT:
sub al,30H
add bl,al
INC rsi
DEC byte[cnt]
JNZ hup
ret
;------HEX TO ASCII CONVERSION METHOD FOR VALUE(2 DIGIT) ---------------
HtoA_value: ;hex_no to be converted is in ebx //result is stored in rdi/user defined
variable mov byte[cnt2],08H aup1: rol ebx,04 mov cl,bl and cl,0FH CMP CL,09H jbe
ANEXT1 ADD cl,07H ANEXT1:
add cl, 30H
mov
byte[rdi],cl
INC rdi dec
byte[cnt2] JNZ
aup1 ret
OUTPUT: