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 9079407
In Process

The Archive Base Latest Questions

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

this is the actual code that I’m trying to convert to gcc style asm

  • 0

this is the actual code that I’m trying to convert to gcc style asm inline assembly code.

#include<iostream>
using namespace std;

int reverse(int num);

int main(){
    int num;
    cout << "enter number: ";
    cin >> num;
    cout << endl;
    cout << reverse(num);
    return 0;
}

int reverse(int num){
    if(num == 0 || num == 1){
        return num;
    }
    __asm
    {
        xor eax, eax
        xor ecx, ecx     
        mov ebx, num    
        clc             ; clear carry
not_found:
        inc ecx
        shl ebx, 1
        jnc not_found


        rcr eax, 1
        mov edx, ecx
again:
        shl ebx, 1
        rcr eax, 1
        inc ecx
        cmp ecx, 32
        jne again

        dec edx     
again2:     
        shr eax, 1
        dec edx
        cmp edx, 0
        jne again2
    }

}

As i was unable to compile the above code with gcc, I tried converting it to some thing that could be compiled successfully by a gcc compiler, but so far I could not manage to produce any meaningful result.

  • 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-16T19:49:48+00:00Added an answer on June 16, 2026 at 7:49 pm

    Based on the code in the comment of the OP, here is a modified example that shifts by one bit using inline assembly:

    #include<iostream>
    using namespace std;
    int reverse(int num);
    int main()
    {
        int num;
        cout << "enter number: ";
        cin >> num;
        cout << endl;
        cout << reverse(num) << endl;
        return 0;
    }
    
    int reverse(int num)
    {
        if (num == 0 || num == 1)
        {
            return num;
        }
        int temp = 0;
        asm( "shll $1, %0 \n\t" : "=r"(temp) : "0"(num));
        return temp;
    }
    

    Note that gcc puts the operands into registers if you ask for it using the r constraint so really no reason to mov them yourself (at least in this small sample).
    Also, I have used 0 in the input constraint to signal that it should be the same register as the output, because that’s how shl works.

    Still not sure about the num == 1 condition, but presumably that’s because we don’t know the actual function of the complete function.

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

Sidebar

Related Questions

This is some of my actual code that deals with exposing C++ classes to
Here's a simplified version of my code that compiles: #include <iostream> class pos {
Full disclosure: this is for an assignment, so please don't post actual code solutions!
Note, this isn't my code, or the actual code in the form. This is
This is not an actual Xcode error message, it is a warning that has
I'm trying to display transparent UINavigationBar on top of Scrollview. This is actual result
This is actual code, how do I pass list[i].fnctn correctly to the click handler?
Okay so I do this (not actual code) try { final View view =
What are your thoughts on code that looks like this: public void doSomething() {
I have some code that I am trying to make it play nicely with

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.