0% found this document useful (0 votes)
7 views34 pages

MPL 3-11

The document contains multiple assignments related to X86/64 Assembly Language Programming (ALP). Each assignment focuses on different tasks such as string length calculation, finding maximum numbers, counting positive and negative integers, and performing block transfers with or without overlap. The programs utilize system calls and macros to handle input/output operations and register manipulations.

Uploaded by

sawantsamikshad
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)
7 views34 pages

MPL 3-11

The document contains multiple assignments related to X86/64 Assembly Language Programming (ALP). Each assignment focuses on different tasks such as string length calculation, finding maximum numbers, counting positive and negative integers, and performing block transfers with or without overlap. The programs utilize system calls and macros to handle input/output operations and register manipulations.

Uploaded by

sawantsamikshad
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/ 34

Assignment No.

3
Write an X86/64 ALP to accept a string and to display its length.

%macro disp 2
mov rax ,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

section .data
msg1 db "Enter your string:",0Ah
msglen equ $-msg1

section .bss
str1 resb 200
result resb 16
section .text

global _start
_start:
mov rax,1
mov rdi,1
mov rsi,msg1
mov rdx,msglen
syscall
mov rax,0
mov rdi,0
mov rdi,0
mov rsi,str1
mov rdx,200
syscall
dec rax
mov rbx,rax
call display
mov rax,60
mov rdi,0
syscall

display:
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 skip

add_37:
add al,37h

skip:
mov[rdi],al
inc rdi
dec cx
jnz up1
disp result,16
ret

OUTPUT
Assignment No.4
Write an X86/64 ALP to find the largest of given Byte/Word/Doubleword/64bit
numbers.

section .data
welmsg db 10, 'The maximum number in the array is :',
len1 equ $-welmsg
array dQ 8abc123456781234h, 90ff123456781234h,7700123456781234h
arrcnt equ 3
section .bss
dispbuff resb 2
buf resb 16
%macro print 2
mov eax,4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 0X80
%endmacro

section .text
global _start
_start:
print welmsg, len1
mov esi, array
mov rax, [esi]
mov ecx, arrcnt
up1:
add esi, 8
mov rbx, [esi]
cmp rax, rbx
jnc skip
mov rax, rbx
mov edi, buf
skip:
loop up1
mov rbx, rax
mov edi, buf
mov ecx, 16
dispup1:
rol rbx, 4
mov dl, bl
and dl, 0fh
add dl, 30h
cmp dl, 39h
jbe dispskip1
add dl, 07h
dispskip1:
mov [edi], dl
inc edi
loop dispup1
print buf, 16
ret
Assignment 5
Write X86/64 ALP to count number of positive and negative numbers from the array

Program-

section .data
msg1 db "Count of Positive numbers:"
len1 equ $-msg1
msg2 db "Count of negative numbers:"
len2 equ $-msg2
array db 10,12,-21,-12,-19,-34,41
%macro print 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .bss
count resb 2
pcount resb 2
ncount resb 2
totalcount resb 2

section .text
global_start
_start:

mov byte[count],07
mov byte[pcount],00
mov byte[ncount],00

mov rsi,array
Up:
mov al,00
add al,[rsi]
js neg
inc byte[pcount]
jmp Down
neg:
inc byte[ncount]
Down:
add rsi,01
dec byte[count]
jnz Up
mov bl,[pcount]
mov dl,[ncount]
b1:
print msg1,len1
mov bh,[pcount]
call disp
print msg2,len2
mov bh,[ncount]
call disp
mov rax,60
mov rdi,00
syscall
disp:
mov byte[count],02
loop:
rol bh,04
mov al,bh
AND Al,0FH
cmp al,09
jbe l1
add al,07h
l1:add al,30h
mov[totalcount],al
print totalcount,02
dec byte[count]
jnz loop
ret

Output:
Assignment 6
Write X86/64 ALP to detect protected mode and display the values of GDTR,
LDTR, IDTR, TR and MSW Registers also identify CPU type using CPUID
instruction.
Program –

%macro scall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
Section .data
title: db 0x0A,"----Assignment 6-----", 0x0A
title_len: equ $-title
regmsg: db 0x0A,"***** REGISTER CONTENTS *****"
regmsg_len: equ $-regmsg
gmsg: db 0x0A,"Contents of GDTR : "
gmsg_len: equ $-gmsg
lmsg: db 0x0A,"Contents of LDTR : "
lmsg_len: equ $-lmsg
imsg: db 0x0A,"Contents of IDTR : "
imsg_len: equ $-imsg
tmsg: db 0x0A,"Contents of TR : "
tmsg_len: equ $-tmsg
mmsg: db 0x0A,"Contents of MSW : "
mmsg_len: equ $-mmsg
realmsg: db "---- In Real mode. ----"
realmsg_len: equ $-realmsg
protmsg: db "---- In Protected Mode. ----"
protmsg_len: equ $-protmsg
cnt2:db 04H
newline: db 0x0A
Section .bss
g: resd 1
resw 1
l: resw 1
idtr: resd 1
resw 1
msw: resd 1
tr: resw 1
value :resb 4
Section .text
global _start
_start:
scall 1,1,title,title_len
smsw [msw]
mov eax,dword[msw]
bt eax,0
jc next
scall 1,1,realmsg,realmsg_len
jmp EXIT
next:
scall 1,1,protmsg,protmsg_len
scall 1,1, regmsg,regmsg_len
;printing register contents
scall 1,1,gmsg,gmsg_len
SGDT [g]
mov bx, word[g+4]
call HtoA
mov bx,word[g+2]
call HtoA
mov bx, word[g]
call HtoA
;--- LDTR CONTENTS---- find valid values for all labels after 1001 passes, giving up.
scall 1,1, lmsg,lmsg_len
SLDT [l]
mov bx,word[l]
call HtoA
;---- IDTR Contents -------
scall 1,1,imsg,imsg_len
SIDT [idtr]
mov bx, word[idtr+4]
call HtoA
mov bx,word[idtr+2]
call HtoA
mov bx, word[idtr]
call HtoA
;---- Task Register Contents -0-----
scall 1,1, tmsg,tmsg_len
mov bx,word[tr]
call HtoA
;------- Content of MSW ---------
scall 1,1,mmsg,mmsg_len
mov bx, word[msw+2]
call HtoA
mov bx, word[msw]
call HtoA
scall 1,1,newline,1
EXIT:
mov rax,60
mov rdi,0
syscall
;------HEX TO ASCII CONVERSION METHOD ----------------
HtoA: ;hex_no to be converted is in bx //result is stored in rdi/user defined variable
mov rdi,value
mov byte[cnt2],4H
aup:
rol bx,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
scall 1,1,value,4
ret

Output
Assignment 7
TITLE : Write X86/64 Assembly language program (ALP) to perform non-
overlapped block transfer.

PROGRAM:
%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;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],'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.

PROGRAM:

%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

------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:
Write an X86/64 ALP to perform multiplication of two 8 bit hexadecimal numbers.
Use successive addition and add and shift method.

%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

menu db 10d,13d," MENU For Multiplication"


db 10d,"1. Successive Addition"
db 10d,"2. Shift and Add method"
db 10d,"3. Exit"
db 10d
db 10d,"Enter your choice: "
lnmenu equ $-menu

m1 db 10d,13d,"Enter First Number: "


l1 equ $-m1
m2 db 10d,13d,"Enter Second Number: "
l2 equ $-m2
m3 db 10d,13d,"Answer: "
l3 equ $-m3
nwline db 10d,13d

section .bss
choice resb 2
answer resb 20
num1 resb 20
num2 resb 20
temp resb 20

section .text
global _start
_start:

;**********************MAIN LOGIC****************************
main:
scall 1,1,menu,lnmenu
scall 0,0,choice,2

cmp byte[choice],'3'
jae exit

scall 1,1,m1,l1
scall 0,0,temp,17
call asciihextohex
mov qword[num1],rbx

scall 1,1,m2,l2
scall 0,0,temp,17
call asciihextohex
mov qword[num2],rbx

cmp byte[choice],'1'
je case1
cmp byte[choice],'2'
je case2

exit:
mov rax,60
mov rdi,0
syscall

case1:
mov rbx,qword[num1]
mov rcx,qword[num2]
mov rax,0 ;rax to store answer

cmp rcx,0 ;check multiplication with 0 condition


je skip3

loop1:
add rax,rbx
loop loop1 ;auto-decrement rcx and jmp
skip3:
mov rbx,rax ;backup rax in rbx
scall 1,1,m3,l3
mov rax,rbx ;restore rax which has answer
call display ;display answer from rax register
jmp main

case2:
mov rbx,qword[num1]
mov rdx,qword[num2]
mov rax,0 ;rax to store answer
mov cl,64
; 16(no. of digit) * 4 (no. of bits per digit) = 64
up1:
shl rax,1
rol rbx,1
jnc down1
add rax,rdx
down1:
loop up1
mov rbx,rax

mov rbx,rax ;backup rax in rbx


scall 1,1,m3,l3
mov rax,rbx ;restore rax which has answer
call display ;display answer from rax register

jmp main

;******************PROCEDURES********************************
asciihextohex:
mov rsi,temp
mov rcx,16
mov rbx,0
mov rax,0

loop4: rol rbx,04


mov al,[rsi]
cmp al,39h
jbe skip1
sub al,07h
skip1: sub al,30h

add rbx,rax

inc rsi
dec rcx
jnz loop4
ret

display:
mov rsi,answer+15
mov rcx,16
loop5: mov rdx,0
mov rbx,16
div rbx
cmp dl,09h
jbe skip2

add dl,07h
skip2: add dl,30h
mov [rsi],dl

dec rsi
dec rcx
jnz loop5
scall 1,1,answer,16
ret

OUTPUT:
ASSIGNMENT 10
TITLE : Implement x86 program for File Operation.
PROGRAM:

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

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

jmp next1
next:

scall 1,1,errormsg,errormsg_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,closemsg_len

;-------------------FILE 2 ------------------

scall 1,1,filenmsg,filen_len

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

jmp next21

next3:

scall 1,1,errormsg,errormsg_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,closemsg_len

jmp main

TYPE:scall 1,1,filenmsg,filen_len

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

jmp tnext1

tnext:

scall 1,1,errormsg,errormsg_len

jmp EXIT

tnext1:

scall 0,[fdis],buffer,200 ;reading contents of file in buffer

mov qword[bufferlen],rax

scall 1,1, typemsg,typemsg_len

scall 1,1, buffer,qword[bufferlen]

;Closing file2

mov rax,3

mov rdi,f2name

syscall

scall 1,1,closemsg,closemsg_len

JMP main

EXIT:

mov rax,60

mov rdi,0

syscall
OUTPUT:
Assignment No : 11
Program :- Implement x86 program to find the factorial of a number

section .bss
number: resb 1
temp: resb 1
num: resb 1
n: resw 1
nod: resb 1

section .data

msg1 db "Enter the number : "


size1 equ $-msg1
msg2 db "factorial of number: "
size2 equ $-msg2

section .text
global _start

_start:

;Printing the message to enter the number


mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, size1
int 80h

;Reading the number


mov eax, 3
mov ebx, 0
mov ecx, number
mov edx, 1
int 80h

sub byte[number], 30h


mov al, byte[number]
mov ah,0
mov word[n],ax

call fact

mov word[num],cx
mov byte[nod], 0 ;No of digits...

;to display msg2


mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, size2
int 80h

extract_no:

cmp word[num], 0
je print_no
inc byte[nod]
mov dx, 0
mov ax, word[num]
mov bx, 10
div bx
push dx
mov word[num], ax
jmp extract_no

print_no:
cmp byte[nod], 0
je end_print
dec byte[nod]
pop dx
mov byte[temp], dl
add byte[temp], 30h
mov eax, 4
mov ebx, 1
mov ecx, temp
mov edx, 1
int 80h
jmp print_no

end_print:
; for new line
mov eax, 1
mov ebx, 0
int 80h

fact:

mov ax, word[n]


cmp ax, 0
je terminate
push word[n]

dec word[n]
call fact

pop word[n]
mov dx, word[n]

mov ax, cx
mul dx
mov cx, ax
jmp exit

terminate:
mov cx, 1

exit:
ret

OUTPUT:-

You might also like