我已经开始了解汇编语言了。我试图理解数据部分中变量的内存布局和寻址并编写了以下代码
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节的存在会影响打印语句2打印的字符。我已经在64位平台的QEMU中对此进行了测试。有人可以帮我克服这个问题吗?
提前谢谢
最佳答案
addressing of variables in data section
我相信您的困惑源于这样的想法:您的变量位于单独的“数据”部分中。
许多汇编器将允许您将程序组织为多个部分,例如 .stack
、.data
和 .code
,并且如果您这样做的话,插入一条额外的指令后,数据项的偏移地址不会改变。
但是您当前的引导扇区代码要简单得多。您根本没有使用部分。您编写的所有内容都会在其所在位置进行编码。
打印地址的代码占用17个字节。
在没有“第 2 节指令”的情况下,char1 变量的地址将为 19。即 17 加上来自 jmp $
指令的 2 个字节.
通过插入“第 2 节指令”,char1 变量的地址变为 22。即 17 加上来自 mov bx, char2
的 3 个字节 加上来自 jmp $
指令的 2 个字节。
ps 我假设在打印代码之前没有任何内容......
关于assembly - 获取数据段中初始化的变量的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67961140/