函数调用后,ESI和EDI会更改值

时间:2018-12-19 12:27:24

标签: windows assembly nasm

我正在尝试使用另一个文件中定义的转换函数将一些表示二进制数的字符串转换为它们的实际值。

这是我的代码:

main.asm

bits 32

global start        

%include 'convert.asm'

extern exit, scanf, printf
import exit msvcrt.dll   
import scanf msvcrt.dll
import printf msvcrt.dll

section data use32 class=data
    s DB '10100111b', '01100011b', '110b', '101011b'
    len EQU $ - s
    res times len DB 0

segment code use32 class=code
    start:
        mov     ESI, s ; move source string 
        mov     EDI, res ; move destination string
        mov     ECX, len ; length of the string
        mov     EBX, 0

        repeat:
            lodsb ; load current byte into AL
            inc     BL
            cmp     AL, 'b' ; check if its equal to the character b
            jne     end ; if its not, we need to keep parsing
            push    dword ESI ; push the position of the current character in the source string to the stack
            push    dword EDI ; push the position of the current character in the destination string to the stack
            push    dword EBX ; push the current length to the stack
            call    func1 ; call the function
            end:
        loop     repeat

        push    dword 0  
        call    [exit]      

convert.asm

func1:
    mov     ECX, [ESP] ; first parameter is the current parsed length
    mov     EDI, [ESP + 4] ; then EDI
    mov     ESI, [ESP + 8] ; and ESI
    sub     ESI, ECX

    parse:
        mov     EDX, [ESI]
        sub     EDX, '0'
        mov     [EDI], EDX
        shl     dword [EDI], 1
        inc     ESI
    loop    parse

    ret     4 * 3

我注意到,尽管在函数调用之后我仍然遇到访问冲突错误。通话后ESI具有一些随机值。难道我做错了什么?我认为参数推送部分应该没问题。在转换函数内部,应该以相反的顺序访问参数。但这由于某种原因没有发生。

我也很确定我使用nasm和alink完成了编译/链接部分。

nasm -fobj main.asm

nasm -fobj convert.asm

alink main.obj convert.obj -oPE -subsys console -entry start

0 个答案:

没有答案