NASM分段错误问题

发布于 2024-10-27 18:28:07 字数 1551 浏览 3 评论 0原文

问候, 我正在Linux下的NASM上编写,我遇到以下问题:我必须编写一个简单的程序,其中用户必须输入字符串,将其传递给返回其大小的函数:

代码是:

%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

这是宏文件:

%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

我调试了程序,问题出在指令 cmp byte [ebx],0h 中,因为当我启动程序时,它在 str_input 之后打印出分段错误。我没有发现任何问题,但也许我的地址有误。

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:

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. I don't see anything wrong, but maybe I've mistake with the addressation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-11-03 18:28:07

在调用 func 之前尝试一下:

mov ebx, offset string

您似乎正在使用 intel 语法。在 AT&T 语法中 mov $string,%ebx 是正确的,因为常量总是立即的,但在 intel 中不会发生这种情况

Try this in your before calling func:

mov ebx, offset string

it seems you're using intel syntax. In AT&T syntax mov $string,%ebx would be correct because constants are always immediate, but this doesn't happen in intel's

汐鸠 2024-11-03 18:28:07

这变得很陈旧......但你的问题是“str_input”不输入以零结尾的字符串!

This is getting quite stale... but your problem is that "str_input" doesn't input a zero-terminated string!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文