Linux下的一个简单汇编程序, .long声明数据的长度问题!

[c-sharp]  view plain copy
  1. #PURPOSE: This program finds the maximum number of a  
  2. #     set of data items.  
  3. #  
  4. #VARIABLES: The registers have the following uses:  
  5. #  
  6. # %edi - Holds the index of the data item being examined  
  7. # %ebx - Largest data item found  
  8. # %eax - Current data item  
  9. #  
  10. # The following memory locations are used:  
  11. #  
  12. # data_items - contains the item data. A 0 is used  
  13. # to terminate the data  
  14. #  
  15.  .section .data  
  16. data_items:         #These are the data items  
  17.  .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0  
  18.   
  19.  .section .text  
  20.  .globl _start  
  21. _start:  
  22.  movl $0, %edi      # move 0 into the index register  
  23.  movl data_items(,%edi,4), %eax # load the first byte of data  
  24.  movl %eax, %ebx    # since this is the first item, %eax is  
  25.             # the biggest  
  26.   
  27. start_loop:         # start loop  
  28.  cmpl $0, %eax      # check to see if we've hit the end  
  29.  je loop_exit  
  30.  incl %edi      # load next value  
  31.  movl data_items(,%edi,4), %eax  
  32.  cmpl %ebx, %eax    # compare values  
  33.  jle start_loop     # jump to loop beginning if the new  
  34.             # one isn't bigger  
  35.  movl %eax, %ebx    # move the value as the largest  
  36.  jmp start_loop     # jump to loop beginning  
  37.   
  38. loop_exit:  
  39.  # %ebx is the status code for the _exit system call  
  40.  # and it already has the maximum number  
  41.  movl $1, %eax      #1 is the _exit() syscall  
  42.  int $0x80  

 

程序中,

data_items:
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0

定义了一组数。.long指示声明一组数,每个数占32位,相当于C语言中的数组。

 

然后就逐一比较大小,最后找到一个最大的数,并把它作为程序的退出状态,调用_exit系统调用。

 

以下是具体操作结果:

 

[c-sharp]  view plain copy
  1. [root@localhost Desktop]# as max.s -o max.o  
  2. [root@localhost Desktop]# ld max.o -o max  
  3. [root@localhost Desktop]# ./max  
  4. [root@localhost Desktop]# echo $?  
  5. 222  

输出结果是:222

很正常!结果和预期一致。

 

但是后来我把数组的第四个元素的值222改成256...

再编译运行!

[c-sharp]  view plain copy
  1. [root@localhost Desktop]# as max.s -o max.o  
  2. [root@localhost Desktop]# ld max.o -o max  
  3. [root@localhost Desktop]# ./max  
  4. [root@localhost Desktop]# echo $?  
  5. 0  
 

输出结果是:0

 

然后我又把它改成257,再编译运行!

[c-sharp]  view plain copy
  1. [root@localhost Desktop]# as max.s -o max.o  
  2. [root@localhost Desktop]# ld max.o -o max  
  3. [root@localhost Desktop]# ./max  
  4. [root@localhost Desktop]# echo $?  
  5. 1  

输出结果是:1

 

看结果,我猜想。

1.

这是数据溢出了。

long申请的内存空间只有一个byte...8位的空间。

2.

书上说,.long声明的每个数占32位!差太远了吧!

想深一层,这会不会是Linux系统的_exit系统调用返回数据的大小只有8位!

 

个人觉得后者解释更为合理!

 

写出此文希望可以召唤高手来帮忙正解,帮小弟驱除灰色地带!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值