0

I don't know how to access a stl vector in x86. I have tried to do it like that but I have some errors.

mov ebx, stl_vector 
mov eax, [ebx] ;Here I want to store the first element of the vector
mov edx, [ebx + 4] ; I want to store the second element of the vector

I want to do the same in SSE language.

Thank you in advance!

1 Answers1

4

stl vectors are objects. Unless you know the exact class layout you can't access them directly. You should probably pass a pointer to the array and a size separately to your assembly function, e.g. asm(vector.data(), vector.size()) so the compiler takes care of the c++ stuff.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • You need to know the _vTable layout of the `stl_vector` class to properly access its members. Your `mov eax, [ebx]` probably will probably get you the address of the destructor of the `stl_vector` class and not the desired data field (address). FYI this layout may change between versions of your compiler. – zx485 May 02 '16 at 15:13
  • Once I have the array, how can I access to the elements? As I wrote before? Thank you! – Manuel Minguez May 02 '16 at 15:15
  • Yes, if you have the address in `ebx` then `[ebx]` and `[ebx+4]` would be the first and second elements respectively. – Jester May 02 '16 at 15:18
  • Depends on the type of value, for 32 bit `int` the `[ebx]` and `[ebx+4]` is first and second element (as in `[ebx + 4 * element_index]`). For other types adjust the "4" according to element type size. – Ped7g May 02 '16 at 15:30