从C运行x86 asm时的SIGSEGV

时间:2018-11-18 11:28:16

标签: c assembly x86 nasm

有人可以告诉我为什么在这里调用printf时会发生段错误吗?

32位NASM代码:

    global replaceDigitsWithAsterisk
    section .text

replaceDigitsWithAsterisk:      ;Arg: ptr to string
    push        ebp             ;Save last frame pointer
    mov         ebp, esp        ;Save current frame pointer
    mov         ebx, dword [ebp + 8]  ;Save address of string
                                ;[ebp + 4] return address, 
    push        eax             ;save address of string
replaceLoop:
    mov         al, byte[ebx]   ;store current char in al
    cmp         al, 0          ;compare if is equal zero
    je          replaceLoopEnd  ;if not null terminator continue

    cmp         al, '0'         ;compare with ASCII 0
    jl          increment
    cmp         al, '9'         ;compare with ASCII 9
    jg          increment         ;insert * if > 0

increment:
    add         ebx, 1          ;increment string pointer by 1 byte
    jmp         replaceLoop     ;unconditionally jump

replace:                        ;somehow cmovb and cmova are not working for my nasm ;__;
    mov         byte[ebx], '*'  
    jmp         increment

replaceLoopEnd:
    pop         eax             ;Restore address of result
    pop         ebp             ;Restore last frame pointer
    ret                         ;sets instruction pointer to return address

C代码:

#include <stdio.h>

char* replaceDigitsWithAsterisk(char* source);

int main()
{
    char s1[] = "Wind 99 the hill";
    char s2[] = "Litwo, 1234450 moja";
    char s3[] = "Tak jak 333678 to";
    char s4[] = "Ebe Ebe Ebe";
    char* result;
    result = replaceDigitsWithAsterisk(s1);
    printf("Result 1: %s\n", result);
    result = replaceDigitsWithAsterisk(s2);
    printf("Result 2: %s\n", result);
    result = replaceDigitsWithAsterisk(s3);
    printf("Result 3: %s\n", result);
    result = replaceDigitsWithAsterisk(s4);
    printf("Result 4: %s\n", result);
    return 0;
}

此外,似乎cmovb和cmova指令不适用于我的nasm(最新),并且在Windows上正常运行

0 个答案:

没有答案