C Linux从内存中读/写字(段,偏移量)
我正在为硬件分配编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
get_word()
中:在
put_word()
中:我怀疑
put_byte
仅采用两个参数,正如您所说。这没有意义。In
get_word()
:In
put_word()
:I doubt that
put_byte
only takes two arguments, as you stated. It wouldn't make sense.他们错了。为了创建一个以 2 个字节开头的单词,您必须:
假设内存中 byte2 位于 byte1 之后,并且您希望单词采用小端“格式”。
要将一个单词拆分为 2 个字节,您必须:
考虑到上述注意事项。
They're wrong. In order to make a word starting with 2 bytes you have to:
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:
With the considerations above.