删除NASM x86组件中的空间问题

时间:2019-06-19 13:03:20

标签: assembly x86 nasm

我正在研究一个将从给定字符串中删除空格的代码。但是我遇到了细分错误。

section .text
extern printf
global  remove_spaces

remove_spaces:
    push ebp
    mov ebp, esp

    mov eax, [esp+8]
    mov edx, eax
rs_loop:    
    mov cl, [edx]
    cmp cl, 0
    je  rs_exit

    inc edx

    cmp cl, ' '
    je  rs_loop

    mov [eax], cl
    inc eax 
    jmp rs_loop

rs_exit:
    push DWORD [esp + 8]
    push DWORD format2
    call printf

exit:
    mov esp, ebp
    pop ebp
    ret


section .data       
    format:         DB "** a=%s\ **",   00ah,       0
    format2:        DB "d* a=%d\ *d",   00ah 
    format3:        DB "type is:%d ",   00ah,       0   

我在eax中有我的字符串地址。 然后,我将其复制到edx,并开始遍历它。 如果character不是空格,我尝试将其添加到eax的字符串中。但是,当我将此行添加到代码中时:

 mov [eax], bl

我遇到细分错误。

这是我的c代码:

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

extern  char* remove_spaces(char *a);

    int main(){
      char *a = "Remove spaces  . ";
      char *spaces_removed = remove_spaces(a);
      //printf("\n%s", spaces_removed);
      return 0;
    }

我检查了我的字符串是否正确。我想知道我是否错过了使用说明      mov [eax],bl 因为直到那时一切都很好。 我该怎么办? PS:问题和代码按照@Jester的建议进行编辑。

这是我的make文件和bash脚本:

CC=g++
ASMBIN=nasm

all : asm cc link
asm : 
    $(ASMBIN) -o food.o -f elf -g -l remove_spaces.lst remove_spaces.asm
cc :
    $(CC) -m32 -c -g -O0 main.cpp &> errors.txt
link :
    $(CC) -m32 -g -o test main.o remove_spaces.o
clean :
    rm *.o
    rm test
    rm errors.txt   
    rm remove_spaces.lst

rm -f main
rm -f remove_spaces.o
nasm -f elf -o remove_spaces.o remove_spaces.asm
gcc -m32 -o main main.c remove_spaces.o
./main

0 个答案:

没有答案
相关问题