Modifying array elements with inline assembly

ぃ、小莉子 提交于 2019-12-04 04:43:32

问题


Is there a way of modifying specific array elements with inline assembly ?

int move[2];

I'm looking to change move[0] and move[1] in __asm.
I am a novice with assembly coding, mainly stick to C++, and there is probably a very simple answer.
So far I've attempted to move move[1] into registers, move the number I want to change it to into another, and then move one into the other. I have managed to get it to compile but it doesnt actually work.


回答1:


You can use something like MOV array[TYPE array * index], value;, for example:

#include <stdio.h>

int main(int argc, char **argv) {
  int foo[] = {1, 2, 3};

  printf("%d\n", foo[0]);
  printf("%d\n", foo[1]);
  printf("%d\n", foo[2]);

  __asm {
    MOV foo[TYPE foo * 0], 11;
    MOV foo[TYPE foo * 1], 22;
    MOV foo[TYPE foo * 2], 33;
  };

  printf("%d\n", foo[0]);
  printf("%d\n", foo[1]);
  printf("%d\n", foo[2]);

  return 0;
}

TYPE will return the size of one element of the array. The output:

1
2
3
11
22
33


来源:https://stackoverflow.com/questions/21369405/modifying-array-elements-with-inline-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!