大会EAX寄存器复位没有道理 [英] Assembly EAX register resetting without reason

查看:168
本文介绍了大会EAX寄存器复位没有道理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下组装code:

I have the following assembly code:

; 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

,直到你开始通过reverseLoop_2循环工作正常。使用GDB,EAX被列为是11,它应该是(这是我通过一个单独的C程序传入的字符串的长度)。这是展现在调试器:

Which works fine until you start looping through reverseLoop_2. Using gdb, eax is listed as being 11, which it should be (this is the length of the string I passed in through a separate c program). This is show in the debugger as:

Breakpoint 2, reverseLoop_2 () at strrev.asm:40
40      mov al, [esi]
(gdb) display $eax
1: $eax = 11

不过,如果我通过程序到下一行一步,它重置为0。

However, if I step through the program to the next line, it resets to 0.

(gdb) next
41      mov bl, [edi]
1: $eax = 0

我需要EAX是preserved自多少次一个保持跟踪reverseLoop_2需要循环。为什么重置为0,来电后MOV?

I need eax to be preserved since its the one keeping track of how many times reverseLoop_2 needs to loop. Why is it resetting to 0 after the call to mov?

推荐答案

如果你使用 EAX 作为一个循环计数器,你不应该写它里面的循环:

If you're using eax as a loop counter, you shouldn't write to it inside the loop :

reverseLoop_2:
  mov al, [esi]

记住是最显著字节 EAX

这篇关于大会EAX寄存器复位没有道理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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