Saturday, April 19, 2014

String manipulation operations (concatenation, substring) using FAR procedure call in NASM[x86/64 architecture]

Problem Statement: Write 8086 ALP to perform string manipulation. The strings to be accepted from the user is to be stored in data segment of program_l and write FAR PROCEDURES in code segment program_2 for following operations on the string:
(a) Concatenation of two strings (b) Number of occurrences of a sub-string in the given string Use PUBLIC and EXTERN directive. Create .OBJ files of both the modules and link them to create an EXE file.


-----------------------------------------------------------------

asgn5.asm file:

global str1,str2,msg2,msg3,len2,len3
extern concate1,substr1

%macro print 2
mov rax,1
mov rdi,1
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 .data
msg1: db "Enter choice",10,"1. Concatenate",10,"2. Number of substrings",10,"3. Exit",10,"Enter your choice: "
len1: equ $-msg1
msg2: db "Enter the first string: "
len2: equ $-msg2
msg3: db "Enter the second string: "
len3: equ $-msg3

SECTION .bss
str1: resb 50
str2: resb 50
choice: resb 2

SECTION .text
global main:
main:
menu:
print msg1,len1
accept choice,2
cmp byte[choice],31h
je ch1
cmp byte[choice],32h
je ch2
cmp byte[choice],33h
je ch3

ch1:
call concate1
jmp menu

ch2:
call substr1
jmp menu

ch3:
mov rax,60
mov rdi,1

syscall

-----------------------------------------------------------------

asgn5_1.asm file:

extern str1,str2,msg2,msg3,len2,len3
global concate1,substr1

%macro print 2
mov rax,1
mov rdi,1
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 .data
msg4: db 10,"The number of substrings are: "
len4: equ $-msg4
msg5: db 10,"The concatenated string is: "
len5: equ $-msg5
msg6: db "",10
len6: equ $-msg6

SECTION .bss
ln1: resb 2
ln2: resb 2
ln21: resb 2
str3: resb 50
count: resb 2

SECTION .text
concate1:
mov byte[str1],0h
mov byte[str2],0h

print msg2,len2
accept str1,50
mov byte[ln1],al
sub byte[ln1],1

print msg3,len3
accept str2,50
mov byte[ln2],al
sub byte[ln2],1

mov r9,str1
mov r10,str3

loop1:
mov bl,[r9]
mov [r10],bl
inc r9
inc r10
dec byte[ln1]
jnz loop1
mov r9,str2
loop2:
mov bl,[r9]
mov [r10],bl
inc r9
inc r10
dec byte[ln2]
jnz loop2

print msg5,len5
print str3,50
print msg6,len6
ret

substr1:
mov byte[str1],0h
mov byte[str2],0h

print msg2,len2
accept str1,50
mov byte[ln1],al
sub byte[ln1],1

print msg3,len3
accept str2,50
mov byte[ln2],al
sub byte[ln2],1
mov byte[ln21],al
sub byte[ln21],1

mov r9,str1
mov r10,str2

loop:
mov al,[r9]
cmp al,[r10]
je l1
inc r9
dec byte[ln1]
cmp byte[ln1],00H
je l3
jne loop

l1:
inc r9
inc r10
dec byte[ln1]
dec byte[ln2]
jz l2
jmp loop

l2:
inc byte[count]
mov al,byte[ln21]
mov byte[ln2],al
mov r10,str2
cmp byte[ln1],00H
jne loop


l3:
xor al,al
mov al,[count]
add al,30h
mov [count],al
print msg4,len4
print count,2
print msg6,len6
ret

-----------------------------------------------------------------

Output

amodi@ubuntu:~/MIL/Assign5$ nasm -f elf64 -l asgn5.lst asgn5.asm
amodi@ubuntu:~/MIL/Assign5$ nasm -f elf64 -l asgn5_1.lst asgn5_1.asm
amodi@ubuntu:~/MIL/Assign5$ ld -o asgn5 asgn5.o asgn5_1.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign5$ ./asgn5
Enter choice
1. Concatenate
2. Number of substrings
3. Exit
Enter your choice: 1
Enter the first string: Iron
Enter the second string: Man

The concatenated string is: IronMan
Enter choice
1. Concatenate
2. Number of substrings
3. Exit
Enter your choice: 2
Enter the first string: Iron
Enter the second string: Man

The number of substrings are: 0
Enter choice
1. Concatenate
2. Number of substrings
3. Exit
Enter your choice: 2
Enter the first string: wheat
Enter the second string: at

The number of substrings are: 1
Enter choice
1. Concatenate
2. Number of substrings
3. Exit
Enter your choice: 3 

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...