0

I have couple of questions on how to convert C code to Assembly and vice versa but with files program.asm, program.o, program, makefile.

  1. First, I have a lab12.asm file that is a text file that contains assembly code. Second, I have a makefile with the following contents:

    lab12: lab12.o
            ld -o lab12 lab12.o
    
    lab12.o: lab12.asm
            nasm -f elf64 -g -F dwarf lab12.asm
    

Now, to generate an executable file called "lab12" and object file called "lab12.o", in Linux terminal we run the command "make". Now we can call debugger and compiler by typing command "kdbg lab12".

This is how to compile and run the Assembly code.

  1. Now, the second case - C to Assembly. I am using the following two tutorials:

tutorial_1

tutorial_2

In these cases, we get the following files: program.c, program.s, program. I am interested in that how to create "makefile" for this case to be able to call the "make" command and "kdbg program" command. In this state as it is, we can't call the "kdbg" command.

So, my question for this case is, how to generate files program.asm, program.o, program, makefile, to be able to call "make" command and "kdbg program" command?

  1. Third case - Assembly to C. How to do this. Again, I want program.asm, program.o, program, makefile files and to be able to call commands "make" and "kdbg program".

Best Regards.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user300045
  • 239
  • 1
  • 3
  • 8
  • `program.s` usually contains generated assembly language code (output by the compiler), so I'm unclear what you want to achieve in your "second case". and your third case makes no sense - you can't really convert assembly to C automatically (with reasonable results) and I fail to see the benefit in doing it – UnholySheep Nov 14 '17 at 13:48
  • @UnholySheep, In the second case, I want to convert program.s to program.asm in the first case. – user300045 Nov 14 '17 at 13:50
  • What do you mean by *"convert program.s to program.asm"*? You can tell the compiler to output a file with a different name (and ending), but other than that I'm unsure what conversion you have in mind – UnholySheep Nov 14 '17 at 13:51
  • @UnholySheep, In the second case, I want to be able to use "make" command and "kdbg program" command. In the first case, I can do that, but in the second I can't. So I want in the second case to have files program.asm, program.o, program, makefile – user300045 Nov 14 '17 at 13:54
  • What do you mean you can't? Of course you can do that, even without any assembly language steps involved. Just write a makefile that compiles the C source code to an executable with debugging information embedded (`-g` flag in GCC) – UnholySheep Nov 14 '17 at 13:56
  • @UnholySheep, Could you show what should be the content (instructions) of that makefile? – user300045 Nov 14 '17 at 14:00
  • 1
    In second case, isn't your problem with the .s file syntax? `gcc` and `clang` will produce by default AT&T syntax assembly, for `as` (GAS) assembler. While you can switch it to produce intel-syntax assembly, which will look almost as NASM, it will be still not fully compatible. So for C -> asm -> binary use the assembler which the vendor of the C compiler had on mind (as). There's no automatic tool to convert from one assembler syntax to other, which would work 100% correctly in every case (you may find some conversion scripts, which will help, but manual review + fixes are required). – Ped7g Nov 14 '17 at 14:21
  • "3rd case": am I understanding right, that you want your compiler to apply the changes you made in the .S, into the .C file it was made from ?!? – Tommylee2k Nov 14 '17 at 15:04
  • Semi-related: https://stackoverflow.com/questions/36861903/assembling-32-bit-binaries-on-a-64-bit-system-gnu-toolchain about building static binaries with/without libc from gas or NASM sources. Just use `gcc -O3 -g program.c` or `gcc -g program.s` to make an executable. ([You may need `-no-pie -fno-pie`](https://stackoverflow.com/questions/43367427/32-bit-absolute-addresses-no-longer-allowed-in-x86-64-linux) depending on how new your gcc is and what code is in your asm.) – Peter Cordes Nov 14 '17 at 17:31

0 Answers0