简体   繁体   中英

Inline assembly in C with a jump and two return statements

I would like to have a void function which prints whether carry (overflow) happened and print the value of the (possibly overflowed) summand.

This is my try, but it does want to compile:

#include <stdio.h>

typedef unsigned long ulong;

void
func()
{
    ulong a = 3;
    ulong b = 1;
    ulong c;

    __asm__(
            "\taddq\t%2, %q0\n"     /* Add b to a and store in c */
            "\tjc\tcarry"
            : "=r" (c)              /* Outputs */
            : "0" (a), "rme" (b)    /* Inputs */
            );
    printf("no carry\nc = %lu\n", c);
    return;
    __asm__("carry:");
    printf("carry\nc = %lu\n", c);
    return;
}

int
main()
{
    func();
}

However, it runs when I remove the first return statement, but then it prints twice if no carry happened.

How can I do this with two return statements?

You need to useasm goto for that. To do that, add goto after __asm__ , change your label to be a C label, pass the label after the clobbers, and then use a %l to refer to it. Here's your program with those fixes applied:

#include <stdio.h>

typedef unsigned long ulong;

void
func()
{
    ulong a = 3;
    ulong b = 1;
    ulong c;

    __asm__ goto(
            "\taddq\t%2, %q0\n"     /* Add b to a and store in c */
            "\tjc\t%l[carry]"
            : "=r" (c)              /* Outputs */
            : "0" (a), "rme" (b)    /* Inputs */
            :
            : carry
            );
    printf("no carry\nc = %lu\n", c);
    return;
    carry:
    printf("carry\nc = %lu\n", c);
    return;
}

int
main()
{
    func();
}

Though as was mentioned in the comments, for this use case in particular, you should just use __builtin_uaddl_overflow instead, unless your goal is just to learn how to jump out of inline assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM