1

Recently, I have started learning assembly by a youtube channel and had some question about declaring a label in section .data

When we write something like: test db "hello world" , is the lable "test" something like pointer in progamming language?(which means that it hold the memory address of the first element in the text "hello world")

So when we write: mov rsi test, does it mean that we throw the address of "hello world" into rsi register?

Another question is about the usage of [], what's the meaning of writing [test]? Does it mean something like dereference in C? Just like derefernce the pointer test and than get the letter "t" which is the first letter in "hello world"?

I will be really appreciate for any responsible about my question.

BooAA
  • 173
  • 3
  • 11
  • For a good answer, posting the source code of the program you reference would be useful. – fuz Oct 20 '17 at 15:37
  • 2
    `test` is address of `'h'`. Full `"hello world"` are several bytes, occupying several addresses in memory. BTW, IMO watching youtube for things like learning asm must be much slower than reading good book about it (which would probably explain all of this well). I would be very suspicious about quality of any asm tutorial in video form, just because that form can't cover enough detail (but can be a start). – Ped7g Oct 20 '17 at 16:00
  • I think that's a good duplicate target; it seems to cover what you're asking. The C equivalent of `test: db "hello world"` is `char test[] = {'h', 'e', ..., 'd'};` (with no terminating zero byte). Or if it's in `section .rodata`, then `const char test[] = ...`. – Peter Cordes Oct 21 '17 at 01:38

1 Answers1

1

Yes, basically almost all of your guesses are mostly correct.

The big difference between a label and a C pointer is that a pointer is actually stored and can be modified during execution, but a label is only known to the assembler (unless exported to the symbol table using the global directive), so a label is the memory address, but it is constant and cannot be modified during execution.

[] syntax frequently means "the data stored at ", but really it affects how the instruction is encoded by the assembler, so what it does really depends on the instruction it is used with. For instance, the lea instruction uses the [] syntax to do address arithmetic, but does not access memory at the resultant address. However, if used with move, it still does address arithmetic, but also accesses the value stored at the resultant address.

Don't think of it as an 'operator', think of it as an operand encoding.

prushik
  • 322
  • 2
  • 7
  • `lea` is sort of like the `&` operator in C. So `lea rax, [rdi+rsi]` is like `uint64_t rax = (uint64_t)&rdi[rsi]`. [It's basically a way to expose the addressing-mode decoding capabilities of the CPU hardware for use as a shift-and-add instruction](https://stackoverflow.com/a/46597375/224132). It does still use the normal addressing mode machine-code encoding, so it makes sense that it uses the same asm syntax. – Peter Cordes Oct 21 '17 at 01:31
  • Right, that's what I was trying to say. My point was that you can't really think of [] as the dereference operator, since its not, its used to access the addressing mode encoding, which isn't always the same a dereferencing an address. – prushik Oct 21 '17 at 01:54
  • I guess I was trying to say it *is* sort of a deref operator, but you can "cancel" it out with `lea` just like `&` "cancels" `*` or `[]` in C. But I have to agree that that's a confusing way to look at asm syntax! – Peter Cordes Oct 21 '17 at 01:56
  • 1
    Another way to point out the difference between a label and a C `char *foo` is what I commented on the OP: a label is like (or actually *is*) the name of a C static/global array. C's `[]` compiles differently on a pointer vs. an array, even though it looks the same in the source. Anyway, I mostly like your answer, but I think it needs some tidying up and separate paragraphs. Feel free to copy anything I said in comments if you want to improve it. (IDK why I'm trying to also answer it in comments here after closing it as a duplicate. I'm not 100% sure it's an exact dup, I guess.) – Peter Cordes Oct 21 '17 at 02:01