I am having some trouble understanding how this operation works in assembly. I have a program I wrote and disassembled. In it, I access individual characters of a string and compare it to a test string. The important part of the code that I am confused about is here:
mov ecx, [ebp+string_offset]
add ecx, [ebp+counter_offset]
movsx edx, byte ptr [ecx]
This is generated via some compiler optimization I am guessing. The actual function of the code makes sense. ECX is given an address, the counter is added to it (to increment the location in memory) and then MOVSX
loads the byte pointed to by ECX.
Where I am confused is the bracketing here. Normally brackets are dereferencing operations. So, if that was true ECX would contain the first actual element of the string. Then, we'd dereference the counter, and add it to ECX to get the element we want, and finally load that element into EDX for comparison.
This does not make any sense because if dereferencing a string's address is giving me back the string itself, then adding the counter would just produce non-sense. Indeed, during debugging ECX will contain the actual address of the character in the string and then EDX will contain the character itself.
Is this a non-standard use of brackets or something special with c-strings (Char* xyz = "test"
) as arrays that I am missing?