So I'm trying to read and write into memory in my own OS, so I'm first trying to get it working in a test program in linux, heres the code:
#include <stdio.h>
#include <stdlib.h>
char MemRead(short addr){
char data;
__asm__ __volatile__ ("mov (%%bx), %%dl" : "=d" (data) : "b" (addr));
return data;
}
void MemWrite(short addr,char c){
__asm__ __volatile__ ("mov %%dl, (%%bx)" : : "b" (addr), "d" (c));
}
int main() {
char d;
unsigned short addr=0xf0f0;
MemWrite(addr, 'a');
d=MemRead(addr);
printf("Hello World,%c",d);
return 0;
}
I tried to compile it with gcc [filename] (my platform is x86_64) and got the error
tmp.c: Assembler messages:
tmp.c:6: Error: `(%bx)' is not a valid base/index expression
tmp.c:12: Error: `(%bx)' is not a valid base/index expression
And when I tried compiling it in x86 that the OS I'm making is in, with gcc -m32 [filename] it returned no error, but a segmentation fault happened during the MemWrite() functions assembly time.
Edit: The assembly part is aparently not necessary, but even without using assembly it still causes the segmentation fault.
Test code:
int main() {
char d;
unsigned short *addr=0xf0f0;
*addr = 'a';
d=*addr;
printf("Hello World,%c",d);
return 0;
}