2

I have the code:

global _start

section .text
_start:
        mov ebx,1
        mov ecx,4
label:
        add ebx,ebx
        dec ecx
        cmp ecx,0
        jg label
        mov eax,1
        int 0x80

It returns 16, which indicates that the code in 'label' was run. However, I did not tell it to jump to label. Does the code automatically jump to the next label if _start is finished?

ecm
  • 2,583
  • 4
  • 21
  • 29
Tis TIller
  • 23
  • 3
  • 1
    `_start` isn't a function per-se (it's not called so there's no return address on the stack), but the duplicates explain that labels don't affect the generated machine code; execution just falls through them. An asm label is like a C label for a goto, not like a function scope. – Peter Cordes Dec 18 '19 at 02:50

1 Answers1

4

Lacking a return or jump instruction or a process termination service call, yes, execution does "fall through" to whatever comes next in memory after the end of your _start code. In this case, that was the code at your label named label.

If there was data after the end of the code, or an unmapped page, the processor would have tried to execute that. Which would cause an infinite loop or crash or hang, most likely.

ecm
  • 2,583
  • 4
  • 21
  • 29