Assembler /usr/bin/as
Linker /usr/bin/ld
Assembly
as exit.s -o exit.o
Link
ld exit.o -o exit
GCC
gcc main.s -o main
echo $?
$1
- exit() syscall
General-purpose registers
%eax
%ebx
%ecx
%edx
%edi
%esi
Additional registers
%edp
%esp
%eip
%eflags
Portions
%eax
4 bytes
%ax
2 bytes
%ah
1 byte
%al
1 byte
.section .data
list:
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
.section .text
hello:
.ascii "hello\0"
_start
entry symbol
.globl _start
_start:
# ... code
.byte
- 1 storage location [0-255] byte
.int
- 2 storage location [0-65535] double-byte
.long
- 4 storage locatons [0-4294967295] word
.section .data
list:
.long 5,1,2,3
# 1 element of the list has 4 storage location = 4 bytes
# 4 elements of list = 4 * 4 bytes = 16 bytes
.ascii
- 1 storage locaton per character (1 byte)
.section .text
hello:
.ascii "hello\0"
# 'h', 'e', 'l', 'l', 'o', '\0' = 6 bytes
# 6 numeric codes, one for each character
ADDRESS(%OFFSET,%INDEX,MULTIPLIER)
FINAL = ADDRESS + %OFFSET + %INDEX * MULTIPLIER
load value at memory address ADDRESS
to register %eax
movl ADDRESS, %eax
move 1 byte from string_start to %ebx register
MULTIPLIER = 1 | 2 | 4
final = string_start + %eax * 1
movl string_start(,%eax,1), %ebx
move value from memory by address %eax
to register %ebx
movl (%eax), %ebx
Move N
bytes from address %eax
to register %ebx
movl 1(%eax), %ebx
movl 2(%eax), %ebx
movl 4(%eax), %ebx
Move constant value 12 (decimal) into %eax
register
movl $12, %eax
Move value by address 12
into %eax
register
movl 12, %eax
movl %eax, %ebx
movl
Move long.
Move word at time.
movb
Move byte.
Move byte at time.
movl %eax, %ebx
movl BEGINNINGADDRESS(,%INDEXREGISTER,WORDSIZE)
list
- is the location number of the start of our number list
each number has 4 storage locations
(because we declared it using .long
type)
%edi = 0
movl list(,%edi,4), %eax
cmpl a, b
cmpl
command compare two values and put the result into status register.
%eflags
- status register
Compare a register to equal zero.
cmpl $0, %eax
je
- jump if values were equal
jg
- jump if the second value was greater than the first value.
jge
- jump if the second value was greater or equal t o the first value
jl
- jump if the second value was less than the first value
jle
- jump if the second value was less or equal than the first value
jmp
- jump no matter what.
Increment of counter
incl %edi
To exit from the program put $1
as exit command to a
rigister
and call $0x80
interruption.
movl $1, %eax
int $0x80
Before function call we need to push function arguments to the stack
int add(int a, int b);
Stack:
- int b
- int a
- return address <-
%esp
Move top of the stack value to %eax
register
movl (%esp), %eax
Move top+1 stack value to %eax
register
movl 4(%esp), %eax
%eip
instruction pointer (point to the start of the function)
%esp
top of the stack address
%ebp
base pointer register using to access function parameters and local variables
Move current stack top to base pointer
movl %esp, %ebp
Push base pointer onto stack
pushl %ebp
Stack-frame:
- Parameter n
n*4 + 4(%ebp)
- ...
- Parameter 2
12(%ebp)
- Parameter 1
8(%ebp)
- Return address
4(%ebp)
- Old
%ebp
(%ebp)
and(%esp)
pushl
popl
call
Assemple, Link and run GDB
as --gstabs max.s -o ./bin/max.o
ld ./bin/max.o -o ./bin/max
gdb ./bin/max
(gdb) quit
(gdb) break 12
(gdb) run
(gdb) info breakpoints
(gdb) info registers
(gdb) print/d $eax # decimal
(gdb) print/f $eax # float
(gdb) print/h $eax # hex
(gdb) print/o $eax # octo
(gdb) print/u $eax # unsigned decimal
(gdb) print/a $eax # address
(gdb) print/c $eax # character
Size
(gdb) x/b $eax # byte
(gdb) x/h $eax # half word
(gdb) x/w $eax # word
(gdb) x/g $eax # double word
Format
(gdb) x/s $eax # string
(gdb) x/i $eax # instruction
(gdb) stepi
(gdb) nexti
(gdb) where
(gdb) info frame