0% found this document useful (0 votes)
163 views70 pages

Final Organized

This program writes an X86/64 assembly language program that uses a switch case structure to perform 64-bit hexadecimal arithmetic operations (addition, subtraction, multiplication, division) based on user input. It defines macros for input/output and sections to declare constants, variables, and functions. Based on the user's choice, it will call the corresponding function to perform the operation and display the result.
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)
163 views70 pages

Final Organized

This program writes an X86/64 assembly language program that uses a switch case structure to perform 64-bit hexadecimal arithmetic operations (addition, subtraction, multiplication, division) based on user input. It defines macros for input/output and sections to declare constants, variables, and functions. Based on the user's choice, it will call the corresponding function to perform the operation and display the result.
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/ 70

1|Page

PRACTICAL NO: 1
Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and
store them in an array and display the accepted numbers.

CODE:
%macro IO 4

mov rax,%1

mov rdi,%2

mov rsi,%3

mov rdx,%4

syscall

%endmacro

section .data

m1 db "Enter the five 64 bit numbers:" ,10 ; 10d -> line feed

l1 equ $-m1

m2 db "The five 64 bit numbers are:" ,10

l2 equ $-m2

m3 db "rahul ghosh 3236" ,10

l3 equ $-m3

m4 db "Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store them in an array
and display the accepted numbers." ,10d

l4 equ $-m4

m5 db 10,"Exiting now" ,10

l5 equ $-m5

m6 db "incorrect input error" ,10

l6 equ $-m6

m7 db 10

debug db "debug "

debug_l equ $-debug

time equ 5

size equ 8

section .bss
2|Page

arr resb 300

_input resb 20

_output resb 20

count resb 1

section .text

global _start

_start:

IO 1,1,m3,l3

IO 1,1,m4,l4

mov byte[count],time ; store time = 5 in count;

mov rbp,arr ;rbp points to begining of arr

IO 1,1,m1,l1

input:

IO 0,0,_input,17

IO 1,1,debug,debug_l

IO 1,1,_input,17

call ascii_to_hex

mov [rbp],rbx ; put the complete summed rbx value to arr[n]

add rbp,size ; move to next value of array 8 -> 4*2 = 1 place -> arr[n+1]

dec byte[count] ; loop

jnz input

mov byte[count],time ; set loop count to 5

mov rbp,arr ;make rbp point to arr beginning

jmp display

display:

mov rax,[rbp] ; address of rbp in rax

call hex_to_ascii

IO 1,1,m7,1

IO 1,1,_output,16

add rbp,size ; move to next value of array 8 -> 4*2 = 1 place arr[n+1]

dec byte[count] ; loop


3|Page

jnz display

jmp exit

exit:

IO 1,1,m5,l5

mov rax,60

mov rdi,0

syscall

error:

IO 1,1,m6,l6

jmp exit

ascii_to_hex:

mov rsi,_input

mov rcx,16

xor rbx,rbx ;cleaning rbx since rbx == rbx , rbx is set to 0without wasting the space

xor rax,rax ;cleaning rax

letter:

rol rbx,4 ; shifting rbx to left by 4 bytes

mov al,[rsi] ; adrress of rsi (_input ) in al _input[0]

cmp al,47h ; error checking

jge error ;

cmp al,39h ;if < ascii 39 => 0-9

jbe skip

sub al,07h ;else => ascii is (A-F)

skip:

sub al,30h ; get value between 0-9

add rbx,rax ; add generated hex value to rbx

inc rsi ; now rsi points at _input[n+1]

dec rcx ; loop

jnz letter

ret

hex_to_ascii:

mov rsi,_output+15 ;max display of 16 characters and rsi points to _output[16]

mov rcx,16 ;loop runs 16 times


4|Page

letter2:

xor rdx,rdx ;cleaning rdx need dl for division remainder and

mov rbx,16 ;base 16

div rbx ;dividing by base 16

cmp dl,09h ;checking if hex value < 9

jbe add30 ;if yes simply add 30h to get the ascii

add dl,07h ;else => (A-F) so add 7 to make it 37 total

add30:

add dl,30h ;common step of adding 30h

mov [rsi],dl ;move generated ascii to _output[n]

dec rsi ;rsi points to _output[n-1]

dec rcx ;loop

jnz letter2

ret
5|Page

OUTPUT:
rahul ghosh 3236
Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from user and store
them in an array and display the accepted numbers.
Enter the five 64 bit numbers:
debug 55555555A5555555
debug 44444444F4444434
debug 33333D3333333333
debug 2222222222E22222
debug 1111111111111111
55555555A5555555
44444444F4444434
33333D3333333333
2222222222E22222
1111111111111111
Exiting now

[Execution complete with exit code 0]


1 | Page

PRACTICAL NO: 2
Write an X86/64 ALP to accept a string and to display its length.
CODE:
%macro IO 4 ; simple macro ,
mov rax,%1 ; param 1 -> system call number
mov rdi,%2 ; param 2 -> file descriptor
mov rsi,%3 ; param 3 -> message
mov rdx,%4 ; param 4 -> length
syscall
%endmacro
section .data
m1 db "enter string",10 ;10 ->line feed
l1 equ $-m1
m2 db "Entered",10
l2 equ $-m2
m3 db "Length is ",10
l3 equ $-m3
m4 db "Write an X86/64 ALP to accept a string and to display its length" ,10
l4 equ $-m4
m5 db "Exiting now" ,10
l5 equ $-m5
m6 db "rahul ghosh 3236" ,10
l6 equ $-m6
m7 db 10
section .bss
string resb 50 ;string array of size 50
strl equ $-string
count resb 1
_output resb 20
section .text
global _start

_start:
IO 1,1,m6,l6 ; display
IO 1,1,m4,l4 ; display
IO 1,1,m1,l1 ; display
input:
IO 0,0,string,strl
mov [count],rax ;count now points to rax
output:
IO 1,1,m2,l2 ; display
IO 1,1,string,strl
IO 1,1,m3,l3 ; display
mov rax,[count] ; value of count passed into rax
call hex_to_dec
IO 1,1,_output,16
IO 1,1,m7,1
exit:
IO 1,1,m5,l5
2 | Page

mov rax, 60 ; system call for exit


mov rdi, 0 ; exit code 0
syscall

hex_to_dec:
mov rsi,_output+15 ; max size of display , for convinience set to 16 and rsipoints to output[16]
mov rcx,2 ; loop count , or number of digits displayed , 2 digits (unlikely we will enter string >
99)(prints preceding zeros otherwise)
letter2:
xor rdx,rdx ; setting rdx to null without setting a null byte (a tip i saw on reddit) needed to clean dl for
use
mov rbx,10 ; conversion base
div rbx ; dividing by conversion base
cmp dl,09h ; comparing 09h with dl , since the division remainder ends up in dx and since its one
digits its in dl
jbe add30 ; if value under in 0-9 , we directly add 30h to get ascii 0-9
add30:
add dl,30h ; adding 30h
mov [rsi],dl ; moving the ascii we generated to rsi
dec rsi ; rsi now points to the next place in display or output[n-1]
dec rcx ; loop
jnz letter2
ret
3 | Page

OUTPUT:
rahul ghosh 3236
Write an X86/64 ALP to accept a string and to display its length
enter string
Entered
Rahul ghosh
Length is
12
Exiting now
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 3
Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit
numbers.
CODE:
section .data
array db 11h,59h,33h,22h,44h

msg1 db 10,"ALP to find the largest number in an array",10


msg1_len equ $ - msg1

msg2 db 10,"The Array contains the elements : ",10


msg2_len equ $ - msg2

msg3 db 10,10, "The Largest number in the array is : ",10


msg3_len equ $ - msg3

section .bss
counter resb 1
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 msg1 , msg1_len


write msg2 , msg2_len

mov byte[counter],05
mov rsi,array
next: mov al,[rsi]
push rsi
call disp
pop rsi
inc rsi
dec byte[counter]
jnz next

write msg3 , msg3_len


2 | Page

mov byte[counter],05
mov rsi, array
mov al, 0 ; al is an 8 bit register , al stores max
repeat: cmp al,[rsi] ;cmp opr1 , opr2 : opr1 - opr2
jg skip
mov al,[rsi]
skip: inc rsi
dec byte[counter]
Jnz repeat

call disp

mov rax,60
mov rdi,1
syscall

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
3 | Page

OUTPUT:
ALP to find the largest number in an array
The Array contains the elements :
1159332244
The Largest number in the array is :
59
[Execution complete with exit code 1]
1 | Page

PRACTICAL NO: 4
Write a switch case driven X86/64 ALP to perform 64 -bit hexadecimal
arithmetic operations (+,-,*, /) using suitable macros. Define procedure for
each operation
CODE:
%macro IO 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
section .data
m1 db "enter choice (+,-,*, /)" ,10 ; 10d -> line feed
l1 equ $-m1
m2 db "Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations (+,-,*, /)
using suitable macros. Define procedure for each operation." ,10
l2 equ $-m2
m3 db "rahul ghosh 3236" ,10
l3 equ $-m3
madd db "addition here" ,10
l4 equ $-madd
msub db "subtraction here" ,10
l5 equ $-msub
mmul db "multiplication here" ,10
l6 equ $-mmul
mdiv db "division here" ,10
l7 equ $-mdiv
mspace db 10
m_result db "result is "
m_result_l equ $-m_result
m_qou db "qoutient is "
m_qou_l equ $-m_qou
m_rem db "remainder is "
m_rem_l equ $-m_rem
m_default db "enter correct choice",10
m_default_l equ $-m_default
section .bss
choice resb 2
_output resq 1
_n1 resq 1
_n2 resq 1
temp_1 resq 1
temp_2 resq 1
section .text
global _start
_start:
IO 1,1,m2,l2
2 | Page

IO 1,1,m3,l3
IO 1,1,m1,l1
IO 0,0,choice,2
cmp byte [choice],'+'
jne case2
call add_fun
jmp exit
case2:
cmp byte [choice],'-'
jne case3
call sub_fun
jmp exit
case3:
cmp byte [choice],'*'
jne case4
call mul_fun
jmp exit
case4:
cmp byte [choice],'/'
jne case5
call div_fun
jmp exit
case5:
cmp byte [choice],'a'
jne error
call add_fun
call sub_fun
call mul_fun
call div_fun
jmp exit
error:
IO 1,1,m_default,m_default_l
jmp exit
exit:
mov rax, 60
mov rdi, 0
syscall
add_fun:
IO 1,1,madd,l4
mov qword[_output],0
IO 0,0,_n1,17
IO 1,1,_n1,17
call ascii_to_hex
add qword[_output],rbx
IO 0,0,_n1,17
IO 1,1,_n1,17
call ascii_to_hex
add qword[_output],rbx
mov rbx,[_output]
IO 1,1,mspace,1
IO 1,1,m_result,m_result_l
3 | Page

call hex_to_ascii
ret
sub_fun:
IO 1,1,msub,l5
mov qword[_output],0
IO 0,0,_n1,17
IO 1,1,_n1,17
;IO 1,1,mspace,1
call ascii_to_hex
add qword[_output],rbx
IO 0,0,_n1,17
IO 1,1,_n1,17
;IO 1,1,mspace,1
call ascii_to_hex
sub qword[_output],rbx
mov rbx,[_output]
IO 1,1,mspace,1
IO 1,1,m_result,m_result_l
call hex_to_ascii

ret
mul_fun:
IO 1,1,mmul,l6 ; message
IO 0,0,_n1,17 ; n1 input
IO 1,1,_n1,17
call ascii_to_hex; conversion returns hex value in rbx
mov [temp_1],rbx ; storing hex in temp_1
IO 0,0,_n1,17 ;n2 input
IO 1,1,_n1,17
call ascii_to_hex
mov [temp_2],rbx ; putting hex of n2 in temp_2
mov rax,[temp_1] ; temp_1->rax
mov rbx,[temp_2] ;temp_2->rbx
mul rbx ; multiplication
push rax
push rdx
IO 1,1,mspace,1
IO 1,1,m_result,m_result_l
pop rdx
mov rbx,rdx; setting rbx value for conversion
call hex_to_ascii
pop rax
mov rbx,rax; setting rbx value for conversion
call hex_to_ascii ; final output
ret
div_fun:
IO 1,1,mdiv,l7
IO 0,0,_n1,17 ; n1 input
IO 1,1,_n1,17
call ascii_to_hex; conversion returns hex value in rbx
mov [temp_1],rbx ; storing hex in temp_1
4 | Page

IO 0,0,_n1,17 ;n2 input


IO 1,1,_n1,17
call ascii_to_hex
mov [temp_2],rbx ; putting hex of n2 in temp_2
mov rax,[temp_1] ; temp_1->rax
mov rbx,[temp_2] ;temp_2->rbx
xor rdx,rdx
mov rax,[temp_1] ; temp_1->rax
mov rbx,[temp_2] ; temp_2->rbx
div rbx ; div
push rax
push rdx
IO 1,1,mspace,1
IO 1,1,m_rem,m_rem_l
pop rdx
mov rbx,rdx
call hex_to_ascii; remainder output
IO 1,1,mspace,1
IO 1,1,m_qou,m_qou_l
pop rax
mov rbx,rax
call hex_to_ascii; quotient output
ret
ascii_to_hex:
mov rsi, _n1
mov rcx, 16
xor rbx, rbx
next1:
rol rbx, 4
mov al, [rsi]
cmp al,47h
jge error
cmp al, 39h
jbe sub30h
sub al, 7
sub30h:
sub al, 30h
add bl, al
inc rsi
loop next1
ret
hex_to_ascii:
mov rcx, 16
mov rsi,_output
next2:
rol rbx, 4
mov al, bl
and al, 0Fh
cmp al, 9
jbe add30h
add al, 7
5 | Page

add30h:
add al, 30h
mov [rsi], al
inc rsi
loop next2
IO 1,1,_output,16
IO 1,1,mspace,1
ret
6 | Page

OUTPUT:
Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic
operations (+,-,*, /) using suitable macros. Define procedure for each operation.
rahul ghosh 3236
enter choice (+,-,*, /)
multiplication here
0000000000000004
0000000000000002
result is 0000000000000000
0000000000000008
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 5
Write X86/64 ALP to count number of positive and negative numbers from the
array
CODE:
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]
2 | Page

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
3 | Page

OUTPUT:
Count of Positive numbers:03Count of negative numbers:04
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 6
Write 64 bit 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.
Display proper strings to prompt the user while accepting the input
and displaying the result. (use of 64-bitregisters is expected)
CODE:
;--------------------------------Assignment No. 3-----------------------
; 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.

global _start

_start:

section .text

; macro for system call for write


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

; macro for system call for read

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

;---------First Choice Hex to BCD----------------


ch1:
; accept numbers
disp msg1,len1
accept num,02
2 | Page

call convert
mov [no.1],al
accept num,03
call convert
mov [no.2],al
disp msg2,len2

; Form ax as input
mov ah,[no.1]
mov al,[no.2]

;Point esi to predefined array in .data


mov esi,array1

; Hex to BCD conversion


l5:
mov dx,0000h
mov bx,[esi]
div bx
mov [rem],dx
mov [t1],al
push rsi
call disp_proc
pop rsi
inc esi
inc esi
mov ax,[rem]
dec byte[cnt]
jnz l5

disp msg,len

;To exit program.


ch3:
mov rax,60
mov rdi,0
syscall

;CONVERT procedure
convert:
mov esi,num
mov al,[esi]
cmp al,39h
jle l1
sub al,07h
l1: sub al,30h
rol al,04h ;to swap number
3 | Page

mov bl,al

inc esi
mov al,[esi]
cmp al,39h
jle l2
sub al,07h
l2: sub al,30h

add al,bl
mov [t1],al
ret

;CONVERT2 procedure
convert2:
mov al,[num]
cmp al,39h
jle l8
sub al,07h
l8:sub al,30h
ret

;DISPLAY procedure
disp_proc:
;for unt's place
mov al,[t1]
cmp al,09h
jle l4
add al,07h
l4:add al,30h
mov [t2],al
disp t2,1
ret

;DISPLAY@ procedure
display2:

mov rsi,charans+3
mov rcx,04h

l12: mov rdx,0


mov rbx,10h
div rbx
cmp dl,09h
jle l3
add dl,07h
l3:add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz l12
4 | Page

mov rax,1
mov rdi,1
mov rsi,charans
mov rdx,4
syscall

ret

section .data
msg: db "",10
len: equ $-msg
msg1: db "Enter Hex number : ",10
len1: equ $-msg1
msg2: db "BCD equivalent is : ",10
len2: equ $-msg2
msg3: db "#####MENU#####",10
db "1.Hex to BCD.",10
db "2.BCD to Hex.",10
db "3.Exit.",10
len3: equ $-msg3
msg4: db "Enter your choice : ",10
len4: equ $-msg4
msg5: db "Enter BCD number : ",10
len5: equ $-msg5
msg6: db "Hex equivalent is : ",10
len6: equ $-msg6

array1 dw 2710h,03E8h,0064h,000Ah,0001h
cnt db 5
cnt2 db 5

section .bss
num resb 03
no.1 resb 02
no.2 resb 02
t1 resb 03
t2 resb 03
t3 resb 03
rem resw 02
result resw 03
choice resb 03
charans resb 08
5 | Page

OUTPUT:
Enter Hex number :
BCD equivalent is :
65535
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 7
Write X86/64 ALP to switch from real mode to protected mode and
display the values of GDTR, LDTR, IDTR, TR and MSW
Registers
CODE:
section .data

msg1:db 'GDTR contents :',0xa


len1:equ $-msg1

msg2:db 'LDTR contents:',0xa


len2:equ $-msg2

msg3:db 'IDTR contents :',0xa


len3:equ $-msg3

msg4:db 'TR contents:',0xa


len4:equ $-msg4

msg5:db 'MSW contents:',0xa


len5:equ $-msg5

msg6:db 'We are in protected mode.!!',0xa


len6:equ $-msg6

msg7:db ' ',0xa


len7:equ $-msg7

msg8:db 'We are not in protected mode.!!',0xa


len8:equ $-msg8

msg9:db ' : ',0xa


len9:equ $-msg9

section .bss

gdt:resd 01
resw 01
ldt:resw 01
idt: resd 01
resw 01
tr:resw 01
msw:resw 01

result: resw 01

section .text
2 | Page

global _start
_start:

smsw [msw]
sgdt [gdt]
sldt [ldt]
sidt [idt]
str [tr]

mov ax,[msw]
bt ax,0
jc next
mov rax,1
mov rdi,1
mov rsi,msg8
mov rdx,len8
syscall
jmp exit

next:
mov rax,1
mov rdi,1
mov rsi,msg6
mov rdx,len6
syscall

;GDTR

mov rax,1
mov rdi,1
mov rsi,msg1
mov rdx,len1
syscall

mov bx,word[gdt+4]
call HtoA
mov bx,word[gdt+2]
call HtoA

mov rax,1
mov rdi,1
mov rsi,msg9
mov rdx,len9
syscall

mov bx,word[gdt]
call HtoA
3 | Page

;LDTR

mov rax,1
mov rdi,1
mov rsi,msg7
mov rdx,len7
syscall

mov rax,1
mov rdi,1
mov rsi,msg2
mov rdx,len2
syscall

mov bx,word[ldt]
call HtoA

;IDTR

mov rax,1
mov rdi,1
mov rsi,msg7
mov rdx,len7
syscall

mov rax,1
mov rdi,1
mov rsi,msg3
mov rdx,len3
syscall

mov bx,word[idt+4]
call HtoA
mov bx,word[idt+2]
call HtoA

mov rax,1
mov rdi,1
mov rsi,msg9
mov rdx,len9
syscall

mov bx,word[idt]
call HtoA

;TR

mov rax,1
mov rdi,1
mov rsi,msg7
4 | Page

mov rdx,len7
syscall

mov rax,1
mov rdi,1
mov rsi,msg4
mov rdx,len4
syscall

mov bx,word[tr]
call HtoA

;MSW

mov rax,1
mov rdi,1
mov rsi,msg7
mov rdx,len7
syscall

mov rax,1
mov rdi,1
mov rsi,msg5
mov rdx,len5
syscall

mov bx,word[msw]
call HtoA

;EXIT
exit:
mov rax,60
mov rdi,0
syscall

HtoA:
mov rcx,4
mov rdi,result
dup1:
rol bx,4
mov al,bl
and al,0fh
cmp al,09h
jg p3
add al,30h
jmp p4
p3: add al,37h
p4:mov [rdi],al
inc rdi
loop dup1
5 | Page

mov rax,1
mov rdi,1
mov rsi,result
mov rdx,4
syscall

ret
6 | Page

OUTPUT:
We are in protected mode.!!
GDTR contents :
00001000 :
007F
LDTR contents:
0000
IDTR contents :
00000000 :
0FFF
TR contents:
0040
MSW contents:
FFFF
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 8
Write X86/64 ALP to perform non-overlapped block transfer
without string specific instructions. Block containing data can be
defined in the data segment.
CODE:
section .data
sourceBlock db 12h,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 resb 5
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
2 | Page

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
3 | Page

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
4 | Page

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
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 9
Write X86/64 ALP to perform overlapped block transfer with stringspecific
instructions. Block containing data can be defined in the
data segment.
CODE:
section .data
nline db 10,10
nline_len equ $-nline

space db ""

ano db 10," Assignment no :2-A",


db 10,"-------------------------------------------------------------------",
db 10," Block Transfer-Non overlapped without String instruction.",
db 10,"-------------------------------------------------------------------",10
ano_len equ $-ano

bmsg db 10,"Before Transfer::"


bmsg_len equ $-bmsg

amsg db 10,"After Transfer::"


amsg_len equ $-amsg

smsg db 10," Source Block :"


smsg_len equ $-smsg

dmsg db 10," Destination Block :"


dmsg_len equ $-dmsg

sblock db 11h,22h,33h,44h,55h
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
2 | Page

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

Print amsg,amsg_len ;Block values after transfer

Print smsg,smsg_len
mov rsi,sblock
call disp_block

Print dmsg,dmsg_len
mov rsi,dblock
call disp_block

Exit
;-----------------------------------------------------------------
BT_NO:
mov rsi,sblock
mov rdi,dblock
mov rcx,5

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


3 | Page

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

inc rsi
inc rdi

dec rcx
jnz back
RET
;-----------------------------------------------------------------
disp_block:
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
4 | Page

OUTPUT:
-------------------------------------------------------------------
Block Transfer-Non overlapped without String instruction.
-------------------------------------------------------------------
Before Transfer::
Source Block :11 22 33 44 55
Destination Block :00 00 00 00 00
After Transfer::
Source Block :11 22 33 44 55
Destination Block :11 22 33 44 55

[Execution complete with exit code 0]


1 | Page

PRACTICAL NO: 10
Write X86 menu driven Assembly Language Program (ALP) to
implement OS (DOS) commands TYPE, COPY and DELETE
using file operations. User is supposed to provide command line
arguments in all cases
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 ;; read choice


2 | Page

mov rdi,0
mov rsi,cho
mov rdx,2
syscall

cmp byte[cho],31h ;; comparing choice


je a

cmp byte[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: ;; ASCII to Hex conversion


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: sub al,30h
p2: add bl,al
inc rsi
loop up1
ret

display: ;; Hex to ASCII conversion


mov rcx,4
3 | Page

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
4 | Page

mov rax,0
mov rdi,0
mov rsi,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
mov rdx,res_len
syscall

mov rbx,[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
mov rsi,num
mov rdx,3
syscall
5 | Page

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
mov rsi,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
mov al,[num1]
mov bl,[num]

p11:
shr bx,01
jnc p
add cx,ax
p:
shl ax,01
dec dl
jnz p11

mov [result],rcx

mov rax,1
mov rdi,1
mov rsi,res
mov rdx,res_len
6 | Page

syscall

;dispmsg res,res_len

mov rbx,[result]
call display

ret
7 | Page

OUTPUT:
Enter your Choice:
1.Successive Addition
2.Add and Shift method
3.Exit
Enter two digit Number::
Enter two digit Number::
Multiplication of elements is::0121Enter your Choice:
1.Successive Addition
2.Add and Shift method
3.Exit
[Execution complete with exit code 0]
1 | Page

PRACTICAL NO: 11
Write X86 menu driven Assembly Language Program (ALP) to
implement OS (DOS) commands TYPE, COPY and DELETE
using file operations. User is supposed to provide command line
arguments in all cases
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 : resb 16
filename_2 : resb 16
choice : resb 8

section .txt
global _start
%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,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

_start:
2 | Page

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
cmp byte[rbx],0H
jne up_1

pop rbx
mov rsi,filename_2
up_2: mov al,byte[rbx]
mov byte[rsi],al
inc rbx
inc rsi
cmp byte[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],rax ; if exists the saving hte file descriptor

;OPENING SECOND FILE


3 | Page

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
mov rdi,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
4 | Page

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
mov rdi,qword[filedes_1]
mov rsi,buffer
mov rdx,1000
syscall

;CLOSING THE FILES


mov rax,3
mov rdi,filedes_1
5 | Page

syscall

jmp exit

;<-------------------PRINT FILE------------------------>
NoFile :
print msg,len
jmp exit

;<--------------TERMINATING THE PROGRAM--------------->


exit:
mov rax,60
mov rdi,0
syscall

; create three file


;a.txt with data
;b.txt
;c.txt to delete

; nasm -f elf64 p.asm


; ld -o p p.o
; ./p COPY a.txt b.txt
;./p TYPE a.txt
;./p DELETE c.txt
6 | Page

OUTPUT:
[Execution complete with exit code -11]
1 | Page

PRACTICAL NO: 12
Write X86 ALP to find,
a) Number of Blank spaces
b) Number of
lines
c) Occurrence of a particular character. Accept the data from
the text file.
The text file has to be accessed during Program_1
execution and write FAR PROCEDURES in Program_2 for the rest
of the processing. Use of PUBLIC and EXTERN directives is
mandatory
CODE:
P1.asm

section .data

global msg6,len6,scount,ncount,chacount,new,new_len

fname: db 'abc.txt',0

msg: db "File opened successfully",0x0A


len: equ $-msg

msg1: db "File closed successfully",0x0A


len1: equ $-msg1

msg2: db "Error in opening file",0x0A


len2: equ $-msg2

msg3: db "Spaces:",0x0A
len3: equ $-msg3

msg4: db "NewLines:",0x0A
len4: equ $-msg4

msg5: db "Enter character",0x0A


len5: equ $-msg5

msg6: db "No of occurances:",0x0A


len6: equ $-msg6

new: db "",0x0A
new_len: equ $-new
2 | Page

scount: db 0
ncount: db 0
ccount: db 0
chacount: db 0

section .bss

global cnt,cnt2,cnt3,buffer

fd: resb 17
buffer: resb 200
buf_len: resb 17
cnt: resb 2
cnt2: resb 2
cnt3: resb 2
cha: resb 2

%macro scall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro

section .text
global _start
_start:

extern spaces,enters,occ

mov rax,2
mov rdi,fname
mov rsi,2
mov rdx,0777
syscall

mov qword[fd],rax
BT rax,63
jc next
scall 1,1,msg,len
jmp next2
next:
scall 1,1,msg2,len2

next2:
scall 0,[fd],buffer, 200
mov qword[buf_len],rax
mov qword[cnt],rax
mov qword[cnt2],rax
mov qword[cnt3],rax
3 | Page

scall 1,1,msg3,len3
call spaces

scall 1,1,msg4,len4
call enters

scall 1,1,msg5,len5
scall 0,1,cha,2
mov bl, byte[cha]
call occ
jmp exit

exit:
mov rax,60
mov rdi,0
syscall

P2.asm

section .data

extern msg6,len6,scount,ncount,chacount,new,new_len

section .bss

extern cnt,cnt2,cnt3,scall,buffer

%macro scall 4

mov rax,%1

mov rdi,%2

mov rsi,%3

mov rdx,%4

syscall

%endmacro
4 | Page

section .text

global main2

main2:

global spaces,enters,occ

spaces:

mov rsi,buffer

up:

mov al, byte[rsi]

cmp al,20H

je next3

inc rsi

dec byte[cnt]

jnz up

jmp next4

next3:

inc rsi

inc byte[scount]

dec byte[cnt]

jnz up

next4:

add byte[scount], 30h

scall 1,1,scount, 2

scall 1,1,new,new_lenret
5 | Page

enters:

mov rsi,buffer

up2:

mov al, byte[rsi]


c
mp al,0AH

je next5

inc rsi

dec byte[cnt2]

jnz up2

jmp next6

next5:

inc rsi

inc byte[ncount]

dec byte[cnt2]

jnz up2

next6:

add byte[ncount], 30h

scall 1,1,ncount, 2

scall 1,1,new,new_len
ret

occ:

mov rsi,buffer

up3:

mov al, byte[rsi]

cmp al,bl
6 | Page

je next7

inc rsi

dec byte[cnt3]

jnz up3

jmp next8

next7:

inc rsi

inc byte[chacount]

dec byte[cnt3]

jnz up3

next8:

add byte[chacount], 30h

scall 1,1,msg6,len6

scall 1,1,chacount, 1

scall 1,1,new,new_len
ret

abc.txt
Hello
Welcome to Pune
This is sumit
7 | Page

OUTPUT:
;***********output**********
; nasm -f elf64 p1 p1.asm
; nasm -f elf64 p2 p2.asm
; ld -o p p1.o p2.o .
; ./p .
;File opened successfully.
;Spaces :
;6 .
;NewLines: .
;3 .
;Enter character .
;s .
;No of occurances: .
;3 .
1 | Page

PRACTICAL NO: 13
Write x86 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.
CODE:
;Problem Statement:Write an x86 ALP to find the factorial of a given integer number
;on a command line by using recursion.Explicit stack manipulation is expected in code
;-------------------------.data section------------------------------
section .data
nummsg db "***Program to find Factorial of a number*** ",10
db "Enter the number : ",
nummsg_len equ $-nummsg

resmsg db "Factorial is : "


resmsg_len equ $-resmsg

thankmsg db 10,"Thank you ",10


thankmsg_len equ $-thankmsg

zerofact db " 00000001 "


zerofactlen equ $-zerofact

;-------------------------.bss section------------------------------
section .bss

dispbuff resb 16
result resb 4
num resb 1
num1 resb 1
numascii resb 3

%macro display 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

;------------------------.text section -----------------------------


2 | Page

section .text

global _start

_start:

display nummsg,nummsg_len
accept numascii,3 ;accept number from user
call packnum8_proc ;convert number from ascii to hex
mov [num],bl

display resmsg,resmsg_len

mov al,[num] ;store number in accumulator


cmp al,01h
jbe endfact

mov bl,[num]
call proc_fact
mov rbx,rax
call disp64_proc
jmp exit

endfact:
display zerofact,zerofactlen

exit:
display thankmsg,thankmsg_len

mov rax,60
mov rdi,0
syscall
ret

;-------------------------------------------------------------
disp64_proc:

mov rdi,dispbuff ;point esi to buffer


mov rcx,16 ;load number of digits to display

dispup1:
rol rbx,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 dispskip1 ;if less than 39h akip adding 07 more
add dl,07h ;else add 07
3 | Page

dispskip1:
mov [rdi],dl ;store ASCII code in buffer
inc rdi ;point to next byte
loop dispup1 ;decrement the count of digits to display
;if not zero jump to repeat

display dispbuff,16
ret
;--------------------------------------------------------------------
packnum8_proc:

mov bx,0
mov ecx,02
mov esi,numascii
up1:
rol bl,04
mov al,[esi]
cmp al,39h
jbe skip1
sub al,07h
skip1:
sub al,30h
add bl,al
inc esi
loop up1
ret
;***********************Recursion*************************************
;There are two kinds of recursion: direct and indirect.
;In direct recursion, the procedure calls itself and
;in indirect recursion, the first procedure calls a second
;procedure,which in turn, calls the first procedure.

proc_fact:
cmp bl, 1
jne do_calculation
mov ax, 1
ret
do_calculation:
push rbx
dec bl
call proc_fact
pop rbx
mul bl
ret

;Name:Abdulmuiz Shaikh
;Roll No.:2101062
;Division:SE (A)
4 | Page

OUTPUT:
***Program to find Factorial of a number***
Enter the number : Factorial is : 0000000000000078
Thank you
[Execution complete with exit code 0]

You might also like