X86 Assembly Printing Max in Array returns Segmentation fault (core dumped)









up vote
1
down vote

favorite












My program works, but there is something wrong with my printMax function. The program terminates with a




Segmentation fault (core dumped).




I have tried building a stack for the function and just doing a pusha popa and both ways, I get the seg fault core dumped.



I've tried calling the function, but it just runs twice.

Any idea what I am doing wrong?



SECTION .data ;data section
msg1 : db "Here are the array elements:", 10, 0
msg1Len: equ $-msg1
msg2 : db "Here is the max value in the array:", 10, 0
msg2Len: equ $-msg2

arr : dd 2,4,6,8,10,20,40
arrLen : equ ($-arr)/4 ;number of elements = array length / 4

SECTION .bss
max resd 1 ;declare and reserve space for max

SECTION .text
global main
main:
push ebp
mov ebp, esp

mov ecx, msg1 ;print msg1
mov edx, msg1Len
call PString

;save array base address in ebx and save sizein in ecx
mov ebx, arr
mov ecx, arrLen; store num elements in ecx
;loop to print array
PrintArray:
mov eax, [ebx] ;move value [ebx] to eax
call PrintDec
call Println
add ebx, 4
loop PrintArray

printMax:
section .text
pusha
;reset array to find max
mov ebx, arr
mov ecx, arrLen

loopForMax:

mov eax, [ebx]
cmp eax, [ebx +4]
jle sameMax
mov [max], eax

sameMax:
add ebx, 4 ;move to next element
loop loopForMax

mov ecx, msg2
mov edx, msg2Len
call PString

mov eax, [max]
call PrintDec
call Println

popa
ret

;exit program and clean stack
mov esp, ebp
pop ebp
ret

PString:; save register values of the called function
pusha
mov eax,4 ; use 'write' system call = 4
mov ebx,1 ; file descriptor 1 = STDOUT
int 80h ; call the kernel

; restore the old register values of the called function
popa
ret

Println:
;will call PString func
;will change content of ecx and edx
;need to save registers used by the main program
section .data
nl db 10
section .text
pusha

mov ecx, nl
mov edx, 1
call PString

;return original register values
popa
ret

PrintDec:
;saves all registers so they return unmodified
;build the function to handle dword size

section .bss
decstr resb 10 ; 10 32-bit digits
ct1 resd 1 ;keep track of dec-string size

section .text
pusha; save registers

mov dword[ct1],0 ;initially assume 0
mov edi, decstr ; edi points to dec-string
add edi, 9 ; moved to the last element of string
xor edx, edx ; clear edx for 64-bit div
whileNotZero:
mov ebx, 10 ; get ready to divide by 10
div ebx ; divide by 10
add edx, '0' ; convert to ascii
mov byte[edi], dl ; put it in string
dec edi ; move to next char in str
inc dword[ct1] ; inc char counter
xor edx, edx ; clear edx
cmp eax, 0 ;is remainder 0?
jne whileNotZero ;if no, keep on looping

inc edi ; conversion finished, bring edi
mov ecx, edi ; back to start of string. make ecx
mov edx, [ct1] ; point to counterm edx gets # chars
mov eax, 4 ; print to stdout
mov ebx, 1
int 0x80 ; call kernel

popa ; restore registers
ret









share|improve this question



















  • 3




    For starters, your ;exit program and clean stack block is in the wrong place, it should be before printMax. As for the fault, use a debugger to at least pinpoint the faulting instruction but ideally to also find out the reason yourself.
    – Jester
    Nov 9 at 23:45






  • 1




    calling printMax and then ;exit program and clean code stack fixed the bug. Thanks.
    – efuddy
    Nov 10 at 0:29














up vote
1
down vote

favorite












My program works, but there is something wrong with my printMax function. The program terminates with a




Segmentation fault (core dumped).




I have tried building a stack for the function and just doing a pusha popa and both ways, I get the seg fault core dumped.



I've tried calling the function, but it just runs twice.

Any idea what I am doing wrong?



SECTION .data ;data section
msg1 : db "Here are the array elements:", 10, 0
msg1Len: equ $-msg1
msg2 : db "Here is the max value in the array:", 10, 0
msg2Len: equ $-msg2

arr : dd 2,4,6,8,10,20,40
arrLen : equ ($-arr)/4 ;number of elements = array length / 4

SECTION .bss
max resd 1 ;declare and reserve space for max

SECTION .text
global main
main:
push ebp
mov ebp, esp

mov ecx, msg1 ;print msg1
mov edx, msg1Len
call PString

;save array base address in ebx and save sizein in ecx
mov ebx, arr
mov ecx, arrLen; store num elements in ecx
;loop to print array
PrintArray:
mov eax, [ebx] ;move value [ebx] to eax
call PrintDec
call Println
add ebx, 4
loop PrintArray

printMax:
section .text
pusha
;reset array to find max
mov ebx, arr
mov ecx, arrLen

loopForMax:

mov eax, [ebx]
cmp eax, [ebx +4]
jle sameMax
mov [max], eax

sameMax:
add ebx, 4 ;move to next element
loop loopForMax

mov ecx, msg2
mov edx, msg2Len
call PString

mov eax, [max]
call PrintDec
call Println

popa
ret

;exit program and clean stack
mov esp, ebp
pop ebp
ret

PString:; save register values of the called function
pusha
mov eax,4 ; use 'write' system call = 4
mov ebx,1 ; file descriptor 1 = STDOUT
int 80h ; call the kernel

; restore the old register values of the called function
popa
ret

Println:
;will call PString func
;will change content of ecx and edx
;need to save registers used by the main program
section .data
nl db 10
section .text
pusha

mov ecx, nl
mov edx, 1
call PString

;return original register values
popa
ret

PrintDec:
;saves all registers so they return unmodified
;build the function to handle dword size

section .bss
decstr resb 10 ; 10 32-bit digits
ct1 resd 1 ;keep track of dec-string size

section .text
pusha; save registers

mov dword[ct1],0 ;initially assume 0
mov edi, decstr ; edi points to dec-string
add edi, 9 ; moved to the last element of string
xor edx, edx ; clear edx for 64-bit div
whileNotZero:
mov ebx, 10 ; get ready to divide by 10
div ebx ; divide by 10
add edx, '0' ; convert to ascii
mov byte[edi], dl ; put it in string
dec edi ; move to next char in str
inc dword[ct1] ; inc char counter
xor edx, edx ; clear edx
cmp eax, 0 ;is remainder 0?
jne whileNotZero ;if no, keep on looping

inc edi ; conversion finished, bring edi
mov ecx, edi ; back to start of string. make ecx
mov edx, [ct1] ; point to counterm edx gets # chars
mov eax, 4 ; print to stdout
mov ebx, 1
int 0x80 ; call kernel

popa ; restore registers
ret









share|improve this question



















  • 3




    For starters, your ;exit program and clean stack block is in the wrong place, it should be before printMax. As for the fault, use a debugger to at least pinpoint the faulting instruction but ideally to also find out the reason yourself.
    – Jester
    Nov 9 at 23:45






  • 1




    calling printMax and then ;exit program and clean code stack fixed the bug. Thanks.
    – efuddy
    Nov 10 at 0:29












up vote
1
down vote

favorite









up vote
1
down vote

favorite











My program works, but there is something wrong with my printMax function. The program terminates with a




Segmentation fault (core dumped).




I have tried building a stack for the function and just doing a pusha popa and both ways, I get the seg fault core dumped.



I've tried calling the function, but it just runs twice.

Any idea what I am doing wrong?



SECTION .data ;data section
msg1 : db "Here are the array elements:", 10, 0
msg1Len: equ $-msg1
msg2 : db "Here is the max value in the array:", 10, 0
msg2Len: equ $-msg2

arr : dd 2,4,6,8,10,20,40
arrLen : equ ($-arr)/4 ;number of elements = array length / 4

SECTION .bss
max resd 1 ;declare and reserve space for max

SECTION .text
global main
main:
push ebp
mov ebp, esp

mov ecx, msg1 ;print msg1
mov edx, msg1Len
call PString

;save array base address in ebx and save sizein in ecx
mov ebx, arr
mov ecx, arrLen; store num elements in ecx
;loop to print array
PrintArray:
mov eax, [ebx] ;move value [ebx] to eax
call PrintDec
call Println
add ebx, 4
loop PrintArray

printMax:
section .text
pusha
;reset array to find max
mov ebx, arr
mov ecx, arrLen

loopForMax:

mov eax, [ebx]
cmp eax, [ebx +4]
jle sameMax
mov [max], eax

sameMax:
add ebx, 4 ;move to next element
loop loopForMax

mov ecx, msg2
mov edx, msg2Len
call PString

mov eax, [max]
call PrintDec
call Println

popa
ret

;exit program and clean stack
mov esp, ebp
pop ebp
ret

PString:; save register values of the called function
pusha
mov eax,4 ; use 'write' system call = 4
mov ebx,1 ; file descriptor 1 = STDOUT
int 80h ; call the kernel

; restore the old register values of the called function
popa
ret

Println:
;will call PString func
;will change content of ecx and edx
;need to save registers used by the main program
section .data
nl db 10
section .text
pusha

mov ecx, nl
mov edx, 1
call PString

;return original register values
popa
ret

PrintDec:
;saves all registers so they return unmodified
;build the function to handle dword size

section .bss
decstr resb 10 ; 10 32-bit digits
ct1 resd 1 ;keep track of dec-string size

section .text
pusha; save registers

mov dword[ct1],0 ;initially assume 0
mov edi, decstr ; edi points to dec-string
add edi, 9 ; moved to the last element of string
xor edx, edx ; clear edx for 64-bit div
whileNotZero:
mov ebx, 10 ; get ready to divide by 10
div ebx ; divide by 10
add edx, '0' ; convert to ascii
mov byte[edi], dl ; put it in string
dec edi ; move to next char in str
inc dword[ct1] ; inc char counter
xor edx, edx ; clear edx
cmp eax, 0 ;is remainder 0?
jne whileNotZero ;if no, keep on looping

inc edi ; conversion finished, bring edi
mov ecx, edi ; back to start of string. make ecx
mov edx, [ct1] ; point to counterm edx gets # chars
mov eax, 4 ; print to stdout
mov ebx, 1
int 0x80 ; call kernel

popa ; restore registers
ret









share|improve this question















My program works, but there is something wrong with my printMax function. The program terminates with a




Segmentation fault (core dumped).




I have tried building a stack for the function and just doing a pusha popa and both ways, I get the seg fault core dumped.



I've tried calling the function, but it just runs twice.

Any idea what I am doing wrong?



SECTION .data ;data section
msg1 : db "Here are the array elements:", 10, 0
msg1Len: equ $-msg1
msg2 : db "Here is the max value in the array:", 10, 0
msg2Len: equ $-msg2

arr : dd 2,4,6,8,10,20,40
arrLen : equ ($-arr)/4 ;number of elements = array length / 4

SECTION .bss
max resd 1 ;declare and reserve space for max

SECTION .text
global main
main:
push ebp
mov ebp, esp

mov ecx, msg1 ;print msg1
mov edx, msg1Len
call PString

;save array base address in ebx and save sizein in ecx
mov ebx, arr
mov ecx, arrLen; store num elements in ecx
;loop to print array
PrintArray:
mov eax, [ebx] ;move value [ebx] to eax
call PrintDec
call Println
add ebx, 4
loop PrintArray

printMax:
section .text
pusha
;reset array to find max
mov ebx, arr
mov ecx, arrLen

loopForMax:

mov eax, [ebx]
cmp eax, [ebx +4]
jle sameMax
mov [max], eax

sameMax:
add ebx, 4 ;move to next element
loop loopForMax

mov ecx, msg2
mov edx, msg2Len
call PString

mov eax, [max]
call PrintDec
call Println

popa
ret

;exit program and clean stack
mov esp, ebp
pop ebp
ret

PString:; save register values of the called function
pusha
mov eax,4 ; use 'write' system call = 4
mov ebx,1 ; file descriptor 1 = STDOUT
int 80h ; call the kernel

; restore the old register values of the called function
popa
ret

Println:
;will call PString func
;will change content of ecx and edx
;need to save registers used by the main program
section .data
nl db 10
section .text
pusha

mov ecx, nl
mov edx, 1
call PString

;return original register values
popa
ret

PrintDec:
;saves all registers so they return unmodified
;build the function to handle dword size

section .bss
decstr resb 10 ; 10 32-bit digits
ct1 resd 1 ;keep track of dec-string size

section .text
pusha; save registers

mov dword[ct1],0 ;initially assume 0
mov edi, decstr ; edi points to dec-string
add edi, 9 ; moved to the last element of string
xor edx, edx ; clear edx for 64-bit div
whileNotZero:
mov ebx, 10 ; get ready to divide by 10
div ebx ; divide by 10
add edx, '0' ; convert to ascii
mov byte[edi], dl ; put it in string
dec edi ; move to next char in str
inc dword[ct1] ; inc char counter
xor edx, edx ; clear edx
cmp eax, 0 ;is remainder 0?
jne whileNotZero ;if no, keep on looping

inc edi ; conversion finished, bring edi
mov ecx, edi ; back to start of string. make ecx
mov edx, [ct1] ; point to counterm edx gets # chars
mov eax, 4 ; print to stdout
mov ebx, 1
int 0x80 ; call kernel

popa ; restore registers
ret






arrays assembly x86 segmentation-fault coredump






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 23:37









zx485

12.9k122845




12.9k122845










asked Nov 9 at 23:26









efuddy

365




365







  • 3




    For starters, your ;exit program and clean stack block is in the wrong place, it should be before printMax. As for the fault, use a debugger to at least pinpoint the faulting instruction but ideally to also find out the reason yourself.
    – Jester
    Nov 9 at 23:45






  • 1




    calling printMax and then ;exit program and clean code stack fixed the bug. Thanks.
    – efuddy
    Nov 10 at 0:29












  • 3




    For starters, your ;exit program and clean stack block is in the wrong place, it should be before printMax. As for the fault, use a debugger to at least pinpoint the faulting instruction but ideally to also find out the reason yourself.
    – Jester
    Nov 9 at 23:45






  • 1




    calling printMax and then ;exit program and clean code stack fixed the bug. Thanks.
    – efuddy
    Nov 10 at 0:29







3




3




For starters, your ;exit program and clean stack block is in the wrong place, it should be before printMax. As for the fault, use a debugger to at least pinpoint the faulting instruction but ideally to also find out the reason yourself.
– Jester
Nov 9 at 23:45




For starters, your ;exit program and clean stack block is in the wrong place, it should be before printMax. As for the fault, use a debugger to at least pinpoint the faulting instruction but ideally to also find out the reason yourself.
– Jester
Nov 9 at 23:45




1




1




calling printMax and then ;exit program and clean code stack fixed the bug. Thanks.
– efuddy
Nov 10 at 0:29




calling printMax and then ;exit program and clean code stack fixed the bug. Thanks.
– efuddy
Nov 10 at 0:29

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234531%2fx86-assembly-printing-max-in-array-returns-segmentation-fault-core-dumped%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234531%2fx86-assembly-printing-max-in-array-returns-segmentation-fault-core-dumped%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo