NASM - 这是地址,价值吗? [英] NASM - is this the address, value?

查看:67
本文介绍了NASM - 这是地址,价值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[memloc] 是指值还是地址?如果它指的是任何一个,那么为什么它既可以作为值又可以作为地址?(见下面的代码,第 4 和 5 行)

Is [memloc] referring to the value or the address? If it's referring to either, then why does it work both as a value and an address? (see code below, lines 4 and 5)

抱歉问了这么长的问题.我对 NASM 中的标签取消引用感到困惑.举个例子:

Sorry for the long question. I'm confused by label dereferencing in NASM. Take this example:

01| section .text
02| ; exiting the program with exit code "15"
03|
04| mov     [memloc], 15 ; move 15 into memloc
05| push    [memloc]     ; push memloc on stack
06| mov     eax, 1       ; prepare exit syscall
07| call    kernel       ; invoke syscall
08|
09| section .data
10| memloc: dd 0    ; let's say this is at address 0x1234

当我运行它时,它以代码 15 退出.它有效!
...但是为什么呢?memlock 不应该没有大括号第 4 行,其中 push 大概需要一个目的地?

When I run it, it exits with code 15. It works!
...but why? Shouldn't memlock be without braces line 4, where push presumably expects a destination?

例如:
04 行的 mov 指令将值 15 移动到 memloc 的地址:

For example:
The mov instruction at line 04 moves the value 15 to the ADDRESS of memloc:

mov     [memloc], 15 ; move 15 into mem @memloc

但是 05 行将存储在 memloc 中的 VALUE 压入堆栈:

But line 05 pushes the VALUE stored at memloc onto the stack:

push    [memloc]     ; push value @memloc on stack

那么,[memloc] 是值(15)还是地址(0x1234)?如果你改为 mov memloc, 15 理论上会发生什么?

So, is [memloc] the value (15) or the address (0x1234)? What happens in theory if you mov memloc, 15 instead?

提前致谢.

推荐答案

mov 指令有 1 个以上的版本.如果编译器 (NASM) 看到 memloc 周围的方括号,它会生成一种形式的 mov 并且如果您的编译器没有看到 memloc 它生成另一种形式的 mov.

There's more than 1 version of the mov instruction. If the compiler (NASM) sees the square brackets around memloc it generates one form of mov and if your compiler doesn't see the square brackets around memloc it generates another form of mov.

考虑以下说明:

mov edx, memloc
mov edx, [memloc]
mov [memloc], edx

它们都是 mov 到/来自同一个目标/源寄存器 EDX,但编译器 (NASM) 将为这些指令生成完全不同的操作码.

They're all mov to/from the same destination/source register EDX but the compiler (NASM) will generate completely different opcodes for these instructions.

第一个 mov 用 5 个字节 0xBA, ?, ?, ?, ?
第二个 mov 被编码为 6 个字节 0x8B, 0x15, ?, ?, ?, ?
第三个 mov 被编码为 6 个字节 0x89, 0x15, ?, ?, ?, ?

The 1st mov is encoded with 5 bytes 0xBA, ?, ?, ?, ?
The 2nd mov is encoded with 6 bytes 0x8B, 0x15, ?, ?, ?, ?
The 3rd mov is encoded with 6 bytes 0x89, 0x15, ?, ?, ?, ?

这 4 个 ? 代表 NASM 分配的 memloc 地址.
在您的问题中使用示例地址 (0x1234) 这将变成:

The 4 ?'s represent the address of memloc as assigned by NASM.
Using the example address (0x1234) in your question this would become:

第一个 mov 被编码为 5 个字节 0xBA, 0x34, 0x12, 0x00, 0x00
第二个 mov 被编码为 6 个字节 0x8B, 0x15, 0x34, 0x12, 0x00, 0x00
第三个 mov 编码为 6 个字节 0x89, 0x15, 0x34, 0x12, 0x00, 0x00

The 1st mov is encoded with 5 bytes 0xBA, 0x34, 0x12, 0x00, 0x00
The 2nd mov is encoded with 6 bytes 0x8B, 0x15, 0x34, 0x12, 0x00, 0x00
The 3rd mov is encoded with 6 bytes 0x89, 0x15, 0x34, 0x12, 0x00, 0x00

这篇关于NASM - 这是地址,价值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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