简体   繁体   English

从内存中逐字节读取数据,而无需系统调用或C语言中的库函数

[英]read byte by byte from memory without system call or a library function in C

I want to move information byte by byte from one memory location to another without using any library function. 我想在不使用任何库函数的情况下将信息从一个内存位置逐字节移动到另一位置。 Am using a 16bits architecture emulated in qemu and this code is part of tiny small kernel i am writing, so i can not use any system call 我正在使用qemu中模拟的16位体系结构,并且此代码是我正在编写的微小内核的一部分,因此我无法使用任何系统调用

// The struct i want to copy
struct procinfo info_tmp;
// here i put some stuff in my struct    
info_tmp.pid = tablaprocesos[indiceProceso].pid;
kstrcpy(info_tmp.nombre, tablaprocesos[indiceProceso].nombre);  
info_tmp.segmento = tablaprocesos[indiceProceso].memoria;
if(tablaprocesos[indiceProceso].estado==PROCESO_LISTO)
  info_tmp.estado = 0;
else if(tablaprocesos[indiceProceso].estado==PROCESO_RUN)
  info_tmp.estado = 1;
else if(tablaprocesos[indiceProceso].estado==BLOQ_TECLADO ||
tablaprocesos[indiceProceso].estado==PROCESO_SYSCALL)
  info_tmp.estado = 2;
else
  info_tmp.estado = 3; // Zombie
// Tiempo total = tiempoSYS + tiempoUSER
info_tmp.tiempo = tablaprocesos[indiceProceso].tiempoUSER +
  tablaprocesos[indiceProceso].tiempoSYS;    

// pointer to destination that i want to copy to
infoProc = (struct procinfo *)(((int)es << 16) + (0x0000ffff & ebx));
// now pointer to my source struct  
procinfo * origen = &info_tmp;         
int limit = sizeof(struct procinfo);
int i;
for(i=0;i<limit;i++){  
   //  I need here to read only one byte from my source pointer to a variable "byte"

   // macro written in ASM to copy one byte to a pointed location
   // it writes in another data segment of another process
   WRITE_POINTER_FAR(infoProc,byte);
   // next byte
   origen += 1; 
   infoProc += 1;
}

Is there a direct way to do it without the need to write a small code in ASM to do it manually ? 是否有直接的方法可以完成,而无需在ASM中编写小的代码来手动完成?

Note: This code is part of a 16bits segmented OS kernel (a 64KB segment for each process), the source struct is in the kernel segment and i want to copy it to a location in another process segment and it can not be like *targetBytePointer = *sourceBytePointer. 注意:此代码是16位分段OS内核(每个进程64KB分段)的一部分,源结构在内核分段中,我想将其复制到另一个进程分段中的某个位置,并且它不能像* targetBytePointer = * sourceBytePointer。

// The struct i want to copy
struct procinfo info_tmp;
// here i put some stuff in my struct    
info_tmp.pid = tablaprocesos[indiceProceso].pid;
kstrcpy(info_tmp.nombre, tablaprocesos[indiceProceso].nombre);  
info_tmp.segmento = tablaprocesos[indiceProceso].memoria;

switch(tablaprocesos[indiceProceso].estado) {
case PROCESO_LISTO:
  info_tmp.estado = 0; break;
case PROCESO_RUN:
  info_tmp.estado = 1; break;
case BLOQ_TECLADO :
case PROCESO_SYSCALL:
  info_tmp.estado = 2; break;
default:
  info_tmp.estado = 3; // Zombie
        break;
        }
// Tiempo total = tiempoSYS + tiempoUSER
info_tmp.tiempo = tablaprocesos[indiceProceso].tiempoUSER +
  tablaprocesos[indiceProceso].tiempoSYS;

// pointer to destination that i want to copy to
infoProc = (struct procinfo *)(((int)es << 16) + (0x0000ffff & ebx));
char *dest = (char*) infoProc;
// now pointer to my source struct  
char * origen = (char*) &info_tmp;
int limit = sizeof(struct procinfo);
int i;
for(i=0;i<limit;i++){ 
   char byte;
   //  I need here to read only one byte from my source pointer to a variable "byte"

   // macro written in ASM to copy one byte to a pointed location
   // it writes in another data segment of another process
   byte = *origen;
   WRITE_POINTER_FAR(dest,byte);
   // next byte
   origen += 1;
   dest += 1;
}

I finally wrote a small ASM code to do it manually. 我终于写了一个小的ASM代码来手动完成它。 I had to, because as i noted before it is a fragment of code of a 16bits kernel i am writing and emulating in qemu and that does memory segmentation (64KB for each process and 10 processes total). 我必须这样做,因为正如我之前指出的那样,它是我在qemu中编写和仿真的16位内核代码的一部分,并且进行内存分段(每个进程64KB,总共10个进程)。 I can not do *targetBytePointer = *sourceBytePointer as targetPointer is in the process segment and sourcePointer is in the kernel segment and that is why i have the macro WRITE_POINTER_FAR for. 我不能做*targetBytePointer = *sourceBytePointer因为targetPointer在进程段中,sourcePointer在内核段中,这就是为什么我有WRITE_POINTER_FAR宏的原因。 Basically WRITE_POINTER_FAR (written is ASM) will use the destination pointer that i already created combining what the calling process passed to me in two registers (ES:BX), and will write only one byte to that destination and that's why i needed to read only one byte from my source each time. 基本上WRITE_POINTER_FAR(写为ASM)将使用我已经创建的目标指针,将调用过程在两个寄存器(ES:BX)中传递给我的东西组合在一起,并且将仅向该目标写入一个字节,这就是为什么我只需要读取的原因每次来自我的来源一个字节。

char byte;
struct procinfo * origen = &info_tmp;  
for(i=0;i<limit;i++){
  asm( // leer un byte
    "mov %1,%%ebx   \n\t"
    "mov (%%ebx),%0  \n\t"
    :"=r"(byte)
    :"r"(origen)
  );
  WRITE_POINTER_FAR(infoProc,byte);
  origen += 1;
  infoProc += 1;
}

Edit: another simple solution 编辑:另一个简单的解决方案

char *byte;
struct procinfo * origen = &info_tmp;  
for(i=0;i<limit;i++){     
  byte = (char*)origen;
  WRITE_POINTER_FAR(infoProc,*byte);
  origen += 1;
  infoProc += 1;
}
// actualizar puntero de proceso
if(indiceProceso+1<=MAX_PROCESOS)
  indiceProceso++;
return 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM