0

I need to convert my C code into assembly

here is my C code that needs to be converted into assembly(.asm)

int main()
{
   //Variables that stores the numbers
   int num1, num2, num3;
   // Variable that stores the result
   int result;
   // Asking the user to input the value of num1
   printf("Enter the first number:");
   scanf("%d",&num1);
   // Asking the user to input the value of num2
   printf("Enter the second number:");
   scanf("%d",&num2);
   // Asking the user to input the value of num3
   printf("Enter the third number: ");
   scanf("%d",&num3);
   //Performing the operation
   result = num3 - (num1 + num2);
   //Printing the result
   printf("The value of result is: %d",result);
   return 0;
}

3 Answers3

1

Well you theoretically can just compile it using gcc and order it to generate asm listing (which will be assembler), so you could just change it's extension to .asm (https://www.systutorials.com/240/generate-a-mixed-source-and-assembly-listing-using-gcc/)

Generally speaking though, it's bad idea to include scanf and printf in assembly, or any library function for that matter (since they can be really long in asm, specially system calls e.g. read/write/open,printf).

And just in case if you someone on university asked you to write something in ASM they will know it's not written by you.

Also judging by title, staring learning ASM with C listings isn't way to go (due to for example stack operations on procedure call).

MarcinS
  • 43
  • 1
  • 5
0

As Antti Haapala said the usual gcc does the trick. You will get a compiled binaty file that you can read with the command :

objdump -d file

You have lot of tools, to help you read the binary file, such as gdb, xxd, objdump, ...
There is also Ghidra, which is new, but I am not sure about this one.

Hamza Ince
  • 604
  • 17
  • 46
0

If you want to compile directly to assembly with gcc, you can use the -S option. From the gcc manpage:

-S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified.

By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s.

Input files that don't require compilation are ignored.

Community
  • 1
  • 1
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29