通过ESI和指针间接

Har*_*rma 1 x86 assembly

我是程序集编程的新手(使用MASM的x86 asm),并且正在学习ESI寄存器支持的间接,您只需将地址放入ESI,然后使用间接运算符,您就可以访问指向的数据.

Q1.在编码中可以使用[esi + 4]但不能使用esi + 4(结果为错误).为什么?因为在汇编中,间接运算符([])显然不是必需的,主要是为了程序员的理解.

Q2.如果我将间接应用于指针变量,那么它们似乎不起作用.为什么?指针是否仅用作容器.

例如-

mov eax, [esi] ; It sets eax with the value of memory location pointed by esi

mov eax,[ptr4] ; Does not work the same
Run Code Online (Sandbox Code Playgroud)

Ray*_*hen 5

这是MASM语法的一个怪癖.该[... ]是自动插入周围的内存地址的标签.换一种说法

mov eax, [ptr4]
Run Code Online (Sandbox Code Playgroud)

表示"将地址加载4个字节ptr4eax寄存器中".但是ptr4是内存地址的标签,所以即使您忘记使用方括号并写入

mov eax, ptr4
Run Code Online (Sandbox Code Playgroud)

MASM将自动插入括号.这两行的意思相同:"将地址加载4个字节ptr4eax寄存器中."

但是,MASM不会对寄存器参数执行此自动插入:

mov eax, esi ; copy the esi register to the eax register
mov eax, [esi] ; load 4 bytes at the address esi into the eax register
Run Code Online (Sandbox Code Playgroud)

这只是MASM的一个怪癖,你必须习惯.