GNU as 汇编问题求助!

编译打包和其他
回复
ybsolar
帖子: 8
注册时间: 2009-05-11 17:05

GNU as 汇编问题求助!

#1

帖子 ybsolar » 2009-08-19 23:55

这个程序的目的是:对于给出的三组数据,求出最大值。
应用的方法:编写一个子函数---max,对于每组数据分别调用max子函数,求出三个数值,在选出三个数中的最大者。
但是,编译,链接过程都没有报错,运行时一直出现“段错误139”,
用gdb时显示为:子函数中的“movl (%ebx),%ecx“ 和 ”movl (%ebx,%edi,4),%eax“ 出现段错误,
求助问题的解决方法。

代码: 全选

#PURPOSE: Find the maximum number in these three data items,
#         for every item we will call MAX to find the maximum one
#		  in THIS item.
#
#VARIABLE: %edx - Holds the MAXIMUM data of the three items.
#
#The following memory locations are used:
#
#data_itemN - contains the item data.  
#
#

.section .data
    data_item1:             
        .long 3,67,34,222,45,75,1,34,44,33,22,11,66,4
	data_item2:
		.long 98,23,43,54,23,67,8,50,98,74,39,26,36,7
	data_item3:
		.long 95,90,77,34,75,58,39,34,85,34,45,65,45,9
	data_items_end:
	    .equ DATA_SIZE1,data_item2-data_item1
		.equ DATA_SIZE2,data_item3-data_item2
		.equ DATA_SIZE3,data_items_end-data_item3


.section .text
.globl _start
_start:
    movl  $data_item1,%ecx
	pushl %ecx
	pushl $DATA_SIZE1
	call max
	movl %eax,%edx            # %edx holds the maximum
	addl $8,%esp              # put %esp back

	movl $data_item2,%ecx
	pushl %ecx
	pushl $DATA_SIZE2
	call max
	addl $8,%esp              # set %esp back
    cmpl %edx,%eax
	jle label
    movl %eax,%edx
    
label:
	movl $data_item3,%ecx
	pushl %ecx
	pushl $DATA_SIZE3
	call max
	addl $8,%esp               # set %esp back
    cmpl %edx,%eax
	jle end
	movl %eax,%edx

end:
    movl %edx,%ebx
	movl $1,%eax
	int $0x80


#PURPOSE:This program finds the maximum number of a set of data items.
#
#VARIABLES:The registers have the following uses:
#
# %edi - Hold the index of the data item being examined
# %esi - Hold that how much ".long" datas there is
# %ecx - Largest data item found now
# %eax - Current data item
# %ebx - The addr of the data
#
.type max,@function
max:
    pushl %ebp
	movl %esp,%ebp
	pushl %edx                   # save %edx, beacuse it would be used in the MAIN procedure

	xor  %edx,%edx
	movl 12(%ebp),%eax
	movl $4,%ebx
	idivl %ebx
	movl %eax,%esi               #the numbers of data is in %esi

    movl $0,%edi
	movl 16(%ebp),%ebx
[color=#FF0000]	movl (%ebx),%ecx[/color]

start_loop:
    cmpl %edi,%esi 
	je exit_loop
	incl %edi
[color=#FF0000]	movl (%ebx,%edi,4),%eax[/color]
	cmpl %ecx,%eax
	jle start_loop

	movl %eax,%ecx
	jmp start_loop

exit_loop:
	popl %edx                      # restore %edx
	movl %ecx,%eax

	movl %ebp,%esp
	popl %ebp
	ret
ybsolar
帖子: 8
注册时间: 2009-05-11 17:05

Re: GNU as 汇编问题求助!

#2

帖子 ybsolar » 2009-08-20 12:52

问题已解决:自己把错误找到了! :em06 :em11
回复