2

I'm developing on Mac OS X (FreeBSD)

here is the code

section .data
v dd 72,54, 89, 21, 0, 12
n equ 6

section .bss
max resd 1
section .text
global _start
_start:
    mov eax, 0
    mov ebx, [v]
    mov cx, n
    dec cx
    change_max:
        mov ebx, [v+eax*4]
        loop lp
    lp:
        inc eax
        cmp ebx, [v+eax*4]
        jl change_max
        loop lp
    return_max:
            mov [max], ebx
    exit:
        push dword 0             
        mov eax, 0x1             
        sub esp, 4
        int 0x80

I get a Segmentation fault:11 and I can't understand why, any clue?

1 Answer 1

2

Looks like there is no way for your program to terminate as you don't decrement the value of cx(which initially holds the array size) and stop the comparisons when it becomes 0. Instead you keep looping and keep doing:

inc eax
cmp ebx, [v+eax*4]

which leads to segmentation fault because at some point you try to access past the array end, a memory which belongs to a segment which you don't own.

Sign up to request clarification or add additional context in comments.

2 Comments

Another problem is that LOOP will use ECX, the entire 32 bits, not just the low 16 bits of CX and it doesn't seem like the provided code clears the top 16 bits of ECX.
the loop command decrease automatically the value in ecx, so my mistake was to use cx, instead of ecx. this is the correction I've made: mov ecx, n. Now it works, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.