I was messing around with some kind of x86 build since it appeared in many of my classes. In particular, I would like to provide a comparison and replacement function (CAS) as a user function. This is because I can implement my own locks.
I am using Linux 2.6.31 with GCC 4.1.1 on an Intel processor.
I have the following:
// int cmpxchg(int *dest, int expected, int update) .globl cmpxchg cmpxchg: pushl %ebp movl %esp, %ebp // edx holds dest movl 8(%ebp), %edx // eax holds expected value movl 12(%ebp), %eax // ecx holds the new value movl 16(%ebp), %ecx // cmpxchg dest_addr, exp_value // compare to %eax is implicit lock cmpxchgl %edx, %ecx leave ret
This is the * .s file that I am compiling with my driver program. When I turn on the line
lock cmpxchgl %edx, %ecx
and execute, I get the error "Invalid instruction." When I replace the string
cmpxchgl %edx, %ecx
my code seems to be working fine.
First of all, do you need a lock ? I'm not sure cmpxchgl naturally atomic, so I used lock to be sure. As a user program, am I even allowed to use lock ?
thanks
==================================================== ===============
My last code (for those who may roam here in the future):
// int cmpxchg(int *dest, int expected, int update) .globl cmpxchg cmpxchg: pushl %ebp movl %esp, %ebp // edx holds dest, use eDx for Destination ;-) movl 8(%ebp), %edx // eax holds expected value implicitly movl 12(%ebp), %eax // cmpxchg dest_add, src_value lock cmpxchgl %edx, 16(%ebp) leave ret