使用绝对内存地址初始化指针

发布于 2024-11-30 13:22:58 字数 729 浏览 0 评论 0原文

我正在我正在处理的自定义操作系统项目中实现 put(在屏幕上打印字符串)系统调用。字符串的绝对内存地址由代表 esi 寄存器的 unsigned int 变量给出,我需要初始化一个指向 unsigned char 的指针。 /code> 从中读取字符串的数组。

寄存器内容表示为:

struct regs
{
    unsigned int gs, fs, es, ds;
    unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
    unsigned int int_no, err_code;
    unsigned int eip, cs, eflags, useresp, ss;
};

我已尝试以下方法来初始化指向 esi 中地址的指针:

void fault_handler(struct regs *r) {
   void *p = (void*)r->esi;
   unsigned char* s = (unsigned char*)p;
   // take s and print it to the screen
}

但我没有得到我应该得到的“Hello\n”,而是我得到垃圾。我验证了 esi 的地址确实指向正确的字符串。我遇到的问题是初始化指向该地址的指针。

谢谢!

更新:我将关闭这个问题,并将讨论转移到一个新问题,因为原始答案得到了回答。谢谢大家!

I'm implementing a puts (print a string on screen) system call in a custom OS project I'm working on. The absolute memory address of the string is given to me by an unsigned int variable representing the esi register, and I need to initialize a pointer to an unsigned char array to read the string from.

The registers contents are represented by:

struct regs
{
    unsigned int gs, fs, es, ds;
    unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
    unsigned int int_no, err_code;
    unsigned int eip, cs, eflags, useresp, ss;
};

I have tried the following to initialize a pointer to the address in esi:

void fault_handler(struct regs *r) {
   void *p = (void*)r->esi;
   unsigned char* s = (unsigned char*)p;
   // take s and print it to the screen
}

But I don't get the "Hello\n" I'm supposed to get, instead I get garbage. I verified that the address of esi indeed points to the correct string. The problem I have is to initialize a pointer to this address.

Thanks!

Update: I will close this question and move the discussion to a new question as the original answer is answered. Thank you everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

·深蓝 2024-12-07 13:22:58

您的代码正确地将 esi 分配给 p,然后分配给 s。因此我只能假设您的问题实际上与此步骤无关。

顺便说一句,我不明白为什么你需要 p,只需将 esi 直接分配给 s 即可。

Your code correctly assigns esi to p and then s. Thus I can only assume your problem is not in fact related to this step.

As an aside I don't see why you need p, just assign esi directly to s.

寄居者 2024-12-07 13:22:58

这段代码没问题,正如 @David Heffernan 所说,没有使用 p。您可以直接将esi分配给s。您已经检查了地址,但请检查段寄存器值(实模式)/段选择器(保护模式),确保段寄存器值正确并且esi中的地址与在处理程序中以及描述字符串的位置相同的段。也许这会有所帮助。

The piece of code is okay, and as @David Heffernan says, there is no use of the p. you can simply directly assign esi to s. You have checked the addresses, but please check the segment register value (real mode)/segment selector (protected mode), make sure that the segment register values are correct and the address in esi is used with the same segment when in the handler, and where you describe the string. Probably this will help.

初见终念 2024-12-07 13:22:58

正如其他人已经指出的那样,您可以立即使用 r->esi 初始化 s,而无需中间变量 p。但需要进行强制转换,因此初始化应为 unsigned char* s = (unsigned char *) r->esi;

然而,关于您的代码的一些事情引起了轻微的怀疑。寄存器名称看起来像 x86 平台上 32 位寄存器的名称。你的指针也是32位的吗?即,您是否将代码编译为 32 位代码?

As others alrady noted, you can immediately initialize s with r->esi, without an intermediate variable p. The cast is needed though, so the initialization should look as unsigned char* s = (unsigned char *) r->esi;.

However, some things about your code raise faint suspicions. Register names look like names of 32-bit registers on x86 platform. Are your pointers 32 bit as well? I.e. are you compiling your code as 32-bit code?

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