Final Organized
Final Organized
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
l2 equ $-m2
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
l5 equ $-m5
l6 equ $-m6
m7 db 10
time equ 5
size equ 8
section .bss
2|Page
_input resb 20
_output resb 20
count resb 1
section .text
global _start
_start:
IO 1,1,m3,l3
IO 1,1,m4,l4
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
add rbp,size ; move to next value of array 8 -> 4*2 = 1 place -> arr[n+1]
jnz input
jmp display
display:
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]
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
letter:
jge error ;
jbe skip
skip:
jnz letter
ret
hex_to_ascii:
letter2:
jbe add30 ;if yes simply add 30h to get the ascii
add30:
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
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
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
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:
mov byte[counter],05
mov rsi,array
next: mov al,[rsi]
push rsi
call disp
pop rsi
inc rsi
dec byte[counter]
jnz next
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
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 accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
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]
disp msg,len
;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
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
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
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:
mov rsi,sourceBlock
mov rdi,destBlock
mov rax,60
mov rdi,0
syscall
dispBlock:
mov rbp,count
next:mov al,[rsi]
push rsi
call disp
pop rsi
inc rsi
dec rbp
jnz next
ret
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
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 ""
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 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
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
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
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
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
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 rdi,0
mov rsi,cho
mov rdx,2
syscall
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
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
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
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
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
;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
syscall
jmp exit
;<-------------------PRINT FILE------------------------>
NoFile :
print msg,len
jmp exit
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
msg3: db "Spaces:",0x0A
len3: equ $-msg3
msg4: db "NewLines:",0x0A
len4: equ $-msg4
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:
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:
scall 1,1,scount, 2
scall 1,1,new,new_lenret
5 | Page
enters:
mov rsi,buffer
up2:
je next5
inc rsi
dec byte[cnt2]
jnz up2
jmp next6
next5:
inc rsi
inc byte[ncount]
dec byte[cnt2]
jnz up2
next6:
scall 1,1,ncount, 2
scall 1,1,new,new_len
ret
occ:
mov rsi,buffer
up3:
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:
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
;-------------------------.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
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 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:
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]