Friday 22 January 2016

Two module String Operations ALP

Q:- Write an ALP for String Manipulation Operations . Store the string in data set of one module and write far procedure in code segment of the other module for performing the string operation. Operations to be performed are as follows:-
  • Concatenation
  • Occurrences of a sub-string in a string
Create .obj files of both modules. And link them to create an executable file.

 

 New Concepts

  • global  

    Similar to public or export . This will make those labels or variables available to other modules through extern.
  • extern 

    Similar to import. This will be used to import variables or labels from other modules which were declared as global.

Code

as51.asm

 

;Author:- Abhishek Munagekar
;Title:-String Operations
%macro read 2  ;macro for read system call
mov rax,0h
mov rdi,1h
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro write 2
mov rax,1h
mov rdi,0h
mov rsi,%1
mov rdx,%2
syscall
%endmacro
;macro ends
section .data
msg1:  db 10,"============================",10
 db "            MENU            ",10
 db "============================",10
 db "1.Concatenate the strings   ",10
 db "2.Find the number of occurances of the substring",10
 db "3.Exit",10
len1: equ $-msg1
msg2:   db "Enter the first string",10
len2: equ $-msg2
msg3: db "Enter the second string",10
len3: equ $-msg3
msg4: db "Enter the string in which no of occurances of substring are to be found out",10
len4: equ $-msg4
msg5: db "Enter the sub string whose occurances are to be found out",10
len5: equ $-msg5
msg6: db "The number of occurances of the substring in the string are"
len6: equ $-msg6
section .bss
extern answer
global stra,strb,lena,lenb ;export these variables
choice resb 2
stra resb 60  ;bigger string will be used to stored the concate
strb resb 30
lena resb 1  ;length of the strings
lenb resb 1
section .text
global _start:
_start:
extern concat,substring
write msg1,len1   ;display the menu
read choice,2
cmp byte[choice],31h
je con
cmp byte[choice],32h
je sub
cmp byte[choice],33h
je exit
jmp _start

con:
write msg2,len2
read stra,30   ;limit of 29 characters only
dec al
mov byte[lena],al
write msg3,len3
read strb,30
dec al
mov byte[lenb],al
call concat
write stra,60
jmp _start

sub:
write msg4,len4
read stra,60
dec al
mov byte[lena],al
write msg5,len5
read strb,30
dec al
mov byte[lenb],al
call substring
write msg6,len6
add byte[answer],30h
write answer,1
jmp _start

exit:
mov rax,60
mov rdx,0
syscall

as52.asm

;Author:-Abhishek Munagekar
;Title:-String Operations
section .data
section .bss
global answer
extern stra,strb,lena,lenb ;import these
count resb 1
answer resb 1


section .text
global concat,substring
concat:   ;function for concatenation of strings
mov esi,stra
mov edi,strb
movsx eax,byte[lena]
add esi,eax  ;we r now at the end of a without considering the enter char
loop1:
mov al,byte[rdi]
mov byte[esi],al
inc esi
inc edi
dec byte[lenb]
jnz loop1
mov byte[esi],10
ret

substring:  ;function for finding the number of occurances of the substring
mov byte[answer],0
mov al,byte[lenb]
mov byte[count],al 
mov rsi,stra  ;main string
mov rdi,strb  ;sub string
loop2:
mov al,byte[rsi]
cmp al,10  ;has the end of the string been reached
je terminator
cmp al,byte[rdi]
je match  ;if they match
mov al,byte[lenb]
mov byte[count],al
inc rsi
mov rdi,strb  ;if they don't match increment rsi  and reset your count and mov rdi back to substring
jmp loop2
match:
inc rsi
inc rdi
dec byte[count]
jnz loop2  ;if not completely matched
mov rdi,strb  ;if completely matched
mov al,byte[lenb]
mov byte[count],al ;reset the count, take rdi back to original substring and increment rsi
inc byte[answer]
jmp loop2
terminator:
ret

Output

xploit@comp:~/assi5$ nasm -f elf64 as51.asm
xploit@comp:~/assi5$ nasm -f elf64 as52.asm
xploit@comp:~/assi5$ ld -o as5 as51.o as52.o
xploit@comp:~/assi5$ ./as5

============================
            MENU           
============================
1.Concatenate the strings  
2.Find the number of occurances of the substring
3.Exit
1
Enter the first string
con
Enter the second string
cat
concat

============================
            MENU           
============================
1.Concatenate the strings  
2.Find the number of occurances of the substring
3.Exit
2
Enter the string in which no of occurances of substring are to be found out
is is a word isn't it?
Enter the sub string whose occurances are to be found out
is
The number of occurances of the substring in the string are3
============================
            MENU           
============================
1.Concatenate the strings  
2.Find the number of occurances of the substring
3.Exit
3
xploit@comp:~/assi5$

No comments:

Post a Comment