Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:23:04+00:00 2026-06-15T03:23:04+00:00

I am trying to reverse a string in assembly. However my code does not

  • 0

I am trying to reverse a string in assembly.
However my code does not seem to work correctly.
I added a newline string for better readability.

I am using linux and nasm as compiler.

I thought that if I took the values of the adresspointers and switched them at the correct place, the string would eventually be reversed and then get back to normal.

This is my code:

section .data
    hello     db 'Hello world!'
    helloLen  equ $-hello
    derp db '=========',10
    derplen equ $-derp

section .text
    global main

main:
    mov eax,0
    mov ecx,helloLen

    reverse:
        ;move pointer
        mov ebx,hello
        add ebx,eax
        push eax

        ;move pointer
        mov eax,hello
        add eax,ecx
        push ecx

        ;switch bytes
        push ebx
        mov ebx,[ebx]
        mov [eax],ebx
        pop ebx
        mov eax,[eax]
        mov [ebx],eax

        ;print text
        mov eax,4
        mov ebx,1
        mov ecx,hello
        mov edx,helloLen
        int 80h

        ;Print newline
        mov eax,4
        mov ebx,1
        mov ecx,derp
        mov edx,derplen
        int 80h

        ;increment and decrement
        pop ecx
        dec ecx
        pop eax
        inc eax

        cmp eax,helloLen
    jne reverse

    end:
        mov eax,1
        mov ebx,0
        int 80h

This is the output I get:

Hello world!Hell=====
Hello worldellol=====
Hello worlllo ol=====
Hello worlo w ol=====
Hello woo wow ol=====
Hello wooooow ol=====
Hello wooooow ol=====
Helloooooooow ol=====
Helloooooooow ol=====
Helooowooooow ol=====
Heoow wooooow ol=====
How o wooooow ol=====
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:23:05+00:00Added an answer on June 15, 2026 at 3:23 am

    The way to reverse a string by swapping characters is to swap the first and last, then the second and next to last, etc. In C, you would write:

    for (i = 0; i < len/2; ++i)
    {
        c = s[i];
        s[i] = s[len-i-1];
        s[len-i-1] = c;
    }
    

    In assembly language, the easiest way is to set up the ESI and EDI registers to point to the start and end of the string, then loop. At each iteration, you increment ESI and decrement EDI. The result looks something like this:

    mov ecx, helloLen
    mov eax, hello
    mov esi, eax  ; esi points to start of string
    add eax, ecx
    mov edi, eax
    dec edi       ; edi points to end of string
    shr ecx, 1    ; ecx is count (length/2)
    jz done       ; if string is 0 or 1 characters long, done
    reverseLoop:
    mov al, [esi] ; load characters
    mov bl, [edi]
    mov [esi], bl ; and swap
    mov [edi], al
    inc esi       ; adjust pointers
    dec edi
    dec ecx       ; and loop
    jnz reverseLoop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write code to reverse a string in place (I'm just
I'm trying to reverse a string and using the below code but the resultant
I am learning C and I'm trying to reverse each string in an array
I am trying to find a way to reverse a string, I've seen alternatives
I am trying to reverse a character string in C Here is what I
I am trying to code a trival interview question of reversing a string. This
I'm trying to reverse an input string var oneway = document.getElementById('input_field').value(); var backway =
I'm trying to reverse words in an array of string variables, but split doesn't
Okay, so I'm trying to reverse a C style string in C++ , and
this is my first question in stackoverflow! im trying to reverse a string using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.