在程序集x86中访问寄存器的值时访问写入冲突

时间:2014-12-06 04:57:28

标签: assembly x86 access-violation masm cpu-registers

compare PROTO, p1:DWORD, p2:DWORD
.code
compare proc p1:DWORD, p2:DWORD
    mov eax, p1
    mov edx, p2
    mov eax, [eax] ;Getting an access violation here
    mov edx, [edx] ; Would probably get one here too, why?
    sub eax, edx
    ret
compare endp
main PROC
     LOCAL thesize:DWORD
    mov thesize, 3
    mov fill, 5
    INVOKE compare, thesize, thesize

    ret
main ENDP
嘿伙计们,只是好奇为什么这段代码不起作用?什么是使其工作的另一种方法,我只是在玩寄存器并在填充数组时尝试使用类似的代码,但此时我陷入困境。

提前致谢!

1 个答案:

答案 0 :(得分:1)

不确定您在做什么,但由于您希望取消引用内存地址,因此必须使用addr本地变量来获取/传递该地址。

main PROC
     LOCAL thesize:DWORD
    mov thesize, 3

;    INVOKE compare, thesize, thesize
;    same as:
;    push    3
;    push    3
;    call    compare

    ; this is what you want:
    invoke compare, addr thesize, addr thesize
;    same as:
;    lea     eax, thesize
;    push    eax
;    push    eax
;    call    compare
    ret
main ENDP