Assignment No. 4

Write X86/64 ALP for the following operations on the string entered by the user. (Use of 64-bit registers is expected) c) Check whether the string is palindrome) Calculate Length of the string b) Reverse the string

;Program for string operation
;——————-p4.asm———————————————–
%macro dispmsg 2
mov eax,04
mov ebx,01
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro accept 2
mov eax,03
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro acceptstr 1
mov eax,03
mov ebx,0
mov ecx,%1
int 80h
%endmacro

section .data
menumsg db 10,10,’##### Menu for String Operations #####’
db 10,13,’1.Enter the String’
db 10,13,’2.Calculate the length’
db 10,13,’3.Reverse the String’
db 10,13,’4.Check Whether String is Palindrome or not’
db 10,13,’5.Exit’,10
db 10,13,’Enter your Choice’

menumsg_len equ $-menumsg
wrchmsg db 10,10,’Wrong Choice Entered….Please try again!!!’,10,10
wrchmsg_len equ $-wrchmsg

entmsg db 10,’Enter the String::’
entmsg_len equ $-entmsg

lmsg db 10,’Length of the String is::’
lmsg_len equ $-lmsg

revmsg db 10,’Revered String is ::’
revmsg_len equ $-revmsg

palmsg db 10,’String is Palindrome !’
palmsg_len equ $-palmsg

npalmsg db 10,’String is not Palindrome !’
npalmsg_len equ $-npalmsg

restmsg db 10,’Do you want to continue ?’
restmsg_len equ $-restmsg

thankmsg db 10,10,’Thank you for using Program created by Prof. Dhokane R.M.!!’,10
thankmsg_len equ $-thankmsg

spacechar db 20h

section .bss
cnt resb 01
optionbuff resb 02
dispbuff resb 03
srcstr resb 10
revstr resb 10
string resb 0

section .text
global _start
_start:

menu:    dispmsg menumsg,menumsg_len

accept optionbuff,02

cmp byte [optionbuff],’1′
jne case2
call entstr_proc
jmp exit1

case2:    cmp byte [optionbuff],’2′
jne case3
call length_proc
jmp exit1

case3:    cmp byte [optionbuff],’3′
jne case4
call reverse_proc
jmp exit1

case4:    cmp byte [optionbuff],’4′
jne case5
call pal_proc
jmp exit1

case5:    cmp byte [optionbuff],’5′
je exit

dispmsg wrchmsg,wrchmsg_len
jmp menu
exit1:
dispmsg restmsg,restmsg_len
accept optionbuff,02
cmp byte [optionbuff],’y’
jne y1
jmp menu
y1:     cmp byte [optionbuff],’Y’
jne exit
jmp menu

exit:
dispmsg thankmsg,thankmsg_len

mov eax,01    ;Exit
mov ebx,0
int 80h

dispblk_proc:
mov ecx,cnt
rdisp:
push ecx
mov bl,[esi]        ;Read ASCII value char by char
call disp8_proc        ;& Display
inc esi            ;Point to next char
dispmsg spacechar,1    ;Display space
pop ecx
loop rdisp        ;Decrement count
;Repeat display process till actual count becomes zero
ret

entstr_proc:
dispmsg entmsg,entmsg_len
acceptstr srcstr
dec al
mov [cnt],al
ret

length_proc:
dispmsg lmsg,lmsg_len
mov bl,[cnt]
call disp8_proc
ret

reverse_proc:
mov ecx,00
mov esi,srcstr
mov edi,revstr
mov cl,[cnt]
add esi,ecx
sub esi,1
up2: mov al,[esi]
mov [edi],al
inc edi
dec esi
loop up2
dispmsg revmsg,revmsg_len
dispmsg revstr,cnt
ret

pal_proc:
mov ecx,00
mov esi,srcstr
mov edi,revstr
mov cl,[cnt]
up3:    mov al,[esi]
mov bl,[edi]
cmp al,bl
jne exit2
inc esi
inc edi
loop up3
dispmsg palmsg,palmsg_len
jmp return
exit2:  dispmsg npalmsg,npalmsg_len
return:    ret

disp8_proc:
mov ecx,02
mov edi,dispbuff
dup1:
rol bl,4
mov al,bl
and al,0Fh
cmp al,09h
jbe dskip
add al,07h
dskip:    add al,30h
mov [edi],al
inc edi
loop dup1

dispmsg dispbuff,03
ret
;—————————————————————————————————————————–
Steps to execute the above program is as follows:

dhokane@dhokane-ThinkCentre-A70:~/MIT IN LINUX$ nasm -f elf p4.asm
dhokane@dhokane-ThinkCentre-A70:~/MIT IN LINUX$ ld -m elf_i386 -s -o p4  p4.o
dhokane@dhokane-ThinkCentre-A70:~/MIT IN LINUX$ ./p4

##### Menu for String Operations #####
1.Enter the String
2.Calculate the length
3.Reverse the String
4.Check Whether String is Palindrome or not
5.Exit

Enter your Choice1

Enter the String::engineering

Do you want to continue ?y

##### Menu for String Operations #####
1.Enter the String
2.Calculate the length
3.Reverse the String
4.Check Whether String is Palindrome or not
5.Exit

Enter your Choice2

Length of the String is::0B
Do you want to continue ?y

##### Menu for String Operations #####
1.Enter the String
2.Calculate the length
3.Reverse the String
4.Check Whether String is Palindrome or not
5.Exit

Enter your Choice3

Revered String is ::gnireenigne
Do you want to continue ?y

##### Menu for String Operations #####
1.Enter the String
2.Calculate the length
3.Reverse the String
4.Check Whether String is Palindrome or not
5.Exit

Enter your Choice4

String is not Palindrome !
Do you want to continue ?y

##### Menu for String Operations #####
1.Enter the String
2.Calculate the length
3.Reverse the String
4.Check Whether String is Palindrome or not
5.Exit

Enter your Choice5
Thank you for using Program created by Prof. Dhokane R.M.!!
dhokane@dhokane-ThinkCentre-A70:~/MIT IN LINUX$

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *