1

I found an example and was editing it for gas.

extern printf
.global _start
.data
hello:
db "Hello", 0xa, 0
.text
_start:
mov %rdi, hello
mov %rax, 0
call printf
mov %rax, 0
ret

But it doesn't work. What's wrong? What does this mean:

    hello:
db "Hello", 0xa, 0

I understand what it scope of memory, but I don't understand this string

db "Hello", 0xa, 0

And here

_start:
mov %rdi, hello
mov %rax, 0
call printf
mov %rax, 0
ret

os: linux (debian). intel 64-bit

nneonneo
  • 171,345
  • 36
  • 312
  • 383
volkov
  • 117
  • 3
  • 13

3 Answers3

1

It's is the null-byte-terminattor. Well-know as C-string.Such byte at end-of-string say where the string ends. For example,you pass the pointer to your string to call a routine,the routine will understand that the area of such string on memory is from begging a[0](in C terminology) until a[x] == 0 is seen.

Jack
  • 16,276
  • 55
  • 159
  • 284
0

All that does is place bytes into the program. The bytes are the characters "Hello", followed by 0xa (which is the line termination), and finally a NULL byte. In C it would be something like "char *hello = "Hello\n";"

At your _start: label, you place the address of the hello label into register %rdi, you place 0 into %rax, and you call the printf function.

mah
  • 39,056
  • 9
  • 76
  • 93
0

The following declares a string with Hello followed by a line feed and null terminator. The null terminator is required for C strings

db "Hello", 0xa, 0

To call printf, you need to pass the parameter on the stack so it would be something like

mov hello, (%esp)
call printf

As far as I know, the convention is mov source, destination. You seem to have coded it the other way round.

cup
  • 7,589
  • 4
  • 19
  • 42
  • Using `rdi` for the first argument is correct on a 64-bit Linux/UNIX. Windows on x86-64 uses `rcx, rdx, r8` and `r9` for the first four integer/pointer arguments, and the stack for the rest. Linux/UNIX uses `rdi, rsi, rdx, rcx, r8` and `r9` for the first six arguments. The source/destination order does look reversed though if this is supposed to be AT&T syntax code. – Michael Mar 23 '13 at 17:09