0
votes

I am trying to write inline assembly in a C file and I get 2 errors :

open.c:10: Error: junkptr nombre' after expression`

open.c:10: Error: suffix or operands invalid forles'`

This is my file :

int open(char * nombre, unsigned char modo)
{
  int retval;
  int nrllamada = 6;
  asm("mov $8,%%ah \n\t"
      "les %%bx, dword ptr nombre \n\t"
      "int $0x22 \n\t"
      "mov %%eax,%0 \n\t"
      : "=r"(retval)
      : "a"(modo)
  );
  return retval;
}
3

3 Answers

1
votes
"les %%bx, dword ptr nombre \n\t"

You have ptr nombre inside a string. That is probably not correct. surely you want to reference the char* nombre in the arguments to the function?

1
votes

The correct way to write in registers ES and BX the 32 bits pointer named nombre is to connect the C variable nombre to register ECX, move the lower 16 bits of ECX to register BX, rotate the ECX register 16 bits so the lower 16 bits goes to the higher 16 bits of the same register and vice versa and finally move the lower 16 bits of ECX to ES.

int open(char * nombre, unsigned char modo)
{
  int retval;
  // numero servicio 6
  // nombre del fichero a abrir (ES:BX) y modo (AL) de apertura
  asm("mov $6,%%ah \n\t"
      "mov %%cx,%%bx \n\t"
      "rol $16,%%ecx \n\t"
      "mov %%cx,%%es \n\t"
      "int $0x22 \n\t"
      "mov %%eax,%0 \n\t"
      : "=r"(retval)
      : "a"(modo), "c"(nombre)
  );
  return retval;       
}
0
votes

GCC is 32-bit compiler. This means also, that you should most likely to try to get rid of the es:bx type pointer arithmetic and move to flat 32-bit addressing (where base of cs=ds=es=0 and each segment limit is 0xffffffff).

Accessing status registers in user space of protected mode gives very likely an access violation exception.

Also, what is the OS, where int 22h is allowed and where it expects es:bx input? In MS-DOS it shouldn't be called, as it's just used as a static variable to restore other interrupt vectors.