Thanks Mettalknight - that makes it easier to try. I can confirm that it segfaults, and I had a hard time figuring out why.
You'll recall, over on SO, someone suggested we weren't loading ebx soon enough? I thought about that, and concluded that it would only be a problem in the case of an empty array - one including only the sentinel, and no data. I was incorrect (first time... not!).
What was happening, once array a was exhausted, we went to copyb. We stored ebx, incremented both indices, went back to merge... and went straight back to copyb - without ever loading a new ebx. We stored the same ol' ebx... etc. Eventually we incremented edx (index to array c) until it ran off the end of memory that we "own", and segfaulted! So we really do need to load ebx a little earlier, as suggested on SO - right after doing eax is fine. This was apparently an error in the book's code, not something you or I did.
There's a minor typo in the code you posted. In copyb, we check ebx for -1. You wrote "edx". That should never happen, so again we'd keep going until a segfault.
Those two changes should at least get you some output. I think you'll find that the merged array is not properly sorted - I really like "jg copyb" where we discussed. Also, it isn't printing the full array. These things can be easily fixed once you can see what you're doing.
This is my current version...
extern printf ; c call to print
segment .data
a dd 1,1,2,3,4,5,5,5,6,9,-1
b dd 2,3,3,4,6,7,8,8,9,9,-1
label db "c array= ",0 ; label for printing
fmt: db "%d ", 0 ;output format for printing
eol: db "",10,0 ; used for line feed
segment .bss
c resd 21
segment .text
global main ; changed due to gcc can't link to _start any longer because of high language library
main:
nop ; helps if using a debugger like ddd else comment out or delete
mov esi,0 ; esi indexes array a
mov edi,0 ; edi indexes array b
mov edx,0 ; edx indexes array c
merge:
mov eax,[a+4*esi] ; load a[i]
mov ebx,[b+4*edi] ; load b[j]
cmp eax,0 ; if array a exhausted then
jl copyb ; goto copyb
cmp ebx,0 ; if array b exhausted then
jl copya ; goto copya
cmp eax,ebx ; if a[i] < b[j] then
; jl copya
jg copyb
copya:
mov [c+4*edx],eax ; c[k] := a[i]
cmp eax,-1
je done
add esi,1
add edx,1
jmp merge
copyb:
mov [c+4*edx],ebx ; c[k] := b[j]
; cmp edx,-1
cmp ebx,-1
je done
add edi,1
add edx,1
jmp merge
done:
mov [c+4*edx],eax ; c[k] := -1
;**************code above this line***********
mov edi, edx
inc edi
push label ; print stirng
call printf ; used to call c library
add esp, 4 ;clean stack
; add EDI, 2 ; add to get all 21 numbers to print
mov esi, 0 ; indexing for c set to zero
printer:
push dword[c+4*esi] ; ARRAY c[i] to print
push dword fmt ; required by c printf function passing format
call printf ; call glibc print function
add esp, 8 ;clean up stack
inc esi ;increase the i index
dec edi ;decrease edi for jne
jne printer ; jumps if zf flag is not set
push dword eol ;add line feed
call printf ; call the c printf function from the library
add esp,4 ; clean stack
ret ; return control back to kernel prooperly used with gcc
I really don't know what to suggest for a "better" book.
Best,
Frank