Uploaded by Nishant More

micropractical (1)

advertisement
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.
PROGRAM:
%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 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
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;
1
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
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]
2
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
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
OUTPUT:
3
Write an X86/64 ALP to accept a string and to display its length
PROGRAM:
%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,10
l4 equ $-m4
m5 db "Exiting now" ,10
l5 equ $-m5
m6 db "Nishant more 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
4
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
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
OUTPUT:
5
Write an X86/64 ALP to find the largest of given Byte/Word/Dword/64-bit numbers.
PROGRAM:
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
6
call disp
pop rsi
inc rsi
dec byte[counter]
jnz next
write msg3 , msg3_len
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
7
write result , 4
ret
OUTPUT:
8
Write an X86/64 ALP to count number of positive and negative numbers from the array.
PROGRAM:
section .data
welmsg db 10,
welmsg_len equ $-welmsg
pmsg db 10,
pmsg_len equ $-pmsg
nmsg db 10,
nmsg_len equ $-nmsg
nwline db 10
array dw 9000h,8001h,9002h,4553h,8006h,9004h,022h
arrcnt equ 7
pcnt db 0
ncnt db 0
section .bss
dispbuff resb 2
%macro print 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
section .text
global _start
_start:
print welmsg,welmsg_len
mov rsi,array
mov ecx,arrcnt
up1:
bt word[rsi],15
9
jnc pnxt
inc byte[ncnt]
jmp pskip
pnxt: inc byte[pcnt]
pskip: inc rsi
inc rsi
loop up1
print pmsg,pmsg_len
mov bl,[pcnt]
call disp8num
print nmsg,nmsg_len
mov bl,[ncnt]
call disp8num
print nwline,1
exit:
mov rax,60
syscall
disp8num:
mov rcx,2
mov rdi,dispbuff
dup1:
rol bl,4
mov al,bl
and al,0fh ;Mask upper digit
cmp al,09
;Compare with 9
jbe dskip
;If number below or equal to 9 go to add only 30h
add al,07h ;Else first add 07h
dskip: add al,30h ;Add 30h
mov [rdi],al
;Store ASCII code in temp buff
inc rdi
;Increment pointer to next location in temp buff
loop dup1
;repeat till ecx becomes zero
print dispbuff,2 ;display the value from temp buff
ret
;return to calling program
10
OUTPUT:
11
Write X85/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. Display proper strings to prompt the user while
accepting the input and displaying the result. (Wherever necessary, use 64-bit registers)
PROGRAM:
.model small
.data
num1 db ?
num db ?
menu db 0ah,0dh,'--------Menu-------'
db 0ah,0dh,'1.HEX To BCD'
db 0ah,0dh,'2.BCD To Hex'
db 0ah,0dh,'3.EXIT '
db 0ah,0dh,'Enter Valid Choice : $'
str1 db 0ah,0dh,'Invalid choice! re-enter valid choice : $'
str2 db 0ah,0dh,'Enter 4 Digit hexadecimal number $'
str3 db 0ah,0dh,'Entered HEX character is invalid hex, re-enter valid hex digit : $'
str4 db 0ah,0dh,'BCD Equivalent is $'
str5 db 0ah,0dh,'enter 4 Digit bcd number : $'
str6 db 0ah,0dh,'invalid bcd digit, re-enter valid BCD digit : $'
str7 db 0ah,0dh,'Equivalent hex num is $'
hexnum dw 0
hexnum1 dw 0
loopcnt1 db 4
lpcnt db 4
lpcnt1 db 4
lpcnt2 db 4
lpcnt3 db 4
divisor1 dw 10000d
divisor2 dw 10d
divisor3 dw 1000d
multiplier1 dw 1000d
remainder1 dw ?
strdis macro str
lea dx,str
mov ah,09h
int 21h
endm
.code
12
main:mov ax,@data
mov ds,ax
again1:strdis menu
mov ah,01h
int 21h
cmp al,'1'
jnz case2
call hextobcdp
jmp again1
case2:cmp al,'2'
jnz case3
call bcdtohexp
jmp again1
case3:cmp al,'3'
jz last
strdis str1
jmp again1
last:mov ah,4ch
int 21H
acpt1bdp proc
up4: mov ah,01h
int 21h
cmp al,'0'
jb down11
cmp al,'9'
ja down11
sub al,30h
mov num1,al
jmp down21
down11:
strdis str6
jmp up4
13
down21:
ret
acpt1bdp endp
disp1hd proc
cmp num1,09h
ja dn1
add num1,30h
jmp dn2
dn1: add num1,37h
dn2: mov dl,num1
mov ah,02h
int 21h
ret
disp1hd endp
disp4hd proc
mov loopcnt1,4
loop1:
rol hexnum1,4
mov ax,hexnum1
and ax,000fh
mov num1,al
call disp1hd
dec loopcnt1
jnz loop1
ret
disp4hd endp
bcdtohexp proc
mov hexnum1,0
mov lpcnt,4
mov multiplier1,1000d
strdis str5
up3:
call acpt1bdp
mov ah,0
mul multiplier1
add hexnum1,ax
mov dx,0
mov ax, multiplier1
div divisor2
14
mov multiplier1,ax
dec lpcnt
jnz up3
strdis str7
call disp4hd
ret
bcdtohexp endp
acpt4hdp proc
mov lpcnt1,4
mov hexnum,0
up2: call acpt1hdp
shl hexnum,4
mov ah,0
or hexnum,ax
dec lpcnt1
jnz up2
ret
acpt4hdp endp
acpt1hdp proc
up1:mov ah,01h
int 21h
cmp al,'0'
jb down4
cmp al,'9'
ja down1
sub al,30h
jmp down3
down1: cmp al,'A'
jb down4
cmp al,'F'
ja down2
sub al,37h
jmp down3
down2: cmp al,'a'
15
jb down4
cmp al,'f'
ja down4
sub al,57h
jmp down3
down4: strdis str3
jmp up1
down3: ret
acpt1hdp endp
disp1bdp proc
add num,30h
mov dl,num
mov ah,02h
int 21h
ret
disp1bdp endp
hextobcdp proc
mov lpcnt3,4
mov divisor1,10000d
strdis str2
call acpt4hdp
strdis str4
up33: mov ax, hexnum
mov dx,0
div divisor1
mov hexnum, dx
mov num,al
call disp1bdp
mov dx,0
mov ax, divisor1
div divisor2
mov divisor1,ax
dec lpcnt3
16
jnz up33
mov al, byte ptr hexnum
mov num,al
call disp1bdp
ret
hextobcdp endp
end main
OUTPUT:
17
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
18
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----t 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]
19
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
20
OUTPUT:
21
Write X86/64 ALP to perform non-overlapped block transfer without string specific
instructions. Block containing data can be defined in the data segment
PROGRAM:
%macro scall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
section .data
menu db 10d,13d,"
MENU"
db 10d,"1. Non-Overlapping (Without String Instructions)"
db 10d,"2. Overlapping (Without String Instructions)"
db 10d,"3. Non-Overlapping (With String Instructions)"
db 10d,"4. Overlapping (With String Instructions)"
db 10d,"5. Exit"
db 10d
db 10d,"Enter your choice: "
lnmenu equ $-menu
m1 db 10d,13d,"Enter Count Of numbers: "
l1 equ $-m1
m2 db 10d,13d,"Enter Numbers: ",10d,13d
l2 equ $-m2
m3 db 10d,13d,"Array 1: ",10d,13d
l3 equ $-m3
m4 db 10d,13d,"Array 2: ",10d,13d
l4 equ $-m4
m5 db 10d,13d,"Enter Overlapping Position: "
l5 equ $-m5
nwline db 10d,13d
section .bss
choice resb 2
answer resb 20
array1 resb 300
array2 resb 300
count resb 20
count1 resb 20
22
count2 resb 20
temp resb 20
posn resb 20
section .text
global _start
_start:
;**********************MAIN LOGIC****************************
main:
scall 1,1,menu,lnmenu
scall 0,0,choice,2
cmp byte[choice],'5'
je exit
call inputarray
mov rax,qword[count1]
mov qword[count],rax
mov qword[count2],rax
cmp byte[choice],'1'
je case1
cmp byte[choice],'2'
je case2
cmp byte[choice],'3'
je case3
cmp byte[choice],'4'
je case4
back:
mov rax,qword[count2];rax=5
mov qword[count],rax;count=5
call displayarray
jmp main
;******************CASE1 to CASE4******************************
case1:
;number moving from arr1 to arr2
mov rsi,array1
mov rdi,array2
23
loop6: mov rax,[rsi]
mov [rdi],rax
add rsi,8
add rdi,8
dec qword[count];count=5
jnz loop6
jmp back
case2:
;position enter
scall 1,1,m5,l5
scall 0,0,temp,17
call asciihextohex
mov qword[posn],rbx;posn=2
add qword[count1],rbx;count1=5+2=7
;number moving from arr1 to arr2
mov rsi,array1
mov rdi,array2
loop7: mov rax,[rsi]
mov [rdi],rax
add rsi,8
add rdi,8
dec qword[count]
jnz loop7
mov rax,qword[count2];count2=5
mov qword[count],rax;count=5
mov rsi,array1
mov rdi,array2
loop8: add rdi,8
dec qword[posn];posn=2
jnz loop8
loop9:mov rax,[rsi]
mov [rdi],rax
add rsi,8
add rdi,8
24
dec qword[count];count=5
jnz loop9
jmp back
case3:
mov rsi,array1
mov rdi,array2
mov rcx,[count];count=5
cld
;CLEAR DIRECTION FLAG
rep movsq;repeat unitl counter becomes zero
jmp back
case4:
;position enter
scall 1,1,m5,l5
scall 0,0,temp,17
call asciihextohex
mov qword[posn],rbx;posn=2
add qword[count1],rbx;count1=5+2=7
;number moving from arr1 to arr2
mov rsi,array1
mov rdi,array2
mov rcx,[count]
cld
;CLEAR DIRECTION FLAG;if DF=0 increment rsi and rdi by one byte
else decrement by one byte
rep movsq
mov rax,qword[count2]
mov qword[count],rax
mov rsi,array1
mov rdi,array2
loop10: add rdi,8
dec qword[posn]
jnz loop10
mov rcx,qword[count1]
rep movsq
jmp back
25
exit:
mov rax,60
mov rdi,0
syscall
;exit code
;*********************PROCEDURES**********************************
inputarray:
scall 1,1,m1,l1
scall 0,0,temp,17
call asciihextohex
mov qword[count],rbx;count=5
mov qword[count1],rbx;count1=5
scall 1,1,m2,l2
mov rbp,array1
loop1: scall 0,0,temp,17
call asciihextohex
mov qword[rbp],rbx
add rbp,8;to point to next element after 8 byte(one quad word)
dec qword[count]
jnz loop1
ret
displayarray:
scall 1,1,m3,l3
mov rbp,array1
loop2: mov rax,[rbp]
call display
scall 1,1,nwline,1
add rbp,8
dec qword[count]
jnz loop2
mov rax,qword[count1]
mov qword[count],rax;value given to it again as count becomes zero in above instruction
scall 1,1,m4,l4
mov rbp,array2
26
loop3: mov rax,[rbp]
call display
scall 1,1,nwline,1
add rbp,8
dec qword[count]
jnz loop3
ret
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;since in 1 byte 2 packed bcd no can be stored
mov rcx,16;total count=no.bytes X 2=8*2=16
loop5: mov rdx,0
mov rbx,16;divisor=10 for decimal and 16 for hex
div rbx
cmp dl,09h
jbe skip2
add dl,07h
skip2: add dl,30h
mov [rsi],dl
27
dec rsi
dec rcx
jnz loop5
scall 1,1,answer,16
ret
OUTPUT:
28
Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive
addition and add and shift method.
PROGRAM:
section .data
;initialised data section
aim db 0ah,
len equ $-aim
menu db 0ah,'1. Successive Addition'
db 0ah,'2. Add and Shift'
db 0ah,'3. Exit'
db 0ah,' Enter your choice : '
menul equ $-menu
msg1 db 0ah,'Enter First Number :
msgl1 equ $-msg1
;length of message
msg2 db 0ah,'Enter Second Number : '
msgl2 equ $-msg2
msg3 db 0ah,'Result : '
msgl3 equ $-msg3
section .bss
n resb 4
num1 resb 1
num2 resb 1
choice resb 2
section .text
global _start
_start :
%macro io 4
;macro for print and scan
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
;end of macro
io 1,1,aim,len
men:
;user defined flag
29
io 1,1,menu,menul
io 0,0,choice,2
case1:
cmp byte[choice],'1'
jne case2
io 1,1,msg1,msgl1
;print message1
io 0,0,n,3
;taking multiplicand as input
call asciihex
;procedure call to convert input ascii to hex hex equivalent
mov [num1],bl
;moving contents of bl to num1 variable
io 1,1,msg2,msgl2
;print message2
io 0,0,n,3
;taking multiplier as input
call asciihex
;procedure call to convert input ascii to hex equivalent
mov [num2],bl
;moving contents of bl to num2 variable
call p1
jmp men
;procedure call
;unconditional jump
case2:
;case 2 for add and shift method
cmp byte[choice],'2'
;comparing whether user's choice is 1
jne exi
;jumping to case2 if user didnot enter 1 as choice
io 1,1,msg1,msgl1
;print message1
io 0,0,n,3
;taking multiplicand as input
call asciihex
;procedure call to convert input ascii to hex hex equivalent
mov [num1],bl
;moving contents of bl to num1 variable
io 1,1,msg2,msgl2
;print message2
io 0,0,n,3
;taking multiplier as input
call asciihex
;procedure call to convert input ascii to hex equivalent
mov [num2],bl
;moving contents of bl to num2 variable
call p2
jmp men
exi:
mov rax,60
mov rdi,0
syscall
p1:
mov rcx,0
mov rbx,0
mov rax,0
mov al,[num1]
mov cl,[num2]
lp2:
;procedure call
;unconditional jump
;user defined flag
;system exit
;system exit
;system interrupt
;procedure for successive addition method
;clearing contents of rcx
;clearing contents of rbx
;clearing contents of rax
;storing num1 in al
;storing num2 in cl
;user defined label
30
add bx,ax
;add contents of bx and ax and store them in bx
loop lp2
;looping instruction,automatically decrements rcx
io 1,1,msg3,msgl3
;printing message3
call displaynum
;procedure call
ret
p2:
mov rcx,0
mov rbx,0
mov rdx,0
mov al,[num1]
mov bl,[num2]
mov rcx,8
lp4:
shr bl,1
jnc flg
add dx,ax
flg:shl ax,1
loop lp4
mov rbx,rdx
io 1,1,msg3,msgl3
call displaynum
ret
displaynum:
mov rcx,4
mov rsi,n
mov rax,0
lp3:
rol bx,4
mov al,bl
and al,0fh
cmp al,9
jbe add30h
add al,7h
add30h : add al,30h
mov [rsi],al
inc rsi
loop lp3
io 1,1,n,4
ret
asciihex:
31
mov rsi,n
mov rbx,0
mov rax,0
mov rcx,2
lp1:
rol bl,4
mov al,[rsi]
cmp al,39h
jbe sub30h
sub al,7h
sub30h: sub al,30h
add bl,al
inc rsi
loop lp1
ret
OUTPUT:
32
33
Write X86 Assembly Language Program (ALP) to implement following OS commands
i)
COPY, ii) TYPE
Using file operations. User is supposed to provide command line arguments
PROGRAM:
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
34
_start:
pop rbx
pop rbx
; REMOVE THE NUMMBER OF ARGUMENS FROM THE STACK
;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
35
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
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
36
mov rax,3
mov rdi,filedes_2
syscall
;PRINTING MESSAGE
print msg1,len1
jmp exit
;<-----------------DELETING FILE-------------------------->
Delete :
pop rbx
mov rsi,filename_1
up_3: mov al,byte[rbx]
mov byte[rsi],al
inc rsi
inc rbx
cmp byte[rbx],0H
jne up_3
;DELETE SYSTEM CALL
mov rax,87
mov rdi,filename_1
syscall
;PRINTING THE MESSAGE
print msg2,len2
jmp exit
;<------------------TYPE IN THE FILE ----------------------->
type :
;SAVING FILE NAME
pop rbx
mov rsi,filename_1
up_4: mov al,byte[rbx]
mov byte[rsi],al
inc rsi
inc rbx
cmp byte[rbx],0H
jne up_4
;OPENING THE FILE
mov rax,2
37
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
syscall
jmp exit
;<-------------------PRINT FILE------------------------>
NoFile :
print msg,len
jmp exit
;<--------------TERMINATING THE PROGRAM--------------->
exit:
mov rax,60
mov rdi,0
syscall
38
OUTPUT:
39
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
PROGRAM:
%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"
FACTORIAL:
call fact_proc
mov rbx,rax
mov rdi,factorial
call HtoA_value
scall 1,1,factorial,8
40
;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
41
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
;------------- END PROGRAM -----------------------------
OUTPUT:
42
43
Download