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).
When as invoked via gcc, the -Wa option forwards arguments to as. In the following example, gcc passes the two options -alh and -L to as:
gcc -c -g -O -Wa,-alh,-L file.c

Symbols and labels

Symbol

A symbol is a case sensitive sequence of the characters az, AZ, 09, ., _, $ which does not start with a digit.
Other characters can be included when embedding the symbol in double quotes ("…"). The " can be escaped with \.
See also the --multibyte-handling option.
Symbols are given by the programmer to name things and then used by the linker and debugger.
A symbol has a name, a type and a value.
The type specifies (among others) relocation information (see also the .type directive)

Local symbols

Local symbols are not saved in object files.
On ELF systems, a symbol that starts with .L is a local symbol.

Labels

A label is a symbol that is immediatly followed by a colon (:).
A label represents the current value of the active location counter.
Labels can be used, for example, as instruction operands.
See also the .set and .eqv directives.

Local lables

Local labels are used to use names temporarily.
The symbol of a local label is (non-negative) integer (for example 1234:).
1234b and 1234f refer to the most recent previous and the next definition of the (local) label.
There are also so-called «dollar-local labels» (1234$:)

Statements

A statement

Kind of statement

The key symbol defines what kind the statement is and thus determines the syntax of the rest of the statement.
  • If the key symbol begins with a letter, the statement is an assembly language instruction.
  • If it begins with a dot, the statement is an assembler directive.

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).
.globl 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 .quad is an 8-byte integer, .octa 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 …

dot symbol

.myaddr: .long defines myaddr to contain its own address.
.=.+4 is equivalent to .space 4

-mtune

-mtune specifies the processor to optimize for.

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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759421684, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/C-C-plus-plus/GCC/as/index(250): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78