简体   繁体   English

NASM分段故障问题

[英]NASM segmentation fault problem

Greetings, I'm writing on NASM under linux, and I have the following problem:I have to write a simple program, in which user must enter string, pass it to function which returns it's size: 问候,我在Linux下的NASM上编写,并且遇到以下问题:我必须编写一个简单的程序,用户必须在其中输入字符串,并将其传递给返回其大小的函数:

The code is: 代码是:

%include "macros.inc"
        section .data
prompt1     db "Enter string: ",0
prompt1len  equ $-prompt1
prompt2     db "The size of string is: ",0
prompt2len  equ $-prompt2

        section .bss
string      resb 20
        section .text
            global _start
_start:     str_output prompt1len,prompt1
            str_input string 
            mov ebx,string
            call func
            str_output prompt2len,prompt2
            output eax
            exit 0

func:
            xor eax,eax
et        **cmp byte [ebx],0h**
            je end
            inc eax
            inc ebx
            jmp et
end         dec eax
            ret

And here is the macro file: 这是宏文件:

%macro exit 1
        mov eax,1
        mov ebx,%1
        int 80h
%endmacro

%macro int_input 1
        mov eax,3
        mov ebx,0
        mov ecx,%1
        mov edx,1
        int 80h
        and eax,0Fh
        mov %1,eax
%endmacro

%macro str_input 1
        mov eax,3
        mov ebx,1
        mov ecx,%1
        mov edx,20
        int 80h
%    endmacro

%macro output 1
        mov eax,%1
        or eax,30h
        mov %1,eax
        mov eax,4
        mov ebx,1
        mov ecx,%1
        mov edx,1
        int 80h
%endmacro

%macro str_output 2+
        mov eax,4
        mov ebx,1
        mov ecx,%2
        mov edx,%1
        int 80h
%endmacro

I debugged the program and the problem is in instruction cmp byte [ebx],0h ,because when I start the program it prints out segmentation fault, after str_input. 我调试了程序,问题出在指令cmp字节[ebx],0h中,因为当我启动程序时,它在str_input之后打印出分段错误。 I don't see anything wrong, but maybe I've mistake with the addressation. 我没有发现任何错误,但也许我对地址有误。

这已经很陈旧了……但是您的问题是“ str_input”没有输入以零结尾的字符串!

Try this in your before calling func : 致电func之前,请尝试以下方法:

mov ebx, offset string

it seems you're using intel syntax. 看来您使用的是Intel语法。 In AT&T syntax mov $string,%ebx would be correct because constants are always immediate, but this doesn't happen in intel's 在AT&T语法mov $string,%ebx是正确的,因为常量始终是立即数,但这在intel的中不会发生

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM