获取在数据段中初始化的变量的地址

Getting the address of a variable initialized in the data section

我开始理解汇编语言了。我试图了解数据部分中变量的内存布局和寻址并编写了以下代码

mov bx,char1  ;copies the address to register bx

mov ah,0Eh

mov al,bh       ;moves the higher bit to al

add al,65       ;added 65 to al to see a recognizable asci character on screen

int 10h         ;print statement 1

mov al,bl       ;moves the lower bit to al

add al,50

int 10h         ;**print statement 2**


;**section 2 start**

mov bx,char2

;**section 2 end**


jmp $

char1: db "X"
char2: db "Y"

times 510 - ($-$$) db 0
dw 0xaa55

据我了解,char1 指向内存中的一个字节,char2 指向下一个字节。但我不知道为什么第 2 节的存在会影响 print 语句 2 打印的字符。我已经在 64 位平台的 QEMU 中对此进行了测试。有人可以帮我解决这个问题吗?

提前致谢

addressing of variables in data section

我相信您的困惑源于您的变量位于单独的 'data' 部分中的想法。

许多汇编器将允许您将程序组织成多个部分,如 .stack.data.code,如果您进行这种编程,那么偏移地址插入额外指令后数据项的属性不会改变。

但是您当前的引导扇区代码要简单得多。您根本没有使用部分。您编写的所有内容都会在原处进行编码。

打印地址的代码占用17个字节

在 'section 2 instruction' 的空位中,char1 变量的地址为 19。即 17 加上来自 jmp $ 的 2 个字节说明。

通过插入 'section 2 instruction',char1 变量的地址变为 22。即 17 加上来自 [=14= 的 3 个字节]加上来自jmp $指令的2个字节。

ps 我假设在打印代码之前没有任何内容...