I'm new to assembly in C, and i dont know how to fix this error. I'm making a function that means to write a file. What I have is:
ssize_t mywrite(int fd, const void *buf, size_t count) {
// return write(fd, buf, count);
ssize_t var;
__asm__("movl $4,%%eax\n\t" // Write
"movl %1,%%ebx\n\t"
"movl %2,%%ecx\n\t"
"movl %3,%%edx\n\t"
"int $0x80\n\t" // System call
"movl %%eax,%0"
:"=r"(var)
:"r"(fd),"r"(buf),"r"(count)
:"%eax","%ebx","%ecx","%edx"
);
return var;
}
My asm is supposed to do the same as write(fd,buf,count); When I compile it, I get "'asm' operand has impossible constraints". However, if don't name the variables and get the values directly from the stack, I get no error. Here's the code
__asm__("movl $4,%%eax\n\t"
"movl 8(%%ebp),%%ebx\n\t"
"movl 12(%%ebp),%%ecx\n\t"
"movl 16(%%ebp),%%edx\n\t"
"int $0x80\n\t"
"movl %%eax,%0"
:"=r"(var)
:
:"%eax","%ebx","%ecx","%edx"
);
I could use the second code, ofc, but I need it compiled with optimization 2. Then %ebp won't point where I need it to. I tried using "a", "b", "c" and "d" instead of "r", but no success. Anyone could help? Thanks :D