C Linux从内存中读/写字(段,偏移量)

发布于 2024-10-14 07:36:57 字数 994 浏览 5 评论 0原文

我正在为硬件分配编写一个 Linux 引导程序,我知道我需要创建一个函数来从内存中读取/写入一个字。我无法确定自己是否走在正确的道路上,任何帮助都会很棒。我会测试这些功能,但目前这对我来说是不可能的,因为我需要先构建核心功能,然后才能将所有内容放在一起。我得到了 get_byte(segment,offset)/put_byte(char,segment,offset) ,它们被我的老师验证可以工作,从汇编编码并导出到 C。

unsigned short get_word(unsigned short segment, unsigned short offset)
{
 unsigned short word, low, hi;
    low = get_byte(segment, offset);
    high = get_byte(segment, offset+1);
    word = low+hi;
    return word;
}

我不确定上述是否正确。我知道高和低需要结合起来,可以添加还是我应该简单地做低和低?你好然后返回结果?如果我完全错了,请告诉我。 offset 是否正确为 unsigned Short 还是应该为 int?

int put_word(unsigned short word, unsigned short segment, unsigned short offset)
{
   unsigned short low, hi;
   low = word | offset; 
   hi = word | offset+1
   put_byte(segment, low);
   put_byte(segment, hi);
   return 0;
}

我不知道上面的代码是否接近正确,但这是我最好的猜测。

有人知道解决方案或有任何提示吗?这些功能应该是非常基本的,但我被困住了,需要继续我的作业的实际部分。

更新(12:46):修复了 put_byte(),如下所述,只接受两个参数是没有意义的,修复了这个问题。我为我的错误道歉。

I am working on programming a Linux booter for a HW assignment and I know that I need to create a function that will read/write a word from/to memory. I am having trouble determining if I am on the right track or not and any help would be great. I'd test these functions but at this time it's not possible for me as I need to work on building the core functions before I can put everything together. I was given get_byte(segment,offset)/put_byte(char,segment,offset) which are verified to be working by my teacher coded from assembly and exported to C.

unsigned short get_word(unsigned short segment, unsigned short offset)
{
 unsigned short word, low, hi;
    low = get_byte(segment, offset);
    high = get_byte(segment, offset+1);
    word = low+hi;
    return word;
}

I am not sure if the above is correct. I know the hi and low need to be combined, is it ok to add or should I simply do low & hi and then return that result? If I am totally off base, let me know. Is it correct for offset to be unsigned short or should it be int?

int put_word(unsigned short word, unsigned short segment, unsigned short offset)
{
   unsigned short low, hi;
   low = word | offset; 
   hi = word | offset+1
   put_byte(segment, low);
   put_byte(segment, hi);
   return 0;
}

I have no idea if the code above is anything close to being right but it's my best guess.

Anyone know the solution or have any tips? These functions should be pretty basic but I'm stuck and need to get going on the real part of my assignment.

UPDATE (12:46): Fixed put_byte(), as stated below, it would not make sense to only accept two arguments, fixed that. I apologize for my mistake.

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

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

发布评论

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

评论(2

焚却相思 2024-10-21 07:36:57

get_word() 中:

word = (high << 8) | low

put_word() 中:

low = word & 0xff;
hi = word >> 8;
put_byte(low, segment, offset);
put_byte(hi, segment, offset+1;

我怀疑 put_byte 仅采用两个参数,正如您所说。这没有意义。

In get_word():

word = (high << 8) | low

In put_word():

low = word & 0xff;
hi = word >> 8;
put_byte(low, segment, offset);
put_byte(hi, segment, offset+1;

I doubt that put_byte only takes two arguments, as you stated. It wouldn't make sense.

我们只是彼此的过ke 2024-10-21 07:36:57

他们错了。为了创建一个以 2 个字节开头的单词,您必须:

word = (byte2 << 8) | byte1

假设内存中 byte2 位于 byte1 之后,并且您希望单词采用小端“格式”。
要将一个单词拆分为 2 个字节,您必须:

byte1 = word & 0xff
byte2 = word >> 8

考虑到上述注意事项。

They're wrong. In order to make a word starting with 2 bytes you have to:

word = (byte2 << 8) | byte1

assuming byte2 follows byte1 in memory and you want your word in little-endian "format".
To split a word in 2 bytes you have to:

byte1 = word & 0xff
byte2 = word >> 8

With the considerations above.

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