I read a book, which give the next example:
There is a list, which each member has the next member’s adress in it four first bytes. The last member has the value of 0. It say that the next implementation is wrong, and I don’t understand why:
freeList
mov eax, [ebp+8]
cmp eax, 0
jne cont
ret
cont:
mov ebx, [eax]
mov [ebp+8], ebx
push eax
call free
pop eax
call freeList
(I don’t need the correct implementation, I have one. I just need to understand what wrong with this one)
Thanks.
The last line of the incorrect implementation is
call freeList. But when the (recursive) call completes, it will try to return to the non-existent code after that line. Appending aretinstruction would make the code work, but normally you try to avoid that except when debugging code in which case it sometimes helps to be able to see all the intermediate calls. Instead you can simplify thecall freeList; retto a simplejmp freeList.Example of debugging high-level code:
If you compile with optimisations, the compiler may want to write the recursive call to
freeList(next)by copingnexttolistand performing ajmp, however this would destroy the value oflist. This means that when you try to debug the function you can’t tell what elements of the list have already been freed. So you would want to disable this optimisation when you are trying to debug a problem in the function.