First of all I want to start off by saying that I've read this, this and this question. Yet none of the answers provided on these questions were sufficient/had detailed enough information in order to answer my question. Moreover, they are all 4 - 6 years old which makes them outdated. With that being said, I opened a new question here.
I am trying to make a simple program that displays 1 - 4 matrices in Linux 32-bit assembly with NASM syntax, I've made a procedure that should print the simple 1x1 matrix.
section .data
msg1: db 'output:', 10
msg1len: equ $-msg1
endmsg: db 10
m1r1: db '5'
m2r1: db '1', '4'
m2r2: db '2', '6'
m3r1: db '8', '3', '4'
m3r2: db '9', '2', '1'
m3r3: db '1', '5', '6'
m4r1: db '6', '3', '1', '7'
m4r2: db '1', '9', '8', '4'
m4r3: db '5', '0', '1', '2'
m4r4: db '2', '7', '1', '0'
section .bss
output1: resb 5
output2: resb 7*2
output3: resb 9*3
output4: resb 11*4
section .text
global _start
_start:
mov eax, 1
call printMatrix
_exit:
mov eax, 0
mov ebx, 1
int 80h
;description:
; displays a visual representation of
; a matrix from size 1 through 4
;parameters:
; eax - matrix size
printMatrix:
push eax
push ebx
push ecx
push edx
push esi
cmp eax, 1
je .printMatrix1
cmp eax, 2
je .printMatrix2
cmp eax, 3
je .printMatrix3
cmp eax, 4
je .printMatrix4
.printMatrix1:
mov eax, '[ '
mov [output1], eax
mov eax, m1r1
mov [output1 + 2], eax
mov eax, ' '
mov [output1 + 3], eax
mov eax, ']'
mov [output1 + 4], eax
mov eax, 4
mov ebx, 1
mov ecx, [output1]
mov edx, 5
jmp .exit
.printMatrix2:
jmp .exit
.printMatrix3:
jmp .exit
.printMatrix4:
jmp .exit
.exit:
pop esi
pop edx
pop ecx
pop ebx
pop eax
ret
printEndl:
push eax
push ebx
push ecx
push edx
mov eax, 4
mov ebx, 1
mov ecx, endmsg
mov edx, 1
int 80h
pop edx
pop ecx
pop ebx
pop eax
ret
Yet when I compile via:
nasm -f elf32 matrix.asm
And link using:
ld -m elf_i386 -s -o matrix matrix.o
I get no errors/warnings whatsoever, but when I run the program using ./matrix
I get segmentation fault (core dumped)
error.
Now I must note that this question provided a rather well definition for what is a segmentation fault and what it's usually caused by, it was a bit unclear though. What I seek is:
- A proper explanation/definition of a segmentation in terms of memory and assembly.
- What have caused the fault in this particular case (According to the first and second links, I suspect it has something to do with the jumps or calling procedures and the stack. However I've been sitting here for hours trying to figure out what caused it with no success).
- How to avoid such a fault and recommended future practices.