免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3475 | 回复: 4
打印 上一主题 下一主题

linux下汇编调用printf的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-15 19:21 |只看该作者 |倒序浏览
小弟最近在学linux下汇编语言,可是在汇编中调用C库函数确出现了问题。gdb调试后,发现是在进入第10行的jmp done语句中的printf时,输出是死循环,不断的输出数字。小弟百思不得其解,望大虾能不吝赐教,万分感激。
  1 .section .data
  2 output:
  3 .asciz "%d "
  4 .section .text
  5 .globl main
  6 main:
  7 nop
  8 movl $28 , %ecx
  9 movl $1 , %ebx
10 jmp done
11 movl $1 , %eax
12 jmp done
13 done:
14 pushl %ebx
15 pushl $output
16 call printf
17 loop1:
18 movl %ebx , %edx
19 addl %eax , %ebx
20 movl %edx , %eax
21 jmp done
22 loop loop1
23 movl $1 , %eax
24 movl $2 , %ebx
25 int $0x80
调试过程如下:
(gdb) b * main+1
Breakpoint 1 at 0x80483c5: file fibonacci.s, line 8.
(gdb) run
Starting program: /home/chunlun/Test/s/fibonacci

Breakpoint 1, main () at fibonacci.s:8
8 movl $28 , %ecx
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) s
9 movl $1 , %ebx
(gdb) p $ecx
$1 = 28
(gdb) s
10 jmp done
(gdb) p $ecx
$2 = 28
(gdb) s
done () at fibonacci.s:14
14 pushl %ebx
(gdb) p $ecx
$3 = 28
(gdb) s
15 pushl $output
(gdb) p ecx
No symbol "ecx" in current context.
(gdb) p $ecx
$4 = 28
(gdb) s
done () at fibonacci.s:16
16 call printf
(gdb) p $ecx
$5 = 28
(gdb) s
1 4 7 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 78 82 86 90 94 98 102 107 112 117 122 127 132 137 142 147 152 157 162 167 172 177 182 187 192 197 202 207 212 217 222 227 232 237 242 247 252 257 262 267 272 277 282 287 292 297 302 307 312 317 322 327 332 337 342 347 352 357 362 367 372 377 382 387 392 397 402 407 412  …………
从调试结果看,%ecx并没有被覆写。

论坛徽章:
0
2 [报告]
发表于 2011-05-16 06:43 |只看该作者
21 行为啥要JUMP DONE? 你确定要跳回第13行么?

论坛徽章:
0
3 [报告]
发表于 2011-05-16 16:24 |只看该作者
21行的jmp done是在计算出%ebx的值后,将其打印出来。关键是程序还没有运行到21行,在第一次调用printf处就已经出错了。

论坛徽章:
0
4 [报告]
发表于 2011-05-18 12:47 |只看该作者
回复 3# ren_jerk


    肯定死循环啊!
3 done:
14 pushl %ebx
15 pushl $output
16 call printf
17 loop1:
18 movl %ebx , %edx
19 addl %eax , %ebx
20 movl %edx , %eax
21 jmp done  ---》貌似loop指令没办法运行哦,而且这里形成了死循环!
22 loop loop1

论坛徽章:
0
5 [报告]
发表于 2011-05-18 16:01 |只看该作者
回复 4# cluter


    多谢楼上。我现在貌似有点明白了。自己在调调吧。不懂在问。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP