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

I am working on programming a Linux loader to assign an HW, and I know that I need to create a function that will read / write a word from / to memory. I am having trouble deciding whether I am on the right track or not, and any help would be great. I would test these functions, but at the moment this is not possible for me, since I need to work on creating basic functions before I can put everything together. I was given get_byte (segment, offset) / put_byte (char, segment, offset), which were tested to work by my teacher, encoded from the 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 that hello and little need to be combined, is it normal to add or should I just do low and hello and then return this result? Let me know if I completely leave. Is it true that the offset must be unsigned or must 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 correct, but this is my best guess.

Does anyone know a solution or have any tips? These functions should be fairly simple, but I am stuck and have to complete the real part of my job.

UPDATE (12:46): Fixed put_byte (), as indicated below, it would be pointless to accept only two arguments, this has been fixed. I apologize for my mistake.

+3
source share
2 answers

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;

, put_byte , . .

+3

. , 2 , :

word = (byte2 << 8) | byte1

, byte2 1 , , . 2 , :

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

.

+2

Source: https://habr.com/ru/post/1788030/


All Articles