为什么在将数据从寄存器移动到内存时需要使用 [ ](方括号),而在其他情况下不需要? [英] Why do I need to use [ ] (square brackets) when moving data from registery to memory, but not when other way around?
问题描述
这是我拥有的代码,它运行良好:
This is the code I have and it works fine:
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
这是一个简单的运行和输出:
Here is a simple run and output:
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$
我的问题是:这两条指令是什么:
My question is: What is with these 2 instructions:
mov [whatread],eax ; Store how many byte reads info to memory at loc whatread
mov ecx,whatread ; Get the address of whatread in ecx
为什么第一个使用 [] 而另一个没有?
Why the first one works with [] but the other one without?
当我尝试将上面的第二行替换为:
When I try replacing the second line above with:
mov ecx,[whatread] ; Get the address of whatread in ecx
可执行文件将无法正常运行,它不会在控制台中显示任何内容.
the executable will not run properly, it will not shown anything in the console.
推荐答案
使用方括号和不使用方括号基本上是两种不同的事情:
Using brackets and not using brackets are basically two different things:
括号表示给定地址的内存中的值.
A bracket means that the value in the memory at the given address is meant.
不带括号的表达式意味着地址(或值)本身.
An expression without a bracket means that the address (or value) itself is meant.
示例:
mov ecx, 1234
意思是:将值1234写入寄存器ecx
Means: Write the value 1234 to the register ecx
mov ecx, [1234]
意思是:将内存中1234地址的值写入寄存器ecx
Means: Write the value that is stored in memory at address 1234 to the register ecx
mov [1234], ecx
意思是:将ecx中存储的值写入内存1234地址
Means: Write the value stored in ecx to the memory at address 1234
mov 1234, ecx
... 没有意义(在此语法中),因为 1234 是一个无法更改的常数.
... makes no sense (in this syntax) because 1234 is a constant number which cannot be changed.
Linux写入"系统调用(INT 80h,EAX=4)需要写入值的地址,而不是值本身!
Linux "write" syscall (INT 80h, EAX=4) requires the address of the value to be written, not the value itself!
这就是为什么你不在这个位置使用括号!
This is why you do not use brackets at this position!
这篇关于为什么在将数据从寄存器移动到内存时需要使用 [ ](方括号),而在其他情况下不需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!