16位汇编代码中的OFFSET是什么意思?

发布于 2024-08-10 01:56:15 字数 216 浏览 12 评论 0原文

我正在查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无声情话 2024-08-17 01:56:16

正如其他一些答案所说, offset 关键字是指距定义它的段的偏移量。但请注意,段可能会重叠,并且一个段中的偏移量可能与另一段中的不同。例如,假设您在实模式下有以下段

data SEGMENT USE16 ;# at segment 0200h, linear address 2000h

    org 0100h
    foo db 0

    org 01100h
    bar db 0

data ENDS

汇编器发现 foo 位于距 data SEGMENT 基点的偏移量 0100h 处,因此无论何时看到 offset foo,它都会输入值 0100h,而不管当时 DS 的值是什么。

例如,如果我们将 DS 更改为 data 段基址以外的内容,则汇编程序会假设:

mov ax, 200h            ; in some assemblers you can use @data for the seg base
mov ds, ax

mov bx, offset foo          ; bx = 0100h
mov byte ptr [bx], 10       ; foo = 10


mov ax, 300h
mov ds, ax

mov bx, offset foo          ; bx = 0100h
mov byte ptr [bx], 10       ; bar = 10, not foo, because DS doesn't match what we told the assembler

在第二个示例中,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 mode

data SEGMENT USE16 ;# at segment 0200h, linear address 2000h

    org 0100h
    foo db 0

    org 01100h
    bar db 0

data ENDS

The assembler sees that foo is at offset 0100h from the base of data SEGMENT, so wherever it sees offset foo it will put the value 0100h, regardless of the value of DS at the time.

For example, if we change DS to something other than the base of the data segment the assembler is assuming:

mov ax, 200h            ; in some assemblers you can use @data for the seg base
mov ds, ax

mov bx, offset foo          ; bx = 0100h
mov byte ptr [bx], 10       ; foo = 10


mov ax, 300h
mov ds, ax

mov bx, offset foo          ; bx = 0100h
mov byte ptr [bx], 10       ; bar = 10, not foo, because DS doesn't match what we told the assembler

In the second example DS is 0300h, so the base of the segment pointed to by DS is 03000h. This means that ds:[offset foo] points to the address 03000h + 0100h which is the same as 02000h + 01100h, which points to bar.

浅唱々樱花落 2024-08-17 01:56:16

它仅表示该符号的地址。这有点像 & C 中的运算符,如果您熟悉的话。

It just means the address of that symbol. It's a bit like the & operator in C, if you are familiar with that.

晚雾 2024-08-17 01:56:16

offset 表示 si 寄存器将等于变量 value1 的偏移量(而不是其实际值)。偏移量是从存储变量的内存段开始的地址。偏移量通常相对于 ds 段(在您的情况下 ds 和 cs 寄存器指向同一段)。

offset means that si register will be equal to the offset of the variable value1 (not to its actual value). Offset is the address from the beginning of memory segment where the variable is stored. The offset is usually relative to ds segment (in your case ds and cs registers are pointing to the same segment).

活泼老夫 2024-08-17 01:56:16

在x86 16bit模式下,地址空间不是平坦的;相反,地址由偏移量和“段”组成。 “段”指向一个64K的空间,偏移量在该空间内。

请参阅http://en.wikipedia.org/wiki/Memory_segmentation

In x86 16bit mode, address space is not flat; instead, addresses are composed of an offset and a "segment". The "segment" points to a 64K space, offset is within that space.

See http://en.wikipedia.org/wiki/Memory_segmentation

雄赳赳气昂昂 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 后确定的。在链接之前,您拥有的只是一个立即值,它表示距段基地址的偏移量。

From MASM Programmer's Guide 6.1 (Microsoft Macro Assembler)

The OFFSET Operator

An address constant is a special type of immediate operand that consists of an offset or segment value. The OFFSET operator returns the offset of a memory location, as shown here:

    mov     bx, OFFSET var  ; Load offset address

For information on differences between MASM 5.1 behavior and MASM 6.1 behavior related to OFFSET, see Appendix A.

Since data in different modules may belong to a single segment, the assembler cannot know for each module the true offsets within a segment. Thus, the offset for var, although an immediate value, is not determined until link time.

If you read carefully, the final value is determined after you "link" your object code to create a DLL/EXE. Prior to linking, all you have is an immediate value which represents the offset from the segment's base address.

大姐,你呐 2024-08-17 01:56:16

偏移基本上是距线段点(也称为基准点)的距离。
例如段地址是0000,偏移量或逻辑地址是0100,那么可以通过将两对相加来计算物理地址。
物理地址=0000+0100=0100
意味着我们需要的位置是0100这个地址。
类似地,如果段地址为 1DDD 并且偏移量为 0100,则:
物理地址是:1DDD+0100=1EDD

意味着我们的目的地是1EDD。

Offset is basically the distance from the segment point(also called datum point).
for example segment address is 0000 and the offset or logical address is 0100 then the physical address can be counted by adding the two pairs.
Physical Address = 0000+0100=0100
Means that our required location is on the address of 0100.
Similarly if segment address is 1DDD and offset is 0100 then :
Physical address is : 1DDD+0100=1EDD

Means that our destination is 1EDD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文