(gdb) b 13
Breakpoint 1 at 0x8048075: file area.s, line 13.
(gdb) run 10 20 30
Starting program: /home/beyes/Program/Assembly/area 10 20 30
Breakpoint 1, _start () at area.s:13
13 finit
(gdb) print $esp
$1 = (void *) 0xbffff430
(gdb) x/20x 0xbffff430
0xbffff430: 0x00000004 0xbffff5c9 0xbffff5eb 0xbffff5ee
0xbffff440: 0xbffff5f1 0x00000000 0xbffff5f4 0xbffff615
0xbffff450: 0xbffff628 0xbffff633 0xbffff643 0xbffff693
0xbffff460: 0xbffff6a5 0xbffff6cf 0xbffff6ef 0xbffff6fa
0xbffff470: 0xbffff71a 0xbffffbbb 0xbffffbe1 0xbffffc13
(gdb) x/s 0xbffff5c9
0xbffff5c9: "/home/beyes/Program/Assembly/area"
(gdb) x/s 0xbffff5eb
0xbffff5eb: "10"
(gdb) x/s 0xbffff5ee
0xbffff5ee: "20"
(gdb) x/s 0xbffff5f1
0xbffff5f1: "30"
(gdb) x/s 0xbffff5f4
0xbffff5f4: "ORBIT_SOCKETDIR=/tmp/orbit-beyes"
(gdb) x/s 0xbffff615
0xbffff615: "SSH_AGENT_PID=1359"
(gdb) x/s 0xbffff628
0xbffff628: "TERM=xterm"
... ...
.section .data
output1:
.asciz "There are %d parameters:\n"
output2:
.asciz "%s\n"
.section .text
.global _start
_start:
movl (%esp), %ecx #读取"参数数目"
pushl %ecx
pushl $output1
call printf #C函数的参数入栈从右到左入栈
addl $4, %esp
popl %ecx
movl %esp, %ebp
addl $4, %ebp #EBP指向第一个命令行参数(即函数名./read)
loop1:
pushl %ecx #printf函数会改变ECX的值,这里要入栈保存起来
pushl (%ebp)
pushl $output2
call printf
addl $8, %esp
popl %ecx #弹出以递减
addl $4, %ebp
loop loop1
pushl $0
call exit
$ ./read 10 20 30
There are 4 parameters:
./read
10
20
30
.section .data
output:
.asciz "%s\n"
.section .text
.global _start
_start:
movl %esp, %ebp
addl $12, %ebp #指向环境变量(不加其他命令行参数运行程序)
loop1:
cmpl $0, (%ebp)
je endit
pushl (%ebp)
pushl $output
call printf
addl $12, %esp
addl $4, %ebp
loop loop1
endit:
pushl $0
call exit
$ ./read2
ORBIT_SOCKETDIR=/tmp/orbit-beyes
SSH_AGENT_PID=1364
SHELL=/bin/bash
TERM=xterm
... ...
.section .data
output:
.asciz "This area is: %f\\n"
.section .bss
.lcomm result, 4
.section .text
.global _start
_start:
nop
finit
pushl 8(%esp) #取得命令行中的半径值(字符串地址)
call atoi
addl $4, %esp #恢复堆栈
movl %eax, result
fldpi
filds result
fmul %st(0), %st(0) #半径平方
fmul %st(1), %st(0) #与pi相乘
fstpl (%esp)
pushl $output
call printf
addl $12, %esp
pushl $0
call exit
$ ./cmd 10
This area is: 314.159265
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |