C 汇编EAX寄存器无原因复位
我有以下汇编代码:C 汇编EAX寄存器无原因复位,c,assembly,C,Assembly,我有以下汇编代码: ; File: strrev.asm ; A subroutine called from C programs. ; Parameters: string A ; Result: String is reversed and returned. SECTION .text global strrev _strrev: nop strrev: push ebp mov ebp, esp ; registers ebx,esi
; File: strrev.asm
; A subroutine called from C programs.
; Parameters: string A
; Result: String is reversed and returned.
SECTION .text
global strrev
_strrev: nop
strrev:
push ebp
mov ebp, esp
; registers ebx,esi, and edi must be saved if used
push ebx
push edi
xor esi, esi
xor eax, eax
mov ecx, [ebp+8] ; load the start of the array into ecx
jecxz end ; jump if [ecx] is zero
mov edi, ecx
reverseLoop:
cmp byte[edi], 0
je reverseLoop_1
inc edi
inc eax
jmp reverseLoop
reverseLoop_1:
mov esi, edi ;move end of array into esi
mov edi, ecx ;reset start of array to edi
reverseLoop_2:
mov al, [esi]
mov bl, [edi]
mov [esi], bl
mov [edi], al
inc edi
dec esi
dec eax
jnz reverseLoop_2
end:
pop edi ; restore registers
pop ebx
mov esp, ebp ; take down stack frame
pop ebp
ret
在你开始反向运行之前,这一切都很正常。使用gdb,eax被列为11,应该是11(这是我通过一个单独的c程序传入的字符串的长度)。这在调试器中显示为:
Breakpoint 2, reverseLoop_2 () at strrev.asm:40
40 mov al, [esi]
(gdb) display $eax
1: $eax = 11
但是,如果我单步执行程序到下一行,它将重置为0
(gdb) next
41 mov bl, [edi]
1: $eax = 0
我需要保存eax,因为它记录了reverseLoop_2需要循环多少次。为什么在调用mov后会重置为0?我认为这应该可以工作
mov eax, address of your string
push esi
push edi
mov edi, eax
mov esi, eax
; find end of string
sub ecx, ecx
not ecx
sub al, al
cld
repne scasb
; points to the byte after '0x00'
dec edi
dec edi
; main loop will swap the first with the last byte
; and increase/decrease the pointer until the cross each other
_loop:
cmp esi, edi ; if both pointers meet, we are done
jg _done
mov al, [edi]
mov bl, [esi]
mov [esi], al
mov [edi], bl
inc esi
dec edi
jmp _loop
_done:
pop edi
pop esi
我认为这应该行得通
mov eax, address of your string
push esi
push edi
mov edi, eax
mov esi, eax
; find end of string
sub ecx, ecx
not ecx
sub al, al
cld
repne scasb
; points to the byte after '0x00'
dec edi
dec edi
; main loop will swap the first with the last byte
; and increase/decrease the pointer until the cross each other
_loop:
cmp esi, edi ; if both pointers meet, we are done
jg _done
mov al, [edi]
mov bl, [esi]
mov [esi], al
mov [edi], bl
inc esi
dec edi
jmp _loop
_done:
pop edi
pop esi
如果使用
eax
作为循环计数器,则不应在循环内写入:
reverseLoop_2:
mov al, [esi]
请记住,al
是eax
的最低有效字节:
如果使用
eax
作为循环计数器,则不应在循环内写入:
reverseLoop_2:
mov al, [esi]
请记住,al
是eax
的最低有效字节:
你在痛击艾尔,因此是eax。看我对你的另一个问题的回答;)将寄存器更改为cl有效。但是,返回到c程序main的字符串是“”,或者是空字符串。在将控制返回到c文件之前,是否需要将edi或esi移到ebp寄存器中?您不需要将任何内容存储到ebp中。听起来您没有完全正确地获得角色交换循环的初始或终止条件。将您的代码与可能重复的Scott代码进行比较:我将我的代码与您的代码进行了比较,发现了我所犯的错误。然而,我的输出结果是:str1反转:“dlroW”,其中框是按原样打印的字节。有什么办法解决这个问题吗?你在打艾尔,所以是eax。看我对你的另一个问题的回答;)将寄存器更改为cl有效。但是,返回到c程序main的字符串是“”,或者是空字符串。在将控制返回到c文件之前,是否需要将edi或esi移到ebp寄存器中?您不需要将任何内容存储到ebp中。听起来您没有完全正确地获得角色交换循环的初始或终止条件。将您的代码与可能重复的Scott代码进行比较:我将我的代码与您的代码进行了比较,发现了我所犯的错误。然而,我的输出结果是:str1反转:“dlroW”,其中框是按原样打印的字节。有办法解决这个问题吗?