16位汇编代码中的OFFSET是什么意思?
我正在查看 16 位实模式的一些示例汇编代码。
我遇到了这样的问题:
mov bx, cs
mov ds, bx
mov si, OFFSET value1
pop es
mov di, OFFSET value2
这是在做什么?那里有“OFFSET”有什么作用?
I am going through some example assembly code for 16-bit real mode.
I've come across the lines:
mov bx, cs
mov ds, bx
mov si, OFFSET value1
pop es
mov di, OFFSET value2
what is this doing? What does having 'OFFSET' there do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(6)
活泼老夫2024-08-17 01:56:16
在x86 16bit模式下,地址空间不是平坦的;相反,地址由偏移量和“段”组成。 “段”指向一个64K的空间,偏移量在该空间内。
雄赳赳气昂昂2024-08-17 01:56:16
来自 MASM 程序员指南 6.1(Microsoft 宏汇编器)
OFFSET 运算符
地址常量是一种特殊类型的立即操作数,由偏移量或段值组成。 OFFSET 运算符返回内存位置的偏移量,如下所示:
mov bx, OFFSET var ;加载偏移地址
有关 OFFSET 的 MASM 5.1 行为和 MASM 6.1 行为之间的差异的信息,请参阅附录 A。
由于不同模块中的数据可能属于单个段,因此汇编器无法知道每个模块在段内的真实偏移量。因此,var 的偏移量虽然是立即值,但直到链接时才确定。
如果您仔细阅读,最终值是在您“链接”目标代码以创建 DLL/EXE 后确定的。在链接之前,您拥有的只是一个立即值,它表示距段基地址的偏移量。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
正如其他一些答案所说,
offset
关键字是指距定义它的段的偏移量。但请注意,段可能会重叠,并且一个段中的偏移量可能与另一段中的不同。例如,假设您在实模式下有以下段汇编器发现
foo
位于距data SEGMENT
基点的偏移量0100h
处,因此无论何时看到offset foo
,它都会输入值0100h
,而不管当时DS
的值是什么。例如,如果我们将
DS
更改为data
段基址以外的内容,则汇编程序会假设:在第二个示例中,
DS
为 < code>0300h,因此DS
指向的段的基址是03000h
。这意味着ds:[offset foo]
指向地址03000h + 0100h
,它与02000h + 01100h
相同,后者指向 <代码>栏。As some of the other answers say, the
offset
keyword refers to the offset from the segment in which it is defined. Note, however, that segments may overlap and the offset in one segment may be different in another segment. For instance, suppose you have the following segment in real modeThe assembler sees that
foo
is at offset0100h
from the base ofdata SEGMENT
, so wherever it seesoffset foo
it will put the value0100h
, regardless of the value ofDS
at the time.For example, if we change
DS
to something other than the base of thedata
segment the assembler is assuming:In the second example
DS
is0300h
, so the base of the segment pointed to byDS
is03000h
. This means thatds:[offset foo]
points to the address03000h + 0100h
which is the same as02000h + 01100h
, which points tobar
.