asked on
atomically in_value is incremented and updated value is returned.
Pseudo code...
__int64* increment(__Int64 *in_value)
{
__int64 *temp = NULL;
__asm {
mov edi, in_value
mov eax, [edi]
mov edx, [edi+4]
retry:
mov ecx, edx
mov ebx, eax
add ebx, 1
adc ecx, 0
lock cmpxchg8b [edi]
jnz retry
mov eax, temp
xor edx, edx
mov [eax+edx*4], ebx
mov [eax+edx*4+4], ecx
}
return *temp;
}
It works great. I want it be converted into gcc assembly.
I unsuccessfully converted it in to gcc assembly. But its wrong, i dont know how to pass in_value (hi and low) in one ready.
#define low(x) *(((unsigned int*)&(x))+0)
#define high(x) *(((unsigned int*)&(x))+1)
asm volatile ( "mov %0, %%edi \n"
"mov %1, %%eax \n"
"mov %2, %%edx \n"
"1: \n"
"mov %%edx, %%ecx\n"
"mov %%eax, %%ebx \n"
"add $1, %%ebx \n"
"adc $0, %%ecx \n"
"lock; cmpxchg8b %0\n"
"jnz 1b \n"
"mov %%edx, %%edx \n"
"mov %3, %%ebx \n" //Error
"mov %3, %%ecx \n" //Error
: "=m" (*in_value)
: "m" (low(*in_value)), "m" (high(*in_value)), "m" (*in_value)
: "memory", "ebx", "ecx", "eax", "edi");
in_value should not re-read for one cycle to preserve the atomicity.
thank you
ASKER
#include <stdio.h>
int main()
{
volatile long long target = 7;
unsigned int incr = 7;
__asm__ __volatile__(
" movl %0, %%eax\n"
" movl 4+%0, %%edx\n"
"1: movl %1, %%ebx\n"
" xorl %%ecx, %%ecx\n"
" addl %%eax, %%ebx\n"
" adcl %%edx, %%ecx\n"
"lock; cmpxchg8b %0\n"
" jnz 1b"
: "+o" (target)
: "m" (incr)
: "memory", "eax", "ebx", "ecx", "edx", "cc");
printf("%lld\n", target);
return 0;
}
ASKER
//untested yet
long long inc64(volatile long long *in)
{
long long rv;
__asm__ __volatile__(
" movl 0+%0, %%eax\n"
" movl 4+%0, %%edx\n"
"1: movl $1, %%ebx\n"
" xorl %%ecx, %%ecx\n"
" addl %%eax, %%ebx\n"
" adcl %%edx, %%ecx\n"
"lock; cmpxchg8b %0\n"
" jnz 1b\n"
" movl %%ebx, %1\n"
" movl %%ecx, 4+%1\n"
: "+o" (*in)
: "m"(rv)
: "memory", "eax", "ebx", "ecx", "edx", "cc");
return rv;
}
ASKER
ASKER
Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.
TRUSTED BY
Also please refer http://www.exit.com/blog/archives/000361.html discussion.
Do You still want the bit2bit translation from windows?