对汇编中的指针和值感到困惑 [英] Confused about pointers and values in Assembly

查看:74
本文介绍了对汇编中的指针和值感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MASM汇编器.让我们看一下这段短代码(从我读过的书中):

I'm using the MASM Assembler. Let's look at this short code (from a book I've been reading):

    .data
var1 BYTE 10h

.code
main proc
    mov esi,OFFSET byteVal
    mov al,[esi] ; AL = 10h

我不完全了解byteVal如何转换为机器代码-我知道它是一个标识符,一个符号名称.据我了解,在运行时byteVal是值为10h的内存地址,对吗?但书上说这两个指示是相同的:

I didn't exactly understand how byteVal is translated to machine code - I know it's an identifier, a symbolic name. As I understand, at runtime byteVal is the memory address of the value 10h, right? but the book says that these two instructions are the same:

mov al var1
mov al,[var1]

我不明白他们为什么这样做.

I don't understand why they do the same.

var1 = 10h的地址

var1 = the address of 10h

[var1] = 10h

[var1] = 10h

这不是真的吗?该书说方括号取消引用"了变量,但说这两个是相同的.

Isn't this true? the book said that square brackets "dereference" the variable, yet it said that these two are the same.

并且如果var1是内存中的位置,那么它与使用OFFSET var1有什么区别?我的意思是,偏移量是内存中的位置,不是吗?书上说:OFFSET 运算符返回一个变量与其封闭的开始的距离部分."为什么第一个变量的偏移量不为零,因为它是细分中的第一个变量?我只是想不通.也许我应该换一本书...

and if var1 is the location in memory, what's the difference between it and using OFFSET var1 then ? I mean, offset is the location in memory, isn't it? and the book said: "The OFFSET operator returns the distance of a variable from the beginning of its enclosing segment." Why then the first variable's offset isn't zero, since it's the first variable in the segment? I just CANT figure it out. Probably I should switch a book...

推荐答案

在MASM中(仅在那里!)动态,var1 content 一起使用>var1.使用标签"naked"的名称称为直接内存操作数".请参阅《 MS MASM 6.0程序员指南》(滚动到第3.2.3节,直接内存操作数").如果您需要该标签的地址,则必须使用 OFFSET -运算符.

In MASM (and only there!) mov al, var1 works with the content of var1. Using the name of the label "naked" is called "Direct Memory Operand". See MS MASM 6.0 Programmer’s Guide (Scroll to section 3.2.3, "Direct Memory Operands"). If you need the address of that label you have to use the OFFSET-Operator.

运算符 [] 有特殊的意义.我将方括号之间的表达式添加到方括号之前的表达式中.

The operator [] has a special meaning. I adds the expression between the brackets to the expression before the brackets.

动态,[var1]

等同于

动态,0 [var1]

等同于

动态,0 + var1

等同于

动态,var1 + 0

等同于

动态,var1

如果您确实不需要它,则不要在MASM中使用[]运算符,因为它有一些副作用.

You shouldn't use the []-operator in MASM, if you don't really need it, since it has some side effects.

这篇关于对汇编中的指针和值感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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