麻烦扭转组装一个字符串 [英] Trouble reversing a string in assembly

查看:139
本文介绍了麻烦扭转组装一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扭转组装的字符串。
但是我的code似乎没有正常工作。
我加了一个新行字符串更好的可读性。

我使用Linux和NASM的编译器。

我想,如果我拿了adresspointers的价值和交换他们在正确的地方,该字符串将最终被逆转,然后恢复正常。

这是我的code:

 部分。数据
    你好DB世界,你好!
    helloLen EQU $ -Hello
    DERP DB=========,10
    derplen EQU $ -derp.text段
    全球主要主要:
    MOV EAX,0
    MOV ECX,helloLen    相反:
        ;将指针
        MOV EBX,你好
        添加EBX,EAX
        推EAX        ;将指针
        MOV EAX,你好
        添加EAX,ECX
        推ECX        ;开关字节
        推EBX
        MOV EBX,[EBX]
        MOV [EAX],EBX
        流行EBX
        MOV EAX,[EAX]
        MOV [EBX],EAX        ;打印文本
        MOV EAX,4
        MOV EBX,1
        MOV ECX,你好
        MOV EDX,helloLen
        INT 80H        ;打印换行
        MOV EAX,4
        MOV EBX,1
        MOV ECX,DERP
        MOV EDX,derplen
        INT 80H        ;增量和减量
        流行ECX
        十二月ECX
        流行EAX
        INC EAX        CMP EAX,helloLen
    JNE反向    结束:
        MOV EAX,1
        MOV EBX,0
        INT 80H

这是输出我得到:

 世界,你好!地狱=====
你好worldellol =====
你好worlllo OL =====
你好worloW¯¯OL =====
你好宇哇醇=====
你好wooooow OL =====
你好wooooow OL =====
Helloooooooow OL =====
Helloooooooow OL =====
Helooowooooow OL =====
Heoow wooooow OL =====
Ø如何OL wooooow =====


解决方案

通过交换角色反转字符串的方法是交换第一个和最后一个,然后第二和倒数等在C语言中,你可以这样写

 为(i = 0; I< LEN / 2; ++ I)
{
    C = S [I]
    S [I] = S [len个-I-1];
    S [len个-I-1] = C;
}

在汇编语言中,最简单的办法是设置与ESI和EDI寄存器以指向字符串,则循环的开始和结束。在每次迭代中,你ESI递增和递减EDI。结果看起来是这样的:

  MOV ECX,helloLen
MOV EAX,你好
MOV ESI,EAX; ESI指向字符串开始的
添加EAX,ECX
MOV EDI,EAX
十二月EDI; EDI点结束的字符串
SHR ECX,1; ECX是计数(长度/ 2)
JZ完成的;如果字符串是0或1个字符,做
reverseLoop:
MOV人,[ESI]负载字符
MOV BL,[EDI]
MOV [ESI],BL;和交换
MOV [EDI],人
INC ESI;调整指针
十二月EDI
十二月ECX;和环
JNZ reverseLoop

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=====

解决方案

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

这篇关于麻烦扭转组装一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆