- #PURPOSE: This program finds the maximum number of a
- # set of data items.
- #
- #VARIABLES: The registers have the following uses:
- #
- # %edi - Holds the index of the data item being examined
- # %ebx - Largest data item found
- # %eax - Current data item
- #
- # The following memory locations are used:
- #
- # data_items - contains the item data. A 0 is used
- # to terminate the data
- #
- .section .data
- data_items: #These are the data items
- .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
-
- .section .text
- .globl _start
- _start:
- movl $0, %edi # move 0 into the index register
- movl data_items(,%edi,4), %eax # load the first byte of data
- movl %eax, %ebx # since this is the first item, %eax is
- # the biggest
-
- start_loop: # start loop
- cmpl $0, %eax # check to see if we've hit the end
- je loop_exit
- incl %edi # load next value
- movl data_items(,%edi,4), %eax
- cmpl %ebx, %eax # compare values
- jle start_loop # jump to loop beginning if the new
- # one isn't bigger
- movl %eax, %ebx # move the value as the largest
- jmp start_loop # jump to loop beginning
-
- loop_exit:
- # %ebx is the status code for the _exit system call
- # and it already has the maximum number
- movl $1, %eax #1 is the _exit() syscall
- int $0x80
程序中,
data_items:
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
定义了一组数。.long指示声明一组数,每个数占32位,相当于C语言中的数组。
然后就逐一比较大小,最后找到一个最大的数,并把它作为程序的退出状态,调用_exit系统调用。
以下是具体操作结果:
- [root@localhost Desktop]# as max.s -o max.o
- [root@localhost Desktop]# ld max.o -o max
- [root@localhost Desktop]# ./max
- [root@localhost Desktop]# echo $?
- 222
输出结果是:222
很正常!结果和预期一致。
但是后来我把数组的第四个元素的值222改成256...
再编译运行!
- [root@localhost Desktop]# as max.s -o max.o
- [root@localhost Desktop]# ld max.o -o max
- [root@localhost Desktop]# ./max
- [root@localhost Desktop]# echo $?
- 0
输出结果是:0
然后我又把它改成257,再编译运行!
- [root@localhost Desktop]# as max.s -o max.o
- [root@localhost Desktop]# ld max.o -o max
- [root@localhost Desktop]# ./max
- [root@localhost Desktop]# echo $?
- 1
输出结果是:1
看结果,我猜想。
1.
这是数据溢出了。
long申请的内存空间只有一个byte...8位的空间。
2.
书上说,.long声明的每个数占32位!差太远了吧!
想深一层,这会不会是Linux系统的_exit系统调用返回数据的大小只有8位!
个人觉得后者解释更为合理!
写出此文希望可以召唤高手来帮忙正解,帮小弟驱除灰色地带!