;Aim:-
;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.

;—————————-declaration of macro———————————
%macro disp 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

;———————–data section—————————————
section .data
menu:     db ‘MENU’, 10
db ‘1. Enter the string’, 10
db ‘2. Calculate length of string’, 10
db ‘3. Reverse string’, 10
db ‘4. Check palindrome’, 10
db ‘5. Exit’, 10
db ‘Enter your choice:’, 10
menulen equ $-menu
msg1  db  ‘Enter string:’, 10
msg1len equ $-msg1
msg2 db ‘The length of given string is:’, 10
msg2len equ $-msg2
msg3 db ‘The reverse of the string is:’
msg3len equ $-msg3
msg4 db ‘The given string is palindrome.’, 10
msg4len equ $-msg4
msg5 db ‘The given string is not palindrome.’, 10
msg5len equ $-msg5
msg6 db ‘Thank you for using programme.’, 10
msg6len equ $-msg6
nl db ”, 10, 13
nllen equ $-nl

;————————–bss section————————————
;Notes:-
;Remeber size of strlen and len
section .bss
string: resb 50
choice: resb 2
strlen: resb 8
len: resb 16
rev: resb 50

;——————————–text section————————————–
section .text
global _start
_start:
disp menu, menulen
accept choice, 2

cmp byte[choice], 31h
je get_string
cmp byte[choice], 32h
je length
cmp byte[choice], 33h
je reverse
cmp byte[choice], 34h
je palindrome
cmp byte[choice], 35h
jae exit

get_string:
disp msg1, msg1len
accept string, 50
dec rax
mov [strlen], rax
jmp _start

;—————–logic for length calculation——————————-
;Notes:-
;Get length in bx
;rol bx to get its one digit
;and with 0fh
;Adjust ASCII
;Move it in rdi

length:
mov bx, [strlen]
mov rdi, len
mov rcx, 16
d1:    rol bx, 4
mov dl, bl
and dl, 0fh
cmp dl, 09h
jbe d2
add dl, 07h
d2:    add dl, 30h
mov [rdi], dl
inc rdi
loop d1

disp msg2, msg2len
disp len, 4
disp nl, nllen
jmp _start

;————————-logic for reversing the string—————————–
;Notes:-
;Put one string in rsi
;put the variable of resulting reverse string in rdi
;Add length in rsi
;transfer rsi to rdi decrementing it
reverse:
mov rsi, string
mov rdi, rev
add rsi, [strlen]
mov rcx, [strlen]
a1:    mov al, [rsi]
mov [rdi], al
dec rsi
inc rdi
loop a1

mov al, [rsi]            ;This extra iteration is required for completion of reverse string.
mov [rdi], al

disp msg3, msg3len
disp rev, 50
disp nl, nllen
jmp _start

;—————–logic to check palindrome—————————————
;Notes:-
;put the string in rsi and rdi
;add length in rsi and decrement it once
;compare elements of rsi and rdi
palindrome:            ;************Can be implemented by using string instruction -> repe cmpsb**************
mov rsi, string
mov rdi, string
add rsi, [strlen]
dec rsi                ;This is decremented because go get access of last element
mov rcx, [strlen]
b1:    mov al, [rsi]
cmp [rdi], al
jne b2
dec rsi
inc rdi
loop b1

disp msg4, msg4len
jmp _start
b2:    disp msg5, msg5len
jmp _start

;—————————————exit block————————————–
exit:        disp msg6, msg6len
mov rax, 60
mov rdi, 0
syscall 

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret