area.s
# The area function. (函数使用堆栈传递数据)
#
# 计算给定半径的圆的面积
#
.section .text
.type area, @function
.globl area
area:
push %ebp
movl %esp, %ebp
subl $4, %esp
fldpi
filds 8(%ebp)
fmul %st(0), %st(0)
fmulp %st(0), %st(1)
fstps -4(%ebp)
movl -4(%ebp), %eax
movl %ebp, %esp
pop %ebp
ret
# as -gstabs -o area.o area.s
main.s
# An example of using external functions. (使用外部函数)
#
# 计算给定半径的圆的面积
#
.section .data
precision:
.byte 0x7f, 0x00
.section .bss
.lcomm result, 4
.section .text
.globl _start
_start:
nop
finit
fldcw precision
push $10
call area
addl $4, %esp
movl %eax, result
push $2
call area
addl $4, %esp
movl %eax, result
push $120
call area
addl $4, %esp
movl %eax, result
movl $1, %eax
movl $0, %ebx
# as -gstabs -o main.o main.s
# ld -o main main.o area.o
#if 0
(gdb) b *_start+1
Breakpoint 1 at 0x8048075: file main.s, line 17.
(gdb) run 10
Starting program: /home/area/main 10
Breakpoint 1, _start () at main.s:17
17 finit
(gdb) p $esp
$1 = (void *) 0xbffff130
(gdb) x/20x 0xbffff130
0xbffff130: 0x00000002 0xbffff2c8 0xbffff31e 0x00000000
0xbffff140: 0xbffff321 0xbffff32c 0xbffff33d 0xbffff34c
0xbffff150: 0xbffff36d 0xbffff3a2 0xbffff3b9 0xbffff3c9
0xbffff160: 0xbffff3dd 0xbffff3ee 0xbffff3fc 0xbffff414
0xbffff170: 0xbffff426 0xbffff45a 0xbffff47b 0xbffff497
(gdb) x/s 0xbffff2c8
0xbffff2c8: "/home/area/main"
(gdb) x/s 0xbffff31e
0xbffff31e: "10"
(gdb) x/s 0xbffff321
0xbffff321: "XDG_VTNR=1"
(gdb) x/s 0xbffff32c
0xbffff32c: "XDG_SESSION_ID=1"
(gdb) x/s 0xbffff32b9
0xffff32b9:
(gdb) x/s 0xbffff329
0xbffff329: "=1"
(gdb) x/s 0xbffff33d
0xbffff33d: "HOSTNAME=linux"
(gdb) x/s 0xbffff34c
0xbffff34c: "IMSETTINGS_INTEGRATE_DESKTOP=yes"
(gdb) x/s 0xbffff36d
0xbffff36d: "GPG_AGENT_INFO=/run/user/1000/keyring-uLOeGm/gpg:0:1"
#endif