簡體   English   中英

NASM分段故障問題

[英]NASM segmentation fault problem

問候,我在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字節[ebx],0h中,因為當我啟動程序時,它在str_input之后打印出分段錯誤。 我沒有發現任何錯誤,但也許我對地址有誤。

這已經很陳舊了……但是您的問題是“ str_input”沒有輸入以零結尾的字符串!

致電func之前,請嘗試以下方法:

mov ebx, offset string

看來您使用的是Intel語法。 在AT&T語法mov $string,%ebx是正確的,因為常量始終是立即數,但這在intel的中不會發生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM