【发布时间】:2012-11-27 15:19:15
【问题描述】:
我正在尝试反转汇编中的字符串。 但是我的代码似乎无法正常工作。 我添加了一个换行符以提高可读性。
我使用 linux 和 nasm 作为编译器。
我想如果我把地址指针的值转换到正确的位置,字符串最终会被反转然后恢复正常。
这是我的代码:
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
这是我得到的输出:
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) 当你交换字符时,你应该交换字节,而不是双字。 2) ecx 应该从 helloLen-1 开始。 3)算法的终止条件错误;你最终会交换每个字符两次。 (当 eax >= ecx 时停止)