汇编取出内存中的值
1 # include <stdio.h> 2 3 int main() 4 { 5 int i = 100; 6 int ret = 0; 7 8 int *p = &i; 9 //ret = *p; 10 __asm__( 11 "ldr %0, [%1]" 12 13 :"+r" (ret) //输出 14 :"r" (p) //输入 15 ); 16 printf("hello world! %d\n", ret); 17 }
说明:[ ]相当于 *P中的*,%1相当于地址P,即 [%1] = *P,把地址P里的内容取出来!
改变变量的值
1 #include <stdio.h> 2 3 int main() 4 { 5 int i = 6; 6 int ret = 0; 7 8 //i = 100; 9 __asm__( 10 "mov r0, #100\n" 11 "str r0, [%1]\n" 12 //"ldr r0, [%1]\n" 13 14 : "+r" (ret) //输出 15 : "r" (&i) 16 ); 17 18 printf("i %d\n", i); 19 }
说明:先将立即数100传到r0,然后将r0传到 *(&i),也就是改变了变量i地址里面的值。
给数组赋值
1 #include <stdio.h> 2 3 int main() 4 { 5 int arr[3] = {0}; 6 7 __asm__( 8 "mov r0, #1\n" 9 "str r0, [%0, #0]\n" 10 "add r0, r0, #1\n" 11 "str r0, [%0, #4]\n" 12 "add r0, r0, #1\n" 13 "str r0, [%0, #8]\n" 14 15 : //"+r" (arr) //error 16 : "r" (arr) 17 : "r0" 18 ); 19 20 printf("arr[0] %d\n", arr[0]); 21 printf("arr[1] %d\n", arr[1]); 22 printf("arr[2] %d\n", arr[2]); 23 }
说明:输入部分是数组的首地址arr,特别注意。
关于地址的后移
1 #include <stdio.h> 2 3 int main() 4 { 5 int arr[3] = {1, 2, 3}; 6 int i = 10; 7 int p = 0; 8 9 __asm__( 10 //"ldr %0, [%2]\n" //addr and value not change 11 //"ldr %0, [%2, #4]\n" //addr not change, value change 12 //"ldr %0, [%2, #4]!\n" //addr change, value change 13 "ldr %0, [%2], #4\n" //addr change, value not change 14 "mov %1, %2\n" 15 16 : "+r" (i), "+r" (p) //error 17 : "r" (arr) 18 : "r0" 19 ); 20 21 printf("i is: %d\n", i); 22 printf("p is: %x\n", p); 23 printf("arr is: %x\n", arr); 24 }
说明:第11行、12行和13行的区别:以举例说明,假设arr地址是0x10
11行:就是0x10 + 4后等于 0x14的地址,arr本身还是0x10,取0x14里面的值到 i ,下一次arr + 4还是 0x14
12行:多了个叹号,就是现在基数还是0x10,+ 4后,arr值变为0x14,下一次arr + 4变成了 0x18
13行:先把0x10里面的值赋到i,然后地址再加到0x14
时间: 2024-10-07 11:26:21