啊好吧我想做一个反转数组的程序 所以我做了这个
section .data
rev:
push eax
push ebx
push esi
push ecx
mov ebx,ecx
mov eax,esi
Lrev_1:
push dword[esi]
inc esi
loop Lrev_1
mov ecx,ebx
Lrev_2:
pop dword[eax]
inc eax
loop Lrev_2
pop ecx
pop esi
pop ebx
pop eax
ret
msg dd "Hello"
section .text
global _start
_start:
mov esi,msg
mov ecx,5
call rev
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,5
int 80h
mov eax,1
xor ebx,ebx
int 80h
它工作得很好,但正如你所看到的,我将内存地址的所有内容推送到堆栈,这可能很慢(像乌龟)
所以我尝试使用 this way
然后我尽可能地用这 3 种方式实现它
section .data
rev1:
push eax
push ebx
push esi
push edi
Lrev1:
cmp esi,edi
jge Lrev1_out
mov eax,dword[esi]
mov ebx,dword[edi]
mov dword[esi],ebx
mov dword[edi],eax
inc esi
dec edi
jmp Lrev1
Lrev1_out:
pop edi
pop esi
pop ebx
pop eax
ret
rev2:
push esi
push edi
Lrev2:
cmp esi,edi
jge Lrev2_out
push dword[esi]
push dword[edi]
pop dword[esi]
pop dword[edi]
pop edi
pop esi
ret
rev3:
push eax
push esi
push edi
Lrev3:
cmp esi,edi
jge Lrev3_out
mov eax,dword[esi]
xchg eax,dword[edi]
mov dword[esi],eax
inc esi
dec edi
jmp Lrev3
Lrev3_out:
pop edi
pop esi
pop eax
ret
msg dd "Hello"
section .text
global _start
_start:
mov esi,msg
mov edi,esi
add edi,4
;if I call rev1 or rev2 here msg will be reverse into oH for me
;if I call rev3 here msg will be reversed into oHel for me
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,5
int 80h
mov eax,1
xor ebx,ebx
int 80h
好吧,我的预期结果是olleH。但后来我得到了意想不到的结果。 我错过了什么?或者只是添加了更多的东西? 得到真正的反转结果
最佳答案
看起来您正在尝试反转作为 32 位双字加载的字节。
也许这对 rev1 有帮助?
mov al,[esi]
mov bl,[edi]
mov [esi],bl
mov [edi],al
您在 rev2 和 rev3 中有类似的问题。此外,带有内存的 XCHG
的含义远远超出了交换 reg<->mem 的范围……我会小心处理该指令。
关于linux - assembly 中的倒车阵列故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48347774/