Search notes:

Learning assembler

gcc is useful when learning the assembly language because of its -S option (which creates assembler code from c source code).
In order to create assembly along with source code, the c source can be compiled with -g and -c and the dumped with objdump -d -S.
This is demonstrated with the following simple source file:
int someFunc(int param_1, int param_2) {

  int ret = param_1 + param_2;
  return ret;

}
Github repository about-assembler, path: /learning/simple/simple.c
gcc -S simple.c produces
	.file	"simple.c"
	.text
	.globl	someFunc
	.type	someFunc, @function
someFunc:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -20(%rbp)
	movl	%esi, -24(%rbp)
	movl	-20(%rbp), %edx
	movl	-24(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	someFunc, .-someFunc
	.ident	"GCC: (GNU) 8.2.1 20181127"
	.section	.note.GNU-stack,"",@progbits
Compiling an object file with debug information and without optimization (gcc -O0 -c -g simple.c) and then dumping it (objdump -d -S simple.o) produces
simple.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <someFunc>:

int someFunc(int param_1, int param_2) {
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	89 7d ec             	mov    %edi,-0x14(%rbp)
   7:	89 75 e8             	mov    %esi,-0x18(%rbp)

  int ret = param_1 + param_2;
   a:	8b 55 ec             	mov    -0x14(%rbp),%edx
   d:	8b 45 e8             	mov    -0x18(%rbp),%eax
  10:	01 d0                	add    %edx,%eax
  12:	89 45 fc             	mov    %eax,-0x4(%rbp)
  return ret;
  15:	8b 45 fc             	mov    -0x4(%rbp),%eax

}
  18:	5d                   	pop    %rbp
  19:	c3                   	retq   

See also

the GNU assembler (gas).

Index