简体   繁体   English

段故障(核心哑音)x86汇编AT&T语法

[英]Segment fault (core dumbed) x86 Assembly AT&T syntax

I'm trying to write a code that takes 3 arrays and return the maximum number in the last one (Yes something like in the ProgramminGroundUp book) but I want the function to exit according to array size not when it reaches element zero but the code gaves me 我正在尝试编写一个代码,该代码需要3个数组并在最后一个数组中返回最大数量(是类似ProgramminGroundUp书中的内容),但是我希望函数根据数组大小而不是在元素达到零时退出,而是代码给我

Segment fault (Core Dumped)

I use 'as' assembler' and gnu loader 'ld' Here is the full code 我使用'as'汇编程序'和gnu加载程序'ld'这是完整的代码

.section .data
first_data_items:
.long 48,65,49,25,36
second_data_items:
.long 123,15,48,67,25,69
third_data_items:
.long 102,120,156,32,14,78,100
.section .text
.globl _start
_start:
pushl $first_data_items
pushl $5
call max  
addl $8, %esp
pushl $second_data_items
pushl $6
call max
addl $8, %esp 
pushl $third_data_items
pushl $7
call max
addl $8, %esp
movl %eax, %ebx
movl $1, %eax
int $0x80

.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx   #ecx will be the size of the array
movl 12(%ebp), %ebx  #ebx will be the base pointer
movl $0, %edi         #edi will be the index
movl 0(%ebx, %edi, 4), %eax   #eax will hold the maximum number
start_loop:
cmpl $0, %ecx
je end_loop
incl %edi
movl 0(%ebx, %edi,4), %esi     #esi will hold the current element
cmpl %eax, %esi
jle start_loop
movl %esi, %eax
decl %ecx
jmp start_loop 
end_loop:
movl %ebp, %esp
popl %ebp
ret

I have moved decl %ecx (Thanks to Michael Petch) right after the compare statemnt to make ecx decrement by 1 in every loop so the code will be 在比较statemnt之后,我已经将decl %ecx (感谢Michael Petch)移到了每个循环中,使ecx减1,因此代码将是

.section .data
first_data_items:
.long 48,65,49,25,36
second_data_items:
.long 123,15,48,67,25,69
third_data_items:
.long 102,120,156,32,14,170,100
.section .text
.globl _start
_start:
pushl $first_data_items
pushl $5
call max
addl $8, %esp
pushl $second_data_items
pushl $6
call max
addl $8, %esp
pushl $third_data_items
pushl $7
call max
addl $8, %esp
movl %eax, %ebx
movl $1, %eax
int $0x80
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx   #ecx will be the size of the array
movl 12(%ebp), %ebx  #ebx will be the base pointer
movl $0, %edi         #edi will be the index
movl 0(%ebx, %edi, 4), %eax   #eax will hold the maximum number
start_loop:
cmpl $0, %ecx
je end_loop
decl %ecx
incl %edi
movl 0(%ebx, %edi,4), %esi         #esi will hold the current element
cmpl %eax, %esi
jle start_loop
movl %esi, %eax
jmp start_loop
end_loop:
movl %ebp, %esp
popl %ebp
ret

About my comment about lodsd usage, example of max function: 关于我对lodsd用法的评论,例如max函数的示例:

.type max, @function
max:
    pushl   %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %ecx       # ecx will be the size of the array
    movl    12(%ebp), %esi      # esi will be the base pointer
    popl    %ebp                # stack_frame usage complete, restore ebp
    lea     (%esi,%ecx,4), %ecx # ecx = (base pointer + 4*size) (end() ptr)
    mov     $0x80000000,%edi    # current max = INT_MIN
start_loop:
    cmp     %ecx, %esi
    jae     end_loop            # end() ptr reached (esi >= ecx)
    lodsl                       # eax = [esi+=4]
    cmp     %edi, %eax          # check if it is new max
    cmovg   %eax, %edi          # update max as needed (eax > edi)
    jmp     start_loop          # go through whole array
end_loop:
    mov     %edi, %eax          # eax = current_max
    ret

( CMOVcc requires i686+ target architecture) CMOVcc需要i686 +目标架构)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM