Chinaunix首页 | 论坛 | 博客
  • 博客访问: 929048
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: LINUX

2009-07-05 14:05:21

   这是我的第二个汇编:

[root@bjxdurs235 c-study]# vi maximum.s

.section .data
 data_items:
.long 78,23,32,33,11,67,230,0

.section .text
 .globl _start
_start:
movl $0 , %edi
movl data_items(,%edi,4) , %eax
movl %eax , %ebx

start_loop:
 cmpl $0 , %eax
 je exit_loop
 incl %edi
 movl data_items(,%edi,4) , %eax
 cmpl %ebx , %eax
 jle start_loop
 movl %eax , %ebx
 jmp start_loop

exit_loop:
 movl $1 , %eax
 int $0x80

~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
~                                                                                                                                  
"maximum.s" 25L, 357C written                                                                                    
[root@bjxdurs235 c-study]# as maximum.s -o maximum.o
[root@bjxdurs235 c-study]# ld maximum.o -o maximum
[root@bjxdurs235 c-study]# ./maximum
[root@bjxdurs235 c-study]# echo $?
230

   昨天去青年湖游泳了,晚上回来实在太累了,倒头就睡着了,否则昨天就把这个小程序搞明白了,现在来搞(晕,现在来搞这个说法说来是出自yodao的daydayup啊,他经常说这个,弄的我现在也经常说这个啦),
   程序开始在数据段里定义一个long类型的数组,数组中每个元素占用4个字节(32位),其特征是:最后一个元素是0(这很重要,后来的loop循环就是根据这个来判断是否结束循环的)
   在正文的循环体中做了这些:
将第一个数组元素保存在寄存器%ebx中,开始循环比较,如果当前的数组元素比%ebx里的数字小,则continue,否则更新ebx寄存器里的数字,循环的结束条件是当前的元素为0了
   结束循环的时候用到了上一节学到的系统调用_exit,这里图了个巧,将最大的数保存在了ebx中,ebx寄存器正好保存的是_exit系统调用退出的状态码。
   ok,总结一下,这个小汇编比第一个只有8行的汇编入门程序是丰富了一些,最起码里面有了几个小小的逻辑判断,还有循环这样的结构,程序一步步的走下去,我学到的有:
1、汇编长啥样
2、几个简单,估计以后会经常看到的汇编指令,如jmp,cmpl,je,jle等等
3、寄存器的知识扫盲
阅读(707) | 评论(0) | 转发(0) |
0

上一篇:第一个汇编

下一篇:打印enum变量的段错误

给主人留下些什么吧!~~