为什么在将数据从寄存器移动到内存时需要使用 [ ](方括号),而在其他情况下不需要?

Kor*_*gay 0 linux x86 assembly nasm

这是我拥有的代码,它工作正常:

section .bss
    bufflen equ 1024
    buff: resb bufflen

    whatread: resb 4

section .data

section .text

global main

main:
    nop
    read:
        mov eax,3           ; Specify sys_read
        mov ebx,0           ; Specify standard input
        mov ecx,buff        ; Where to read to...
        mov edx,bufflen     ; How long to read
        int 80h             ; Tell linux to do its magic

                            ; Eax currently has the return value from linux system call..
        add eax, 30h        ; Convert number to ASCII digit
        mov [whatread],eax  ; Store how many bytes has been read to memory at loc **whatread**

        mov eax,4           ; Specify sys_write
        mov ebx,1           ; Specify standart output
        mov ecx,whatread    ; Get the address of whatread to ecx
        mov edx,4           ; number of bytes to be written
        int 80h             ; Tell linux to do its work

        mov eax, 1; 
        mov ebx, 0; 
        int 80h
Run Code Online (Sandbox Code Playgroud)

这是一个简单的运行和输出:

koray@koray-VirtualBox:~/asm/buffasm$ nasm -f elf -g -F dwarf buff.asm
koray@koray-VirtualBox:~/asm/buffasm$ gcc -o buff buff.o
koray@koray-VirtualBox:~/asm/buffasm$ ./buff
p
2koray@koray-VirtualBox:~/asm/buffasm$ ./buff
ppp
4koray@koray-VirtualBox:~/asm/buffasm$ 
Run Code Online (Sandbox Code Playgroud)

我的问题是:这两条指令是什么意思:

        mov [whatread],eax  ; Store how many byte reads info to memory at loc whatread
        mov ecx,whatread    ; Get the address of whatread in ecx
Run Code Online (Sandbox Code Playgroud)

为什么第一个使用 [] 而另一个没有?

当我尝试用以下内容替换上面的第二行时:

        mov ecx,[whatread]  ; Get the address of whatread in ecx
Run Code Online (Sandbox Code Playgroud)

可执行文件将无法正常运行,它不会在控制台中显示任何内容。

Mar*_*nau 5

使用方括号和不使用方括号基本上是两种不同的事情:

括号表示给定地址处的内存中的值。

没有括号的表达式意味着地址(或值)本身。

例子:

mov ecx, 1234
Run Code Online (Sandbox Code Playgroud)

意思是:将值1234写入寄存器ecx

mov ecx, [1234]
Run Code Online (Sandbox Code Playgroud)

意思是:将内存中地址1234的值写入寄存器ecx

mov [1234], ecx
Run Code Online (Sandbox Code Playgroud)

意思是:将ecx中存储的值写入内存1234地址

mov 1234, ecx
Run Code Online (Sandbox Code Playgroud)

... 没有意义(在此语法中),因为 1234 是一个无法更改的常数。

Linux“写入”系统调用(INT 80h,EAX=4)需要写入值的地址,而不是值本身!

这就是为什么你不在这个位置使用括号的原因!