大会:使用数据段寄存器(DS) [英] Assembly: Using the Data Segment Register (DS)

查看:324
本文介绍了大会:使用数据段寄存器(DS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在学习x86汇编的乐趣之中,我爱单片机编程,所以我熟悉汇编。

Currently I am in the midst of learning x86 assembly for fun, I'm love microcontroller programming, so I'm familiar with assembly.

目前我一直在寻找高和低的回答这个问题,但似乎无法找到它... DS注册,我知道它应该指向全局数据在我的计划,但我不知道它是怎么工作的。我使用NASM,并在最简单的程序我看到以下内容:

Currently I've been searching high and low for the answer to this question, but can't seem to find it... the DS register, I know it's supposed to point to the global data in my program, but I don't know how it works exactly. I'm using NASM, and in most simple programs I see the following:


[org 0x7C00]
[bits 16]  

main:
mov ax, 0x0000
mov ds, ax
mov al, [msg]  
mov ah, 0x0E  
mov bx, 0x0007  
int 0x10    
jmp $  

msg db 'X'

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

和完美的作品(即使我省略了加粗code),但如何?请问CPU自动地加载全局变量开始为0x0000?或者是有什么内在这里我失踪?

and that works perfectly (even if I omit the bolded code), but how? Does the CPU automagically load the global variables starting at 0x0000? or is there something intrinsic here that I'm missing?

推荐答案

很简单:乘以16段寄存器的值(移位位4位置至左),​​然后添加偏移

When the computer is under real mode (the mode the CPU is at when the BIOS executes the bootloader), the method the CPU uses to calculate the address is very simple: Multiply segment register value by 16 (shift bits 4 positions to left), then add the offset.

例如像指令MOV AX,[×1234]的CPU将使用DS * 0×10 + 0×1234的有效地址(第一项解析为零你的情况)。当你有一个像 MOV AX,[BP + 0x32],那么CPU将采用SS *为0x10 + BP + 0x32。需要注意的是,现在的CPU使用了不同的段寄存器(堆栈段),这是因为使用BP寄存器时,CPU会认为你wan't默认访问的堆栈(但你可以通过重写此[DS :BP + 0x32])

For instance in an instruction like "mov ax, [0x1234]" the CPU would use "DS * 0x10 + 0x1234" as the effective address (the first term resolves to zero in your case.) When you have one like "mov ax, [BP+0x32]" then the CPU will use "SS * 0x10 + BP + 0x32". Note that now the CPU used a different segment register (the Stack Segment), and that is because when the BP register is used, the CPU assumes you wan't to access the stack by default (but you can override this by using [DS:BP + 0x32]).

多个O少了什么我已经解释,更可以在 http://wiki.osdev.org/Real_Mode 找到和<一个href=\"http://www.internals.com/articles/protmode/realmode.htm\">http://www.internals.com/articles/protmode/realmode.htm和许多更多的地方。

More o less what I've explained and more can be found at http://wiki.osdev.org/Real_Mode and http://www.internals.com/articles/protmode/realmode.htm and lots of more places.

顺便说一句,味精的位置应该或多或少地0x7C11地址。

BTW, "msg" should be located more or less at 0x7C11 address.

这篇关于大会:使用数据段寄存器(DS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆