x86 assembly programming with GNU – tool tips

1. coding pattern

# Pattern used by as and ld
.section .data
< initialized data here>
.section .bss
< uninitialized data here>
.section .text
.globl _start
_start:
< instruction code here>

———————————-
# Pattern used by gcc
.section .data
< initialized data here>
.section .bss
< uninitialized data here>
.section .text
.globl main
main:
< instruction code here>

———————————-

2. as

# common usage
as -o demo.o demo.s

# add debugging info for gdb
as -gstabs -o demo.o demo.s

3. ld

# common usage
ld -o demo demo.o

# C lib function is called instead of syscall
ld -dynamic-linker /lib/ld-linux.so.2 -o demo -lc demo.o

4. gcc

# common usage
gcc -o demo demo.s

# add debugging info for gdb
gcc -gstabs -o demo demo.s

# add statistic info for gprof
gcc -o demo demo.c -pg

# generate assembly source from C source
gcc -S demo.c

5. objdump

# disassemble object file into assemble source
objdump -d demo.o

6. gdb

# common usage
gdb demo
run

# add break point right after ‘_start’/’main’
add ‘nop’ as the first instruction code after the label
break *_start+1

# run the program step by step or continuously
next/step/cont

# check registers and stacks
info reg/frame
print/d/t/x $ebx
d: decimal
t: binary
x: hex

# check the memory
x/nyz &output
n: number of byte segment
y: format of output – c:char, d:decimal, x:hex
z: length of byte segment – b:byte, h:16-bit-byte, w:32-bit-byte

7. gprof

# common usage
gprof demo > gprof.log

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.