[英]NASM Segmentation fault (core dumped)
所以,我想創建一個允許用戶輸入矩陣 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
你有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
也犯了同樣的錯誤。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.