gpt4 book ai didi

从程序集调用 C 函数,段错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:47 24 4
gpt4 key购买 nike

我正在编写一个汇编函数来替换字符串中的字母,一个字符一个字符,如果传递给函数(由函数指针参数给定)的那个字符返回 1。例如,如果 isVowel(c) 返回对角色来说是真的。调用函数指针时出现段错误。我使用 ebx 作为字符串中的字符数以从中倒数。

使用 Linux、Nasm、x86

这是 c 中的汇编函数签名:

int strrepl(char *str, int c, int (* isinsubset) (int c) ) ;

如果我错了请纠正我,但是:

-The string pointer is at ebp+8.
-The character to replace with is in ebp+12 and takes up 4 bytes (16 bits).
-The function pointer is ebp + 28;

调用表单组件的函数:

int isvowel (int c) {

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return 1 ;

if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
return 1 ;

return 0 ;
}

这是我的实现

mov     edx, [ebp + 28]
mov esi, [ebp + 8]
mov eax, [esi + 4*ebx - 4]

之后 gdb 显示 eax = 0。然后,

push    eax
call edx

以上产生SEGFAULT

add     esp, 4

最佳答案

  • The string pointer is at ebp+8.
  • The character to replace with is in ebp+12 and takes up 4 bytes (16 bits).
  • The function pointer is ebp + 28;

按照您的约定,由于函数指针紧跟在字符之后,它的地址是 ebp + 16 而不是 28。地址是面向字节的而不是面向位的。

关于从程序集调用 C 函数,段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16020048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com