首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >组件EAX寄存器无故重置

组件EAX寄存器无故重置
EN

Stack Overflow用户
提问于 2013-05-06 20:48:25
回答 2查看 1.2K关注 0票数 0

我有以下汇编代码:

代码语言:javascript
运行
复制
; 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,这是应该的(这是我通过一个单独的c程序传入的字符串的长度)。这在调试器中显示为:

代码语言:javascript
运行
复制
Breakpoint 2, reverseLoop_2 () at strrev.asm:40
40      mov al, [esi]
(gdb) display $eax
1: $eax = 11

但是,如果我单步执行程序到下一行,它将重置为0。

代码语言:javascript
运行
复制
(gdb) next
41      mov bl, [edi]
1: $eax = 0

我需要保留eax,因为它记录了reverseLoop_2需要循环的次数。为什么在调用mov之后重置为0?

EN

回答 2

Stack Overflow用户

发布于 2013-05-06 22:12:21

如果您使用eax作为循环计数器,则不应在循环内对其进行写入:

代码语言:javascript
运行
复制
reverseLoop_2:
  mov al, [esi]

请记住,aleax的最低有效字节:

票数 6
EN

Stack Overflow用户

发布于 2013-05-06 22:01:59

我想这应该行得通。

代码语言:javascript
运行
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16399100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档