The address of mesg
can vary depending on how your program is laid out in memory.
The following will hard-code a specific address and will not work reliably (or at all):
mov ecx, mesg
For reference, the first approach hard-codes the following address:
mov ecx, 0x804807d
The second approach does work because it figures out the address of mesg
at runtime, using the return address of a call
instruction.
Put another way, the first version only works if loaded at a specific address whereas the second is position-independent.
It is worth noting that the jmp
and the call
instructions that appear in the second version use relative addressing, meaning that the opcodes specify the distance to the target rather than the address of the target. This makes these instructions work regardless of where they are placed in memory.
If you examine the opcodes, you'll see that the jmp
is encoded as
e9 19 00 00 00
(i.e. jump 0x19, or 2510, bytes forward), and the call
is encoded as
e8 e2 ff ff ff
where 0xffffffe2
is a small negative number (-30).