Problem Statement: Write X86/64 ALP for the following operations on the string entered by the user. (use of 64-bit registers is
expected)
a) Calculate Length of the string b) Reverse the string
c) Check whether the string is palindrome
OR
Make your program user friendly by providing MENU like:
(a) Enter the string b) Calculate length of string c) Reverse string d) Check palindrome e) Exit
Display appropriate messages to prompt the user while accepting the input and displaying the result.
%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 string: "
len1: equ $-msg1
msg2: db "1. Enter String",10,"2. Length",10,"3. Reverse",10,"4. Palindrome",10,"5. Exit",10
len2: equ $-msg2
msg3: db "Enter choice",10
len3: equ $-msg3
msg4: db "Length: "
len4: equ $-msg4
msg5: db "Reversed string: "
len5: equ $-msg5
msg6: db "Palindrome",10
len6: equ $-msg6
msg7: db "Not Palindrome",10
len7: equ $-msg7
msg8: db "",10
len8: equ $-msg8
SECTION .bss
str1: resb 20
str2: resb 20
choice: resb 2
len: resb 2
count: resb 2
SECTION .text
global main
main:
menu:
print msg2,len2
print msg3,len3
accept choice,2
cmp byte[choice],31H
je ch1
cmp byte[choice],32H
je ch2
cmp byte[choice],33H
je ch3
cmp byte[choice],34H
je ch4
cmp byte[choice],35H
je ch5
ch1: ;input string
print msg1,len1
accept str1,20
mov byte[len], al
add byte[len], 30h
sub byte[len], 0x1
jmp menu
ch2: ;length
print msg4,len4
print len,1
print msg8,len8
jmp menu
ch3: ;reverse
mov r9,str1
mov r10,str2
xor rcx,rcx
mov cl,byte[len]
sub cl,30h
mov byte[count],cl
add r10,rcx
loop1:
mov bl,[r9]
mov [r10],bl
inc r9
dec r10
dec byte[count]
jnz loop1
print msg5,len5
print str2,20
print msg8,len8
jmp menu
ch4: ;palindrome
xor rax,rax
xor rbx,rbx
xor rcx,rcx
mov r9,str1
mov r10,r9
mov cl,byte[len]
sub cl,30h
add r10,rcx
dec r10
loop2:
mov al,[r9]
mov bl,[r10]
cmp al,bl
jne l1
inc r9
dec r10
cmp r9,r10
jb loop2
print msg6,len6
jmp menu
l1:
print msg7,len7
jmp menu
ch5: ;exit
jmp exit
exit:
mov rax,60
mov rdi,1
syscall
c) Check whether the string is palindrome
OR
Make your program user friendly by providing MENU like:
(a) Enter the string b) Calculate length of string c) Reverse string d) Check palindrome e) Exit
Display appropriate messages to prompt the user while accepting the input and displaying the result.
%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 string: "
len1: equ $-msg1
msg2: db "1. Enter String",10,"2. Length",10,"3. Reverse",10,"4. Palindrome",10,"5. Exit",10
len2: equ $-msg2
msg3: db "Enter choice",10
len3: equ $-msg3
msg4: db "Length: "
len4: equ $-msg4
msg5: db "Reversed string: "
len5: equ $-msg5
msg6: db "Palindrome",10
len6: equ $-msg6
msg7: db "Not Palindrome",10
len7: equ $-msg7
msg8: db "",10
len8: equ $-msg8
SECTION .bss
str1: resb 20
str2: resb 20
choice: resb 2
len: resb 2
count: resb 2
SECTION .text
global main
main:
menu:
print msg2,len2
print msg3,len3
accept choice,2
cmp byte[choice],31H
je ch1
cmp byte[choice],32H
je ch2
cmp byte[choice],33H
je ch3
cmp byte[choice],34H
je ch4
cmp byte[choice],35H
je ch5
ch1: ;input string
print msg1,len1
accept str1,20
mov byte[len], al
add byte[len], 30h
sub byte[len], 0x1
jmp menu
ch2: ;length
print msg4,len4
print len,1
print msg8,len8
jmp menu
ch3: ;reverse
mov r9,str1
mov r10,str2
xor rcx,rcx
mov cl,byte[len]
sub cl,30h
mov byte[count],cl
add r10,rcx
loop1:
mov bl,[r9]
mov [r10],bl
inc r9
dec r10
dec byte[count]
jnz loop1
print msg5,len5
print str2,20
print msg8,len8
jmp menu
ch4: ;palindrome
xor rax,rax
xor rbx,rbx
xor rcx,rcx
mov r9,str1
mov r10,r9
mov cl,byte[len]
sub cl,30h
add r10,rcx
dec r10
loop2:
mov al,[r9]
mov bl,[r10]
cmp al,bl
jne l1
inc r9
dec r10
cmp r9,r10
jb loop2
print msg6,len6
jmp menu
l1:
print msg7,len7
jmp menu
ch5: ;exit
jmp exit
exit:
mov rax,60
mov rdi,1
syscall
Output
amodi@ubuntu:~/MIL/Assign4$ nasm -f elf64 -l asgn4.lst
asgn4.asm
amodi@ubuntu:~/MIL/Assign4$ ld -o asgn4 asgn4.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign4$ ./asgn4
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
1
Enter string: Robert JR
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
2
Length: 9
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
3
Reversed string: RJ treboR
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
4
Not Palindrome
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
1
Enter string: a nut for a jar of tuna
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
4
Palindrome
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
5
amodi@ubuntu:~/MIL/Assign4$ ld -o asgn4 asgn4.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign4$ ./asgn4
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
1
Enter string: Robert JR
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
2
Length: 9
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
3
Reversed string: RJ treboR
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
4
Not Palindrome
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
1
Enter string: a nut for a jar of tuna
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
4
Palindrome
1. Enter String
2. Length
3. Reverse
4. Palindrome
5. Exit
Enter choice
5
No comments:
Post a Comment