Search notes:

GNU assembler

The executable name for the GNU assembler is as.
The main purpose of as is to assemble the output of the compiler (gcc) as object file which is then fed to the linker (ld).

Turning on intel syntax or AT&T syntax

.intel_syntax noprefix
xor eax, eax

.att_syntax prefix
movl %adx, %eax…
See also

Some interesting directives

Some interesting directives are:
.ascii "some text" … Adds text without trailing zero byte
.asciz "some text" … Adds text with trailing zero byte. When concatenating multiple strings (separated by commas), only the final string will be zero terminated.
.bss [n] Assemble the following statements to the end of the bss section. ELF targets allow to add an optional subsection n which is a positive number.
.code32, .code64
.data [n] Assemble the following statements to the end of the data section. The optional n specifies the subsection and default to 0 if omitted.
.def NAM, .endef Define deubging information for the symbol NAM. The definition extends until .endef
.end End of source to be compiled
.func NAM[, LBL], .endfunc If source is assembled with enabled debugging, .func emits the function name as debugging information. LBL is entry point for the function and resolves to NAM prepended with the leading char (which typically is an underscore or nothing, depending on the architecture).
.global SYM Makes symbol visible to ld. .global and .globl are equivalent.
.hidden, .internal, protected ELF visiblity directives.
.if, .else etc. Conditional compilation
.incbin "file"[, skip[, count]] Include file verbatim at current location.
.include "file"
.byte, .word, .long, .quad, .octa .quat is a 8-byte integer, .octa is a 16-byte integer. .short is equivalent to .word, .int is equivalent to .long.
.loc fileno lineno [column] … Emit DWARF2 line number information
.macro … Startof a macro definition
.offset loc Set the location counter to loc in the absolute section. loc must be an absolute expression. Do not confuse with .org
.org new-lc , fill
.reloc offset, reloc_name[, expression]
.section NAM Specify the name of the section into which the following code is assembled.
.text [n] Assemble the following statements to the end of the text section. The optional n specifies the subsection and default to 0 if omitted.
.weak NAMes
.zero n Emit n bytes whose value is 0.

TODO

foo.s:
.text
.globl _start
_start:
	push	$11		# sys_execve
	popl	%eax

	cdq

	pushl %edx		# null-terminator byte(s)
	pushl $0x68732f2f	# //sh
	pushl $0x6e69622f	# /bin
	movl %esp, %ebx		# "/bin//sh" addr

	movl	%edx, %ecx

int $0x80	# syscall
Turning foo.S into foo.o:
$ as foo.s  -o foo.o
$ ld foo.o  -o foo
$ objdump -d foo

Useful preprocessor directives

Useful preprocessor directives:
  • .arch target-architecture (to emit an error if an incompatible instruction for the target architecture is used). Compares to the -march gcc option.
  • .cpu cpu-name: to specify the target processor (also to emit errors when incompatible instructions are used). Compares to the -mcpu gcc option.
  • .include file
  • .macro
  • .if
  • .global (to make a symbol visible to ld).
  • .equ to define a constant. (Compares to the #define preprocessor directive)
  • .set
  • .name to create an alias for register names
  • .size
  • .struct
  • .skip
  • .space
  • .text
  • .string
  • .hword, .2byte, .word, .4byte, .quad, .8byte for 16, 32 and 64 bit variable declarations.
  • .ascii / .asciiz
  • .data
  • .bss (unitialized data)
  • .align …

See also

gcc -S to create assembler code.
gcc -masm=… to select the assembly dialect.
gcc -fverbose-asm to add extra commentary to make the produced assembly file more readable.
inline assembly.
GCC
binutils
assembler, Learning assembler with gas.

Index