I am able to assembly, but when I step through with gdb, it crashes when I try to mov the value in the array to the register
I am assembling with nasm -f elf32 test.asm
And I am linking with ld -m elf_i386 test.o
section data
list:
dw 7, 9, 3, 0
section text
global _start
_start:
push ebp ; prologue
mov ebp, esp ; prologue
mov edx, 1 ; i = 1
mov ecx, [edx * 4 + list] ; ecx = list[i]
loop:
mov ebx, [edx * 4 + list]
cmp ebx,0 ; if (list[i] = 0)
jz end ; exit loop
cmp ecx,ebx ; ecx - list[i]
jl if_greater ; ecx - list[i] = <0
inc edx ; i++
jmp loop
if_greater: ; if condition block
mov ecx, [edx * 4 + list] ; new highest value
inc edx ; i++
jmp loop
end:
mov esp, ebp ; epilogue
pop ebp ; epilogue
I have added updated code. I left the original code. My goal now is to find the highest pair in list, but am unsuccessful.
section data
list:
dw 7, 9, 3, 0
section text
global _start
_start:
push ebp ; prologue
mov ebp, esp ; prologue
mov edx, 1 ; i = 1
mov ecx, [edx * 4 + list] ; ecx = list[i]
loop:
mov ebx, [edx * 4 + list]
cmp ebx,0 ; if (list[i] = 0)
jz end ; exit loop
cmp ecx,ebx ; ecx - list[i]
jl if_greater ; ecx - list[i] = <0
inc edx ; i++
jmp loop
if_greater: ; if condition block
mov ecx, [edx * 4 + list] ; new highest value
inc edx ; i++
jmp loop
end:
mov eax, 1
xor ebx, ebx
int 0x80
mov esp, ebp ; epilogue
pop ebp ; epilogue
update 2: I have deleted both the prologue and the epilogue. I also changed edx to 0
section data
list:
dd 7, 9, 3, 0
section text
global _start
_start:
mov edx, 0 ; i = 0
mov ecx, [edx * 4 + list] ; ecx = list[i]
loop:
mov ebx, [edx * 4 + list]
cmp ebx,0 ; if (list[i] = 0)
jz end ; exit loop
cmp ecx,ebx ; ecx - list[i]
jl if_greater ; ecx - list[i] = <0
inc edx ; i++
jmp loop
if_greater: ; if condition block
mov ecx, [edx * 4 + list] ; new highest value
inc edx ; i++
jmp loop
end:
mov eax, 1
xor ebx, ebx
int 0x80