I'm learning assembly using GNU Assembler using the Intel syntax and there is one think that I don't really understand. Say for example this code right here :
.intel_syntax noprefix
.data
string: .asciz "hello world"
.text
.global entry
.type entry, @function
entry:
mov byte ptr[string + 4], 'a'
mov eax, offset flat:string
ret
I get the idea to use offset flat: as we are writing relocatable code. But why don't we also specify offset flat:string
at his line : mov byte ptr[string + 4], 'a'
as we are doing over here mov eax, offset flat:string
?
I'm really confused. If doing mov byte ptr[string + 4], 'a'
works to get the address of the string label + 4 then why doing mov eax, string
isn't the same ?
Edit :
To clarify, After calling entry, I use printf to print what's in EAX as follow :
#include <stdio.h>
extern char *entry(void);
int main(int argc, char*argv[])
{
printf("%s", entry());
}