;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
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.
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.
Assembly language usually consists of three sections,
Data section
To initialize variables and constants, buffer size these values doesn't change at runtime.
bss section
To declare variables
text section
_start
specifies the starting of this section where the actually code is written.
There are various define directives to allocate space for variables for both initialized and uninitialized data.
variable-name define-directive initial-value
Define Directive | Description | Allocated Space |
---|---|---|
DB | Define Byte | 1 byte |
DW | Define Word | 2 bytes |
DD | Define Doubleword | 4 bytes |
DQ | Define Quadword | 8 bytes |
DT | Define Ten Bytes | 10 bytes |
Define Directive | Description |
---|---|
RESB | Reserve a Byte |
RESW | Reserve a Word |
RESD | Reserve a Doubleword |
RESQ | Reserve a Quadword |
REST | Reserve a Ten Bytes |
Constants can be defined using
CONSTANT_NAME EQU regular-exp or value
%assign constant_name value
%define constant_name value
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.
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