Wednesday, 15 April 2015

String Operations in Assembly Language

***********PROGRAM**********
STRING OPERATIONS
***********************************

section .data
m1 db 10,"MENU",10
   db "1)String Length",10
   db "2)String copy",10
   db "3)String Compare",10
   db "4)String reverse",10
   db "5)Palindrome",10
   db "6)exit",10
   db "Enter your choice:",10
l1 equ $-m1
m2 db "Enter string:",10
l2 equ $-m2
m3 db "Length of the string is:",10
l3 equ $-m3
m4 db "The copied String is:",10
l4 equ $-m4
m5 db "The Strings are equal",10
l5 equ $-m5
m6 db "The Strings are not equal",10
l6 equ $-m6
m7 db "The String is a palindrome",10
l7 equ $-m7
m8 db "The String is not a palindrome",10
l8 equ $-m8
m9 db "The Reversed String is:",10
l9 equ $-m9

section .bss
choice resb 3
s1 resb 40
s2 resb 40
cnt resq 1
ctr resq 1
temp resb 2

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro exit 0
mov rax,60
mov rbx,0
syscall
%endmacro

section .text
global _start
_start:
loop:
write m1,l1
read choice,3
dec rax
cmp byte[choice],31h
je lenght
cmp byte[choice],32h
je copy
cmp byte[choice],33h
je compare
cmp byte[choice],34h
je reverse
cmp byte[choice],35h
je palin
exit

lenght:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov rsi,cnt
mov qword[ctr],08h
add rsi,07h
push rsi
push rax
write m3,l3
pop rax
pop rsi
call display
call loop

copy:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov qword[ctr],rax
mov rsi,s1
mov rdi,s2

again:
mov al,byte[rsi]
mov byte[rdi],al
inc rsi
inc rdi
dec qword[cnt]
cmp qword[cnt],00
jnz again
write m4,l4
write s2,qword[ctr]
call loop
exit

compare:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
write m2,l2
read s2,40
mov qword[ctr],rax
mov rsi,s1
mov rdi,s2
mov rax,00
mov rbx,00
go:
add al,byte[rsi]
add bl,byte[rdi]
inc rsi
inc rdi
dec qword[ctr]
cmp qword[ctr],00
jnz go
cmp al,bl
je got
write m6,l6
call loop
got:
write m5,l5
call loop

reverse:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov rsi,s1
mov rdi,s2
add rdi,qword[cnt]
dec rdi

rev:
mov al,byte[rsi]
mov byte[rdi],al
dec rdi
inc rsi
dec qword[cnt]
cmp qword[cnt],00
jne rev
write m9,l9
write s2,40
call loop

palin:
write m2,l2
read s1,40
dec rax
mov qword[cnt],rax
mov rsi,s1
mov rdi,s1
add rdi,qword[cnt]
dec rdi

pl:
mov al,byte[rsi]
cmp al,byte[rdi]
je same
jne diff
same:
inc rsi
dec rdi
cmp rsi,rdi
jge yep
jmp pl

yep:
write m7,l7
call loop

diff:
write m8,l8
call loop

display:
mov al,byte[rsi]
mov cl,04
shr al,cl
cmp al,09h
jle a1
add al,07h
a1: add al,30h
mov byte[temp],al
push rsi
write temp,01
pop rsi
mov al,byte[rsi]
mov cl,04
shl al,cl
shr al,cl
cmp al,09h
jle a2
add al,07h
a2: add al,30h
mov byte[temp],al
push rsi
write temp,01
pop rsi
dec rsi
dec byte[ctr]
jne display
ret

******************OUTPUT******************
sourabh@ubuntu~$ nasm -f elf64 str.asm
sourabh@ubuntu~$ ld -o str str.o
sourabh@ubuntu~$ ./str

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 1
Enter string: string
Length of the string is: 0000000000000006

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 2
Enter string: hello world
The copied String is: hello world

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 3
Enter string: hello
Enter string: hello
The Strings are equal

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 4
Enter string: hello
The Reversed String is: olleh

MENU
1)String Length
2)String copy
3)String Compare
4)String reverse
5)Palindrome
6)exit
Enter your choice: 5
Enter string: aabbaa
The String is a palindrome

No comments:

Post a Comment