c - 汇编返回 int 到 C 函数段错误

标签 c assembly segmentation-fault

我正在完成一个汇编程序,该程序用给定的替换字符替换字符串中的字符。汇编代码调用 C 函数,而汇编程序本身是从我的 .c 文件中的 main 调用的。但是,当尝试完成并从汇编程序返回最终 int 值到 C 时,我遇到了段错误。我的.asm文件如下:

; File: strrepl.asm
; Implements a C function with the prototype:
;
;   int strrepl(char *str, int c, int (* isinsubset) (int c) ) ;
;
; 
; Result: chars in string are replaced with the replacement character and string is returned.

    SECTION .text
    global  strrepl


_strrepl:   nop
strrepl:
    push    ebp         ; set up stack frame
    mov ebp, esp

    push    esi         ; save registers
    push    ebx
    xor eax, eax
    mov ecx, [ebp + 8]      ;load string (char array) into ecx
    jecxz   end         ;jump if [ecx] is zero
    mov al, [ebp + 12]      ;move the replacement character into esi
    mov edx, [ebp + 16]     ;move function pointer into edx

firstLoop:

    xor eax, eax

    mov edi, [ecx]
    cmp edi, 0
    jz  end

    mov edi, ecx        ; save array


    movzx   eax, byte [ecx]     ;load single byte into eax  
    push    eax         ; parameter for (*isinsubset)
    mov edx, [ebp + 16]         
    call    edx         ; execute (*isinsubset)


    mov ecx, edi        ; restore array
    cmp eax, 0
    jne secondLoop  
    add esp, 4          ; "pop off" the parameter
    mov ebx, eax        ; store return value
    add ecx, 1
    jmp firstLoop

secondLoop:
    mov eax, [ebp+12]
    mov [ecx], al
    mov edx, [ebp+16]
    add esp, 4
    mov ebx, eax
    add ecx, 1
    jmp     firstLoop

end:
    pop ebx         ; restore registers
    pop esi
    mov esp, ebp        ; take down stack frame
    pop ebp
    mov eax, 9
    push    eax         ;test
    ret

我的 c 文件是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

//display *((char *) $edi)
// These functions will be implemented in assembly:
//

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


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 ;
}

int main(){
    char *str1;
    int r;


    str1 = strdup("ABC 123 779 Hello World") ;
    r = strrepl(str1, '#', &isdigit) ;
    printf("str1 = \"%s\"\n", str1) ;
    printf("%d chararcters were replaced\n", r) ;
    free(str1) ;
    return 0;
}

在我的汇编代码中,你可以看到最后

mov eax, 9
push    eax 

我只是想将值 9 返回到值“r”,它是 C 文件中的 int 值。这只是一个测试,看看我是否可以将 int 返回到 c 文件中的 r 。最终我会将被替换的字符数返回给 r。但是,我需要弄清楚为什么上面的以下代码会出现段错误。有任何想法吗?

最佳答案

mov     eax, 9
push    eax          ; NOT a good idea
ret

这是一个错误。它将根据堆栈上最低的内容返回,而您刚刚将一些内容推送到堆栈上,这几乎肯定不是有效的返回地址。

大多数函数只需将代码放入 eax 中即可返回代码(这当然取决于调用约定,但这是很常见的),通常不需要将其插入堆栈,并且这样做肯定有很多缺点。

关于c - 汇编返回 int 到 C 函数段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435888/

相关文章:

c - FreeRTOS:osDelay 与 HAL_delay

linux - 就其第三个参数而言,mprotect() 作为 ASM 系统调用的用法是什么样的?

gcc - Libffi 可以为 Cortex-M3 构建吗?

C++:即使被转换的对象不是 NULL,dynamic_cast 也会导致 SEGFAULT。怎么会这样?

c - 在 C/Linux 中,您如何从系统中找到程序内存/分段的界限?

c - 为什么不会出现 5x5 乘法数组?

C:建立一个字节

c - 无法解释的段错误

c - scanf 和 char 变量不会很长

c - 有没有8051汇编语言到C代码的转换器