使用asm和c的程序中printf的段错误

时间:2018-11-20 21:52:53

标签: c assembly segmentation-fault

我正在尝试使用C作为main和asm函数来获取最大元素的值及其在数组中的位置。这是我的C程序:

#include <stdio.h>
#include <stdlib.h>
unsigned int get_max(unsigned int *arr, unsigned int len, unsigned int *pos);

    int main(void)
    {
    unsigned int arr[] = { 19, 7, 129, 87, 54, 218, 67, 12, 19, 99 };
    unsigned int max;
    unsigned int pos;
    max = get_max(arr, 10, &pos);

    printf("max: %u\n index: %u\n", max, pos);

    return 0;
}

这是我在asm中编写的函数:

section .text

global get_max

get_max:
    push ebp
    mov ebp, esp

    ; [ebp+8] is array pointer
    ; [ebp+12] is array length
    ; [ebp+16] is address of max index
    mov ebx, [ebp+8]
    mov ecx, [ebp+12]
    mov edx, [ebp+16]
    xor eax, eax

compare:
    cmp eax, [ebx+ecx*4-4]
    jge check_end
    mov eax, [ebx+ecx*4-4]
    mov edx, ecx
check_end:
    loopnz compare

    leave
    ret

使用gdb似乎在printf时崩溃了,但是我不确定100%。

0 个答案:

没有答案