Could anyone help me to solve "zsh: bus error"

1.3k Views Asked by At

On mac OS(which has intel inside), I tried to make a simple x86 hybrid program with main module written in C and a function written in x86 assembly language (NASM assembler).

Then, the following function is to reverse the string of the argument.

My C code is

#include <stdio.h>
char *revstring(char *s);
int main(int argc, char* argv[]){
for (int i=1; i<argc; i++){
    printf("%s->", argv[i]);
    printf("%s\n", revstring(argv[i]));
    }
}

Then my assembly code

    section .text
    global _revstring
_revstring:
    push    rbp
    mov     rbp, rsp
    mov     rax, [rbp+8]
    mov     rcx, rax
_find_end:
    mov     dl, [rax]
    inc     rax
    test    dl, dl
    jnz     _find_end
    sub     rax, 2
    
_swap:
    cmp     rax, rcx
    jbe     _fin
    
    mov     dl, [rax]
    xchg    dl, [rcx]
    mov     [rax], dl
    dec     rax
    inc     rcx
    jmp     _swap

_fin:
    mov     rax, [rbp+8]
    pop     rbp
    ret

Or

    section .text
    global _revstring
_revstring:
    push    rbp
    mov     rbp, rsp
    mov     rax, [rbp+8]
    mov     rcx, rax
find_end:
    mov     dl, [rax]
    inc     rax
    test    dl, dl
    jnz     find_end
    sub     rax, 2
    
swap:
    cmp     rax, rcx
    jbe     fin
    
    mov     dl, [rax]
    xchg    dl, [rcx]
    mov     [rax], dl
    dec     rax
    inc     rcx
    jmp     swap

fin:
    mov     rax, [rbp+8]
    pop     rbp
    ret

Currnt MacOS cannot run 32 bit program, so I built the program by using these commands.

cc -m64 -std=c99 -c revs.c
nasm -f macho64 revstring.s
cc -m64 -o revs revs.o revstring.o

But When I enter

./revs abc123

the following error occured.

zsh: bus error  ./revs abc123 

I cannot find any solutions, so could anyone help me?

0

There are 0 best solutions below