首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从32位转换到8位值,反之亦然给出分段故障。

从32位转换到8位值,反之亦然给出分段故障。
EN

Stack Overflow用户
提问于 2019-04-07 01:32:38
回答 1查看 686关注 0票数 0

这可能是我学习x86汇编语言的最后一个障碍。

下面的子程序给了我一个分段错误:

代码语言:javascript
复制
    ;================================================================= 
    ; RemCharCodeFromAToB - removes all chars between a and e from str
    ; arguments:
    ;   str - string to be processed
    ;   a   - start
    ;   e   - end
    ; return value:
    ;   n/a 
    ;-------------------------------------------------------------------
    RemCharCodeFromAToB:
        ; standard entry sequence
        push    ebp    ; save the previous value of ebp for the benefi$
        mov     ebp, esp ; copy esp -> ebp so that ebp can be used as a $   

        ; accessing arguments   
                                ; [ebp + 0] = old ebp stack frame
                                ; [ebp + 4] = return address
        mov     edx, [ebp + 8]  ; string address

        while_loop_rcc:
            mov cl, [edx]       ; obtain the address of the 1st character of the string
            cmp cl, 0           ; check the null value  

            je  while_loop_exit_rcc     ; exit if the null-character is reached

            mov al, cl ; save cl
            mov cl, [ebp + 16]      ; end-char
            push cx                 ; push end-char
            mov cl, [ebp + 12]      ; start-char
            push cx                 ; push start-char
            push ax;                ; push ch
            call IsBetweenAandB
            add esp, 12

            cmp eax, 0          ; if(ch is not between 'a' and 'e')

            je inner_loop_exit_rcc

            mov eax, edx    ; copy the current address

            inner_loop_rcc:
                mov cl, [eax+1]
                cmp cl, 0
                je  inner_loop_exit_rcc 

                mov [eax], cl

                inc eax
                jmp inner_loop_rcc
            inner_loop_exit_rcc:

            inc edx             ; increment the address
            jmp while_loop_rcc  ; start the loop again
        while_loop_exit_rcc:

        ; standard exit sequence
        mov     esp, ebp        ; restore esp with ebp
        pop     ebp             ; remove ebp from stack
        ret                     ; return the value of temporary variable    
    ;===================================================================

我怀疑从32位到8位寄存器的数据转换有什么问题,反之亦然。我对此的概念还不清楚。

或者,下面的部分有什么问题吗?

代码语言:javascript
复制
        mov al, cl ; save cl
        mov cl, [ebp + 16]      ; end-char
        push cx                 ; push end-char
        mov cl, [ebp + 12]      ; start-char
        push cx                 ; push start-char
        push ax;                ; push ch
        call IsBetweenAandB
        add esp, 12
EN

回答 1

Stack Overflow用户

发布于 2019-04-07 02:28:40

cxax是16位寄存器,所以push cx ; push cx; push ax在堆栈上推送16位值,总共有6个字节。但是IsBetweenAandB显然期望32位值,并且在esp末尾添加12 (而不是6)。所以你可能想要push ecx等等。

此外,在使用eaxecx之前,您可能希望将它们调零。就目前情况而言,它们可能最初包含垃圾,并且只将有用的数据加载到低8位的alcl中。因此,当IsBetweenAandB试图比较整个32位值时,您将得到错误的结果。否则,您希望重写IsBetweenAandB,以便只比较您所关心的低字节。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55554890

复制
相关文章

相似问题

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