0

I got an assignment from my college to create thisenter image description here

Now I know there might be some way, that I can copy the disassembly of my C code I created from a debugger, I guess...

If there is a way please let me know... Or atleast...

Help me with the disassembly of this code I created...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Logic loop
int to_base3(int decimal_number) 
{
    //To return the function at the end of recursion 
    if (decimal_number == 0)  
        return 0;
    //The Recursion  
    else
        return (decimal_number % 3 + 10 *  to_base3(decimal_number / 3)); 
} 

//Main function  
int main() 
{ 
    int decimal_number;
    char check[20];
    //Inputting the values and checking for characters
    do{                             
        int flag=1;
        printf("Enter the number: ");
        fgets(check, 19, stdin);
        //Loop to check for number or character
        for(int i=0; i<sizeof(check);i++){
            if(isdigit(check[i])){
                flag =0;
                break;
            }
            else{
                continue;
            }
        }
        //The place where to_base3() gets called
        if(flag==0)
        {
            decimal_number = atoi(check);
            printf("Base 3 number: %d\n", to_base3(decimal_number));
        }
        //If flag == 1 i.e. if it's a char, then exit the loop
        else
            break;
    }while(1);
    //The final statement
    printf("Thank you for using my program. Bye!");
    return 0;
} 

Also, if you think that you can come up a shorter assembly code for this problem... Let me know...

16
  • How to remove "noise" from GCC/clang assembly output? explains how to look at compiler-generated asm. godbolt.org matches up source lines with blocks of asm, so explaining how each block implements that statement is easier. Commented Jun 6, 2020 at 14:30
  • 1
    after building your program, assembly can be viewed with objdump -d <program>; see man objdump; also see gdb documentation and the cmd disassemble [symbol] Commented Jun 6, 2020 at 14:50
  • 2
    The best way to get assembly code from a C source file is to generate it directly from the compiler, when compiling it. You can specify the -S option when compiling, e.g. gcc -S file.c This will produce a file called file.s which contains the assembly code. This will be more informative than disassembling an object file. Things like objdump are useful when you don't have access to the source code. Commented Jun 6, 2020 at 14:57
  • 1
    Please note that the user of uppercase, even in your title, is considered shouting, so don't do that. Commented Jun 6, 2020 at 15:11
  • This assignment is asking you to explain what a compiler chooses to emit. Implementing to_base3 by hand in assembly could certainly be more efficient, especially if you convert your integer to a string of ASCII digits. But more importantly, don't make a recursive function, just a loop like in How do I print an integer in Assembly Level Programming without printf from the c library?. Commented Jun 6, 2020 at 15:45

0