Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9048071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:03:47+00:00 2026-06-16T12:03:47+00:00

I’m trying to do some Code Optimization to Eliminate Branches, the original c code

  • 0

I’m trying to do some Code Optimization to Eliminate Branches, the original c code is

if( a < b ) 
   k = (k<<1) + 1;
else
   k = (k<<1)

I intend to replace it with assembly code like below

mov a, %rax 
mov b, %rbx
mov k, %rcx
xor %rdx %rdx
shl 1, %rcx
cmp %rax, %rax
setb %rdx
add %rdx,%rcx
mov %rcx, k 

so I write c inline assembly code like blow,

#define next(a, b, k)\
 __asm__("shl $0x1, %0; \
         xor %%rbx, %%rbx; \
         cmp %1, %2; \
         setb %%rbx; \
         addl  %%rbx,%0;":"+c"(k) :"g"(a),"g"(b))

when I compile the code below i got error:

operand type mismatch for `add'
operand type mismatch for `setb'

How can I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:03:51+00:00Added an answer on June 16, 2026 at 12:03 pm

    Here are the mistakes in your code:

    1. Error: operand type mismatch for ‘cmp’ — One of CMP‘s operands must be a register. You’re probably generating code that’s trying to compare two immediates. Change the second operand’s constraint from “g” to “r”. (See GCC Manual – Extended Asm – Simple Constraints)
    2. Error: operand type mismatch for ‘setb’ — SETB only takes 8 bit operands, i.e. setb %bl works while setb %rbx doesn’t.
    3. The C expression T = (A < B) should translate to cmp B,A; setb T in AT&T x86 assembler syntax. You had the two operands to CMP in the wrong order. Remember that CMP works like SUB.

    Once you realize the first two error messages are produced by the assembler, it follows that the trick to debugging them is to look at the assembler code generated by gcc. Try gcc $CFLAGS -S t.c and compare the problematic lines in t.s with an x86 opcode reference. Focus on the allowed operand codes for each instruction and you’ll quickly see the problems.

    In the fixed source code posted below, I assume your operands are unsigned since you’re using SETB instead of SETL. I switched from using RBX to RCX to hold the temporary value because RCX is a call clobbered register in the ABI and used the "=&c" constraint to mark it as an earlyclobber operand since RCX is cleared before the inputs a and b are read:

    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    static uint64_t next(uint64_t a, uint64_t b, uint64_t k)
    {
        uint64_t tmp;
        __asm__("shl $0x1, %[k];"
            "xor %%rcx, %%rcx;"
            "cmp %[b], %[a];"
            "setb %%cl;"
            "addq %%rcx, %[k];"
            : /* outputs */ [k] "+g" (k), [tmp] "=&c" (tmp)
            : /* inputs  */ [a] "r" (a), [b] "g" (b)
            : /* clobbers */ "cc");
        return k;
    }
    
    int main()
    {
        uint64_t t, t0, k;
        k = next(1, 2, 0);
        printf("%" PRId64 "\n", k);
    
        scanf("%" SCNd64 "%" SCNd64, &t, &t0);
        k = next(t, t0, k);
        printf("%" PRId64 "\n", k);
    
        return 0;
    }
    

    main() translates to:

    <+0>:   push   %rbx
    <+1>:   xor    %ebx,%ebx
    <+3>:   mov    $0x4006c0,%edi
    <+8>:   mov    $0x1,%bl
    <+10>:  xor    %eax,%eax
    <+12>:  sub    $0x10,%rsp
    <+16>:  shl    %rax
    <+19>:  xor    %rcx,%rcx
    <+22>:  cmp    $0x2,%rbx
    <+26>:  setb   %cl
    <+29>:  add    %rcx,%rax
    <+32>:  mov    %rax,%rbx
    <+35>:  mov    %rax,%rsi
    <+38>:  xor    %eax,%eax
    <+40>:  callq  0x400470 <printf@plt>
    <+45>:  lea    0x8(%rsp),%rdx
    <+50>:  mov    %rsp,%rsi
    <+53>:  mov    $0x4006c5,%edi
    <+58>:  xor    %eax,%eax
    <+60>:  callq  0x4004a0 <__isoc99_scanf@plt>
    <+65>:  mov    (%rsp),%rax
    <+69>:  mov    %rbx,%rsi
    <+72>:  mov    $0x4006c0,%edi
    <+77>:  shl    %rsi
    <+80>:  xor    %rcx,%rcx
    <+83>:  cmp    0x8(%rsp),%rax
    <+88>:  setb   %cl
    <+91>:  add    %rcx,%rsi
    <+94>:  xor    %eax,%eax
    <+96>:  callq  0x400470 <printf@plt>
    <+101>: add    $0x10,%rsp
    <+105>: xor    %eax,%eax
    <+107>: pop    %rbx
    <+108>: retq   
    

    You can see the result of next() being moved into RSI before each printf() call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.