【问题标题】:NASM Segmentation fault (core dumped)NASM 分段错误(核心转储)
【发布时间】:2020-05-03 23:41:55
【问题描述】:

所以,我想创建一个允许用户输入矩阵 4x5 的程序。我声明了指向特定行的“指针”和指向这些指针的“指针”,以便使用一个指针访问每个字符串。我创建了字符串,因为我想检查这些字符串中的符号(用户只能输入数字和 + 或 - 作为第一个符号)。因此,当我检查第一个符号(这个应该是 + 或 -)并且如果用户没有输入 + 或 - 我看到分段错误(核心转储)时,我遇到了问题。我使用 Ubuntu。 (英语不是我的母语,如果我犯了错误,请不要担心))。

; Task: write a program that allows to enter 2-D array 4x5. Show only 2 first columns.
;   # # # # #  
;   # # # # #
;   # # # # #
;   # # # # # 


section .bss ; The section intended for uninitilizated data
    string_00 resb 7
    string_01 resb 7
    string_02 resb 7
    string_03 resb 7
    string_04 resb 7

    string_10 resb 7
    string_11 resb 7
    string_12 resb 7
    string_13 resb 7
    string_14 resb 7

    string_20 resb 7
    string_21 resb 7
    string_22 resb 7
    string_23 resb 7
    string_24 resb 7

    string_30 resb 7
    string_31 resb 7
    string_32 resb 7
    string_33 resb 7
    string_34 resb 7

section .data ; The section designed for using initilizated data
    GreenColor db 0x1b, '[32m' ; The string that can change text color to green
    lenGreenColor equ $ - GreenColor ; The lenth of this string

    RedColor db 0x1b, '[31m' ; The string that can change text color to red (used if user will enter incorrect data)
    lenRedColor equ $ - RedColor ; The lenth of this string

    Task db 'This program allows you to enter matrix 4x5 and show only 2 first colums.', 0xa ; Just a string that shows my task
    lenTask equ $ - Task ; The lenth of this string

    Warning db 'Enter 16-bit numbers (from -32767 to +32768) and enter the sign of number, please.', 0xa ; Warning, I chose 16-bit number because the bigger
    lenWarning equ $ - Warning ; The lenth of this string                                                ; numbers will be processed in the same way, but long
                                                                                                         ; numbers are not necessary    

    ErrorSign db 'You should use the sign of the number ( + or - )!', 0xa ; An error message to user if he did not use the number sign
    lenErrorSing equ $ - ErrorSign ; THe lenth of this message

    pointer_0 dd string_00, string_01, string_02, string_03, string_04
    pointer_1 dd string_10, string_11, string_12, string_13, string_14
    pointer_2 dd string_20, string_21, string_22, string_23, string_24
    pointer_3 dd string_30, string_31, string_32, string_33, string_34

    pointer dd pointer_0, pointer_1, pointer_2, pointer_3

    external_counter db 0
    internal_counter db 0

section .text
    global _start
_start:
    mov edx, lenGreenColor
    mov ecx, GreenColor
    call _print

    mov edx, lenTask
    mov ecx, Task
    call _print

    mov edx, lenWarning
    mov ecx, Warning
    call _print

    call _check



    mov ebx, 0
    mov eax, 1
    int 0x80

_print:
    mov ebx, 1
    mov eax, 4
    int 0x80
    ret

_enter:
    mov edx, 7
    mov ebx, 0
    mov eax, 3
    int 0x80
    ret

_check:
    external_loop:
        mov eax, [pointer]
        mov ebx, [external_counter]
        mov esi, [eax + ebx * 4]

            internal_loop:
                mov ebx, [internal_counter]

                add ebx, ebx
                add ebx, ebx
                add ebx, ebx

                add esi, ebx
                mov ecx, esi
                call _enter

                cmp [esi], byte 43
                je norm_sign
                cmp [esi], byte 45
                je norm_sign


                xor esi, esi
                xor eax, eax
                xor ebx, ebx
                xor ecx, ecx
                xor edx, edx


                jmp _check
                    norm_sign:
                        ret

【问题讨论】:

  • 第一次尝试我可以输入数字,如果我输入的数字没有 + 或 - 程序必须转到函数的开头并发出相同的命令,但这是一个错误
  • 您发布的代码似乎不完整。它从不调用_enter,它从不循环到标签external_loop 和internal_loop。我建议使用从_check 返回的进位标志来表示输入错误,并充分分支主线程。

标签: assembly x86-64 nasm


【解决方案1】:

您有mov ebx, [external_counter],但也有external_counter db 0。由于ebx 是一个32 位寄存器,它将从external_counter 中读取4 个字节。问题是external_counter 只有 1 个字节长。这意味着您要将 3 个垃圾字节拉入ebx,当您执行mov esi, [eax + ebx * 4] 时,这将使您超出范围。请改用external_counter dd 0。 (看起来你对internal_counter 也犯了同样的错误。)

【讨论】:

  • 超级!谢谢!但是我不明白为什么我第一次输入数字时可以,但第二次输入错误?我已经使用 xor 来清除寄存器...
  • @Danny 当你的程序第一次启动时,“垃圾字节”可能是 0,所以事情是偶然发生的。在您阅读第一个条目后,它们已经习惯了某些东西,从而中断了下一次迭代。
  • @Danny:stackoverflow.com/tags/x86/info 有文档链接(包括 NASM 手册,但更重要的是英特尔的 x86 手册/指令集参考)。此外,请使用调试器,以便您自己查看问题所在(例如 EBX 持有巨大的价值)。
猜你喜欢
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2015-06-25
  • 2021-06-03
相关资源
最近更新 更多