0

I'm learning inline assembly in c and I'm trying to load a byte into another variable so I can eventually get into working with strings.

My current code took a pretty simple approach with my comments showing my logic below:

char load = 't', ret;

asm volatile(
  "mov $1, %%esi;"  /* move arg 1 (load) into %esi */
  "lodsb;"          /* load one bite from %esi into %eax */
  : "=a" (ret)      /* ret in %eax */
  : "S" (load)      /* load in %esi */
);

However, my output gives me a seg fault. Any assistance would be appreciated, thanks!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Dave
  • 65
  • 1
  • 12
  • 1
    Operands use `%` not `$`. So you just put an integer `1` into ESI and used it as a pointer. Use a debugger. (Also, you already asked for it in ESI/RSI, so if you'd written it correctly it would have compiled to a useless `mov %esi, %esi`. Also, if you'd used `"S" (&load)` to get the *address* of the char in a register, you still need a `"memory"` clobber - [How can I indicate that the memory \*pointed\* to by an inline ASM argument may be used?](//stackoverflow.com/q/56432259)) – Peter Cordes Feb 17 '20 at 07:37
  • 1
    (I closed this before I noticed you were passing a char *value* as a pointer instead of an address. After fixing this bug, you'd still segfault from effectively doing `*(char*)'t'`. – Peter Cordes Feb 17 '20 at 07:39
  • Oh, and also `lodsb` modifies ESI/RSI so you need a `"+S"(load)` operand or a dummy output with a matching constraint. Input operands are assumed to be unmodified. (Like source operands for a single instruction; GNU C inline asm constraints are designed to describe a single instruction to the optimizer. They are the same constraints that GCC's internal machine-description files use to describe the real instruction set.) – Peter Cordes Feb 17 '20 at 22:06

0 Answers0