reverse an input string in assembly language

Reverse an input string in assembly language

To reverse an input string in assembly language, you typically need to follow these steps:

  1. Read the input string from the user.
  2. Calculate the length of the input string.
  3. Iterate through the input string from the end to the beginning, storing each character in a new string in reverse order.
  4. Output or display the reversed string.

Below is an example of how you can achieve this in x86 Assembly language for Linux using NASM (Netwide Assembler):

section .data input_msg db 'Enter a string: ', 0 output_msg db 'Reversed string: ', 0 newline db 10, 0 section .bss input resb 256 ; Input buffer reversed resb 256 ; Reversed string buffer section .text global _start _start: ; Display input prompt mov eax, 4 ; sys_write syscall number mov ebx, 1 ; stdout file descriptor mov ecx, input_msg ; message address mov edx, input_msg_len ; message length int 0x80 ; call kernel ; Read input string mov eax, 3 ; sys_read syscall number mov ebx, 0 ; stdin file descriptor mov ecx, input ; input buffer address mov edx, 256 ; buffer size int 0x80 ; call kernel ; Calculate string length mov edi, input xor ecx, ecx xor eax, eax count_length: cmp byte [edi + ecx], 0 ; Check for null terminator je end_count_length inc ecx jmp count_length end_count_length: ; Reverse the string mov esi, input ; Source pointer lea edi, [reversed + ecx - 1] ; Destination pointer (start from the end) reverse_loop: mov al, [esi] ; Load character from source mov [edi], al ; Store character to destination inc esi ; Move to next character in source dec edi ; Move to previous position in destination cmp esi, edi ; Check if pointers have crossed jg end_reverse_loop jmp reverse_loop end_reverse_loop: ; Display reversed string mov eax, 4 ; sys_write syscall number mov ebx, 1 ; stdout file descriptor mov ecx, output_msg ; message address mov edx, output_msg_len ; message length int 0x80 ; call kernel mov eax, 4 ; sys_write syscall number mov ebx, 1 ; stdout file descriptor mov ecx, reversed ; reversed string address mov edx, ecx ; reversed string length (same as input) int 0x80 ; call kernel ; Exit mov eax, 1 ; sys_exit syscall number xor ebx, ebx ; exit code 0 int 0x80 ; call kernel section .data input_msg_len equ $ - input_msg output_msg_len equ $ - output_msg 

This code reads an input string from the user, calculates its length, and then reverses it character by character, storing the reversed string in a new buffer. Finally, it displays the reversed string to the user.

Examples

  1. How to reverse a string in assembly language?

    Description: To reverse a string in assembly language, you typically iterate through the string from the end to the beginning, copying characters to a new buffer in reverse order.

    .model small .data msg db 'Hello, world!', '$' len equ $ - msg reverse_msg db len dup(?) ; Buffer to store reversed string .code main proc mov si, 0 ; Source index mov di, len - 2 ; Destination index (excluding null terminator) reverse_loop: mov al, msg[si] ; Load character from source mov reverse_msg[di], al ; Store character in reverse order inc si ; Move to next character in source dec di ; Move to previous position in destination cmp si, len ; Check if end of string reached jne reverse_loop ; If not, continue loop mov byte ptr reverse_msg[len], '$' ; Null-terminate reversed string mov dx, offset reverse_msg ; Point dx to reversed string mov ah, 9 ; DOS function to print string int 21h ; Call DOS mov ax, 4C00h ; Exit program int 21h ; Call DOS main endp end main 
  2. Assembly program to reverse a string using stack?

    Description: Another approach to reverse a string in assembly language involves using a stack to reverse the characters.

    .model small .data msg db 'Hello, world!', '$' len equ $ - msg stack db len dup(?) ; Stack to store characters .code main proc mov cx, len - 2 ; Initialize loop counter (excluding null terminator) mov si, 0 ; Initialize source index push_loop: mov al, msg[si] ; Load character from source push al ; Push character onto stack inc si ; Move to next character in source loop push_loop ; Continue loop until all characters pushed mov si, 0 ; Reset source index pop_loop: pop al ; Pop character from stack mov msg[si], al ; Store character back in message buffer inc si ; Move to next position in message buffer loop pop_loop ; Continue loop until stack is empty mov dx, offset msg ; Point dx to reversed string mov ah, 9 ; DOS function to print string int 21h ; Call DOS mov ax, 4C00h ; Exit program int 21h ; Call DOS main endp end main 
  3. Assembly code to reverse a string in-place?

    Description: In-place string reversal involves swapping characters from both ends of the string until the entire string is reversed.

    .model small .data msg db 'Hello, world!', '$' len equ $ - msg left_index equ 0 right_index equ len - 2 .code main proc mov cx, len / 2 ; Set loop counter to half the length of the string reverse_loop: mov al, msg[left_index] ; Load character from left side mov bl, msg[right_index] ; Load character from right side mov msg[right_index], al ; Swap characters mov msg[left_index], bl inc left_index ; Move to next character from left dec right_index ; Move to previous character from right loop reverse_loop ; Repeat until loop counter becomes zero mov dx, offset msg ; Point dx to reversed string mov ah, 9 ; DOS function to print string int 21h ; Call DOS mov ax, 4C00h ; Exit program int 21h ; Call DOS main endp end main 
  4. Assembly program to reverse a string using recursion?

    Description: Recursion can also be used to reverse a string by recursively swapping characters from both ends.

    .model small .data msg db 'Hello, world!', '$' len equ $ - msg left_index equ 0 right_index equ len - 2 .code main proc mov cx, len / 2 ; Set loop counter to half the length of the string call reverseString ; Call the reverseString function mov dx, offset msg ; Point dx to reversed string mov ah, 9 ; DOS function to print string int 21h ; Call DOS mov ax, 4C00h ; Exit program int 21h ; Call DOS main endp reverseString proc cmp left_index, right_index ; Check if indices have crossed jae end_reverse ; If so, exit function mov al, msg[left_index] ; Load character from left side mov bl, msg[right_index] ; Load character from right side mov msg[right_index], al ; Swap characters mov msg[left_index], bl inc left_index ; Move to next character from left dec right_index ; Move to previous character from right call reverseString ; Recursive call end_reverse: ret reverseString endp end 

More Tags

base-conversion loopbackjs viewflipper chm taskmanager treemap readfile nestedscrollview visual-studio-debugging ms-word

More Programming Questions

More Fitness Calculators

More Chemical reactions Calculators

More Math Calculators

More Weather Calculators