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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) explains how to look at compiler-generated asm. https://godbolt.org/ matches up source lines with blocks of asm, so explaining how each block implements that statement is easier. – Peter Cordes Jun 06 '20 at 14:30
  • 1
    after building your program, assembly can be viewed with `objdump -d `; see `man objdump`; also see `gdb` documentation and the cmd `disassemble [symbol]` – Milag Jun 06 '20 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. – Tom Karzes Jun 06 '20 at 14:57
  • 1
    Please note that the user of uppercase, even in your title, is considered _shouting_, so don't do that. – Paul Ogilvie Jun 06 '20 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?](https://stackoverflow.com/a/46301894). – Peter Cordes Jun 06 '20 at 15:45
  • Converting to another number whose decimal digits (reinterpreted as base 3) give the value of the original number is slower, and can easily overflow. The only thing you'd ever want to do with that number is print it as decimal, so you should have just converted to a string in the first place, not a binary `int`. – Peter Cordes Jun 06 '20 at 15:46
  • @TomKarzes Thanks but I wanted this in intel format not AT&T any suggestions for them?? –  Jun 06 '20 at 18:02
  • @PeterCordes But I have to make and compare it with recursion... In that case, can you help me in writing that assembly for me?? It would a great help –  Jun 06 '20 at 18:04
  • @CJackson Any C compiler should support `-S`. The format it produces should match the tool chain used by the compiler. – Tom Karzes Jun 06 '20 at 18:05
  • @CJackson: My reading of the assignment is that you're supposed to compile C to asm, then compare the compiler-generated asm with the C source to see how it matches up. Nowhere do I see any hint that you're supposed to write your own asm by hand. – Peter Cordes Jun 06 '20 at 18:13
  • @TomKarzes yea... gcc use at&t... any compiler you know that support at&t format... That is more easy to read –  Jun 07 '20 at 09:41
  • @PeterCordes Yeaa you are right... I too thought it that way... thats why I asked if there's any way to extract that assembly... But man if you see the disassembly of that code. It's like a jumbotron... I wish if there's a simple way to easily get the code in our easy to understand terms... like ik push esp; mov ebp, esp isn't what we really need... so i wonder if i could remove those type of terms in my code rest all is fine... –  Jun 07 '20 at 09:45
  • If you compile with optimization enabled, much of the noise goes away. e.g. https://godbolt.org/z/5ojUDU uses `gcc -Os` to get `div` instead of a multiplicative inverse. (But it also optimized away the tail recursion into a loop.) Breaking the C up into more statements, e.g. with tmp vars, would make highlighting blocks per statement more useful. See [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). – Peter Cordes Jun 07 '20 at 09:53
  • If you compile without optimization, `push esp` / `mov ebp, esp` are exactly what you should expect to see for traditional frame-pointer setup. Understanding what that does is asm basics that you really need to know, even though that specific behaviour is unnecessary in most functions. – Peter Cordes Jun 07 '20 at 09:58
  • @PeterCordes Alright, thanks I'll look into that optimization part... But one more thing... Can we make it into Intel format?? I really find it difficult to read AT&T –  Jun 09 '20 at 04:22
  • Yes, gcc/clang and `objdump` support GAS's flavour of `.intel_syntax noprefix` which is MASM-like. My answer on [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) which I already linked mentions `-masm=intel`, go read it. The Godbolt link I included in an earlier comment also already uses Intel syntax. If you have any more questions, please try to answer them yourself first, only ask if you don't find the answer in something I already linked, please. – Peter Cordes Jun 09 '20 at 04:25

0 Answers0