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]
                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


Avatar of undefined
Last Comment

8/22/2022 - Mon

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?

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.

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

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;

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


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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" ....
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Thank you all worked. Now to change it to decrement. I just need to change
addl to subl and adc to sbb right?

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

ravenpl is awesome. I salute his spirit of responding query on time. Your answer works great .. Looking forward to your help in future.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck