Gregor Trplan
Gregor Trplan

Reputation: 33

ASM what do [] do exactly?

I've been following this tutorial but I got stuck. He starts explaining at about 6:30.
So there is a for loop in that episode that looks like this. And I got lost during the explanation as he said. But I rewatched it a couple of times and there is one thing I don't understand. So this is the code and I took some notes while watching the registers.
mov rax, [rbp+arg]
So after this line rax = 0x7ffe63c2d498 and arg = 0x7ffe63c2d380 . I decoded these two but nothing comes up so I'm assuming they are pointers.
add rax, 8
mov rdx, [rax]
After this line rdx = 0x7ffe63c2e09d . I'm assuming this is a pointer again.
mov eax, [rbp+i]
This moves the value of i in eax for example 0x01
cdqe
add rax, rdx
Here you add the value of i to the pointer of the string.
movzx eax, byte ptr [rax]
Here you move the character that the rax was pointing to into eax.
movsx eax, al
add [rbp+sum], eax
add [rbp+i], 1
Here you sum up and i++.

My question is: aren't [] supposed to move the value that the address points to into the register? So it moves the value arg is pointing to into rax and then the value rax is pointing to into rdx. But both of these are pointers. How come? So arg is a pointer to a pointer to a pointer?

Upvotes: 1

Views: 446

Answers (1)

bolov
bolov

Reputation: 75668

Assembly doesn't have data types in the sense that C does. There are no pointers in assembly. There are only registers and immediate operands. The instruction dictates how the value of a register or immediate is interpreted by that instruction.

but you can still think of them as pointers? They point to a value that is accessed with []?

Sort of. Only in the context of an instruction and only for you.

For instance mov eax rdi. Here the value of rdi is interpreted just like a number. For you, who are trying to understand the algorithm it could mean a counter, or a sum, or an offset, or a pointer. For the instruction however it's just a number.

But in mov eax [rdi] here rdi is interpreted as an address in memory.

In lea eax, [rsi + rdi] here the value of rsi + rdi is interpreted as a memory address. But for you this instruction just computes rsi + rdi so it really could mean anything to you, the sum of a pointer and an offset, or the sum of two integers. But that is just the meaning you put to them to understand the algorithm.

To answer your question [OP] means "the value found in memory at address OP".

lea eax, [rsi + rdi] means "load in eax the effective address of the value found in memory at address rsi + rdi" which is just rsi + rdi

Upvotes: 1

Related Questions