开发者

C Linux read/write word from memory (segment,offset)

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜