Avatar of SimonHowald
SimonHowald

asked on 

gcc assembly help in converting windows assembly code to linux gcc.

64-bit counter increment/decrement on 32-bit system.

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

Open in new window

AssemblyCLinux

Avatar of undefined
Last Comment
SimonHowald
Avatar of ravenpl
ravenpl
Flag of Poland image

have You tried gcc built-ins? http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
Also please refer http://www.exit.com/blog/archives/000361.html discussion.

Do You still want the bit2bit translation from windows?
Avatar of SimonHowald
SimonHowald

ASKER

Yes I did look into the second link but it does not compile on linux. I am looking for compilable version of your second link or bit2bit conversion from windows.
Thanks
Avatar of ravenpl
ravenpl
Flag of Poland image

Right - about gcc correctness
#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;
}

Open in new window

Avatar of SimonHowald
SimonHowald

ASKER

thank you I am yet to test but I think you solved my problem and there is one more thing to be done. I need to return the updated counter.
the before the update counter value is there in b and c register. This value should put into local varriable and return it.
int64  increment(__int64 *in_value)
{
  return temp;
}
Avatar of ravenpl
ravenpl
Flag of Poland image


//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;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ravenpl
ravenpl
Flag of Poland image

Although the code works fine(due to volatile), the rv should be placed on the output parametrs, rather than input
: "+o" (*in), "=m" (rv) : : "memory" ....
Avatar of SimonHowald
SimonHowald

ASKER

Thank you all worked. Now to change it to decrement. I just need to change
addl to subl and adc to sbb right?
Avatar of ravenpl
ravenpl
Flag of Poland image

I would rather add -1
<    "1: movl $1, %%ebx\n"
<    " xorl %%ecx, %%ecx\n"
>    "1: movl $-1, %%ebx\n"
>    " movl %%ebx, %%ecx\n"
Avatar of SimonHowald
SimonHowald

ASKER

ravenpl is awesome. I salute his spirit of responding query on time. Your answer works great .. Looking forward to your help in future.
Linux
Linux

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.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo