generate asm
You can generate assembly code from the source code or object file. However the way to do it would be different depending on what kind of compiler you use. In this page, I will describe the assembly code generation with gcc.
Method 1 :
The first method is to generate assembly code from C source code. The command in gcc compiler is as follows
Command Format : 'gcc -S sourcefile.c'.
Let's see one example.
HelloWorld.c // Soure code file and contents
#include <stdio.h>
int main()
{
printf("Hello World !");
return 0;
}
Check the source code file in the folder
c:\Samples>dir
Volume in drive C is Windows
Volume Serial Number is F8D3-F022
Directory of c:\RyuCloud\C\Samples
01/12/2017 05:40 PM <DIR> .
01/12/2017 05:40 PM <DIR> ..
09/19/2016 03:37 PM 85 HelloWorld.c
Generate the assembly code as shown below
c:\Samples> gcc.exe -S HelloWorld.c
Confirm that the assembly file (*.s) is generated
c:\Samples>dir
Volume in drive C is Windows
Volume Serial Number is F8D3-F022
Directory of c:\RyuCloud\C\Samples
01/12/2017 05:40 PM <DIR> .
01/12/2017 05:40 PM <DIR> ..
09/19/2016 03:37 PM 85 HelloWorld.c
01/12/2017 06:01 PM 526 HelloWorld.s
If you open HelloWorld.s, you would get the assembly code as shown below
.file "HelloWorld.c"
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "Hello World !\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
call __main
leaq .LC0(%rip), %rcx
call printf
movl $0, %eax
addq $32, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (tdm64-1) 4.9.2"
.def printf; .scl 2; .type 32; .endef
Method 2 :
The second method is to extract assembly code from a object file and the command is as follows.
Command Format : objdump -d -M CPU_name -S objectfile.o
HelloWorld.c // Soure code file and contents
#include <stdio.h>
int main()
{
printf("Hello World !");
return 0;
}
Check the source code file in the folder
c:\Samples>dir
Volume in drive C is Windows
Volume Serial Number is F8D3-F022
Directory of c:\RyuCloud\C\Samples
01/12/2017 05:40 PM <DIR> .
01/12/2017 05:40 PM <DIR> ..
09/19/2016 03:37 PM 85 HelloWorld.c
Generate the object file
c:\Samples> gcc.exe -g -c HelloWorld.c
Check the object file in the folder
c:\Samples>dir
Volume in drive C is Windows
Volume Serial Number is F8D3-F022
Directory of c:\RyuCloud\C\Samples
01/12/2017 05:40 PM <DIR> .
01/12/2017 05:40 PM <DIR> ..
09/19/2016 03:37 PM 85 HelloWorld.c
01/12/2017 06:53 PM 2,055 HelloWorld.o
Generate the asm code
c:\Samples> objdump -d -M intel -S HelloWorld.o
HelloWorld.o: file format pe-x86-64
Disassembly of section .text:
0000000000000000 <main>:
#include <stdio.h>
int main()
{
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
4: 48 83 ec 20 sub rsp,0x20
8: e8 00 00 00 00 call d <main+0xd>
printf("Hello World !");
d: 48 8d 0d 00 00 00 00 lea rcx,[rip+0x0] # 14 <main+0x14>
14: e8 00 00 00 00 call 19 <main+0x19>
return 0;
19: b8 00 00 00 00 mov eax,0x0
}
1e: 48 83 c4 20 add rsp,0x20
22: 5d pop rbp
23: c3 ret
24: 90 nop
25: 90 nop
26: 90 nop
27: 90 nop
28: 90 nop
29: 90 nop
2a: 90 nop
2b: 90 nop
2c: 90 nop
2d: 90 nop
2e: 90 nop
2f: 90 nop