为什么在NASM中我们必须使用方括号([])将MOV移至内存位置? [英] Why in NASM do we have to use square brackets ([ ]) to MOV to memory location?

查看:224
本文介绍了为什么在NASM中我们必须使用方括号([])将MOV移至内存位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有一个名为test的变量声明为:

For example if I have a variable named test declared like:

test db 0x01      ;suppose the address is 0x00000052

如果我做类似的事情:

mov rax, test     ;rax = 0x00000052
mov rax, [test]   ;rax = 0x01

但是,当我尝试保存在其中时,如果我们遵循相同的模式:

But, when I try to save in it, if we're following the same pattern:

mov test, 0x01    ;address 0x00000052 = 0x01
mov [test], 0x01  ;address 0x01 = 0x01

但实际上是:

mov [test], 0x01  ;address 0x00000052 = 0x01

那么,为什么方括号根据它们是第一个还是第二个操作数而表现不同?

So, why the square brackets behave differently depending if they are the first or second operands?

推荐答案

在大多数汇编程序中,使用方括号会取消引用内存位置.您正在将该值当作一个内存地址.

In most assemblers, using square brackets dereferences a memory location. You are treating the value as a memory address.

例如,让我们以它为例.

For example, let's take this for an example.

mov ax, [0x1000]

这将获得0x1000的值并将其放入AX.如果删除方括号,则只能移动0x1000.

This will get the value at 0x1000 and put it into AX. If you remove the square brackets, you only move 0x1000.

如果将值移动到数字,则将其放入值(内存位置)中.

If you move a value to a number, you are putting it into the value (memory location).

如果您被别人欺负学习C并称您为巨魔",请不要让这个示例惹恼您.

Don't let this example annoy you if you've been bullied into learning C by others, calling you a 'troll'.

如果需要,您可以忽略它,但是如果您知道C,则可能已经知道scanf().

You can ignore this if you want but you might have known about scanf() if you know C.

int a = 10;
scanf("%d", a);

现在,这是一个非常常见的错误,因为我们没有获取变量的内存地址.相反,我们将其值用作地址. scanf()函数要求您提供地址.

Now, this is a very common mistake because we are not getting the memory address of the variable. Instead, we are using its value as the address. The scanf() function requires you to give the the address.

如果我们这样做,

scanf("%d", &a);

我们将拥有变量a的地址.

we would have the address of the variable a.

这篇关于为什么在NASM中我们必须使用方括号([])将MOV移至内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆