Why is no value returned if a function does not explicity use 'ret'

前端 未结 2 2003
离开以前
离开以前 2020-12-07 04:00

I have the following program:

SECTION .text
main:
     mov ebx, 10
     mov ecx, 50

repeat:
     inc ebx
     loop repeat

     mov eax, ebx
     ret
         


        
2条回答
  •  难免孤独
    2020-12-07 04:38

    When you leave off the "ret", the computer executes the last "move eax, ebx" and then executes whatever happens to be next in computer memory.

    I'm surprised you don't get an illegal instruction/access; that would be the most common response. Somehow the garbage instruction acts like a return, after trashing the registers.

    Its also a little unclear what you mean by "returns 60". You mean as a value to the command prompt? It is clear that your program has no defense against illegal instruction traps. What Windows does when you get such a trap without a defense is unclear to me; I know from experience when I do that Windows tends to just terminate my process and I get some random exit status. "0" might be such a status.

    Try adding:

          mov   byte ptr[eax], 0
    

    before the "ret" instruction; this will cause an illegal memory reference. You report what status you get. It wouldn't suprise me if you got the zero status result in this case.

提交回复
热议问题