在IT技术领域,人们经常说到绑定/关联寄存器和段的概念,但我想知道具体是什么被绑定了。
在IT技术领域,人们经常说到绑定/关联寄存器和段的概念,但我想知道具体是什么被绑定了。
ASSUME
指令告诉汇编器使用哪个段寄存器来访问一个段。.MODEL SMALL
.286
;Segment are laid out sequentially, starting from X and aligned on 16 bytes.
;
;_DATI X
;_DATI2 X + 10h
;_DATI3 X + 20h
;
;All the variables testX are the first variables in a segment so their
;addresses are the same of their segments
_DATI SEGMENT PARA PUBLIC 'DATA' USE16
test1 dw 0
_DATI ENDS
_DATI2 SEGMENT PARA PUBLIC 'DATA' USE16
test2 dw 0
_DATI2 ENDS
_DATI3 SEGMENT PARA PUBLIC 'DATA' USE16
test3 dw 0
_DATI3 ENDS
_CODE SEGMENT PARA PUBLIC 'CODE' USE16
;Use CS to access labels defined inside _CODE and use _CODE to compute those offsets
;Use DS to access names defined inside _DATI and use _DATI to compute offsets whenever DS is explicitly used as a segment register
;... and so on
ASSUME CS:_CODE, DS:_DATI, ES:_DATI2
;NOTE: _DATI3 NOT ASSUMED!
__START__:
;No explicit segment override, find the segment of test1 (_DATI) and use
;the assumed register (DS).
;Assembled into mov ax, WORD PTR [0000] (A1 00 00)
mov ax, WORD PTR [test1]
;No explicit segment override, find the segment of test2 (_DATI2) and use
;the assumed register (ES).
;Assembled into mov bx, WORD PTR es:[0000] (26 8B 1E 00 00)
mov bx, WORD PTR [test2]
;Explicit segment override, use the segment assumed for ES (_DATI2) to
;calculate the offset (0000h).
;Assembled as the previous mov cx, WORD PTR es:[0000] (26 8B 0E 00 00)
mov cx, WORD PTR es:[test2]
;Explicit segment override, use the segment assumed for DS (_DATI) to
;calculate the offset (0010h).
;Assembled as the previous mov dx, WORD PTR es:[0010] (8B 16 10 00)
mov dx, WORD PTR ds:[test2]
;OFFSET of X is always relative to the segment X is declared in.
;This is true for MASM mode only, IDEAL mode use the group
;Both use an offset of 0, as both test1 and test2 are the first variables
;of their segments
mov ax, OFFSET test1 ;mov ax, 0000 (B8 00 00)
mov bx, OFFSET test2 ;mov bx, 0000 (BB 00 00)
;No explicit segment override, find the segment of test3 (_DATI3) and
;use the assumed register (none)
;Can't assemly: error -> Can't address with currently ASSUMEd segment registers
mov ax, WORD PTR [test3]
;Explicit segment override, calculate offset of test3 with respect of the
;segment assumed for DS (_DATI)
;Offset is 20h
mov bx, WORD PTR ds:[test3] ;mov bx, WORD PTR [0020] (8B 1E 20 00)
;OFFSET operator don't use assumed register
mov cx, OFFSET test3
_CODE ENDS
END __START__
ASSUME
CS
,汇编器会报错:
因为您在一个没有被任何地方当前段无法到达 CS
ASSUME
的代码段中定义了一个标签__START__
。mov dx, WORD PTR ds:[test2]
,汇编器将计算相对于_DATI
段开头的test2
的偏移量,得到0010h。然而,在我的反汇编中,我发现test2
被计算为位于偏移量0000h处(即相对于_DATI2
而不是_DATI
)。我使用TASM 4.1和MASM 6.14汇编了上面的清单(不包括错误的行),在两种情况下都看到了相同的结果。我是否误解了这一点,或者您知道什么可以解释这种差异? - ntwk