Review-C language Inline compilation-Beginner (2)

Source: Internet
Author: User

Assembly takes out the value in memory

1# include <stdio.h>2 3 intMain ()4 {5         inti = -;6         intRET =0;7 8         int*p = &i;9         //ret = *p;Ten __asm__ ( One                 "Ldr%0, [%1]" A  -:"+r"(ret)//Output -:"R"(p)//input the         ); -printf"Hello world!%d\n", ret); -}

Description: [] equivalent to *p in the *,%1 equivalent to address p, that is [%1] = *p, the contents of the address p out!

Change the value of a variable

1#include <stdio.h>2 3 intMain ()4 {5         inti =6;6         intRET =0;7 8         //i = +;9 __asm__ (Ten                 "mov r0, #100 \ n" One                 "str r0, [%1]\n" A                 //"Ldr r0, [%1]\n" -  -:"+r"(ret)//Output the:"R"(&i) -         ); -  -printf"I%d\n", i); +}

Note: The immediate number 100 is uploaded to R0, then r0 to * (&i), that is, change the value of the variable I address.

Assigning a value to an array

1#include <stdio.h>2 3 intMain ()4 {5         intarr[3] = {0};6 7 __asm__ (8                 "mov r0, #1 \ n"9                 "str r0, [%0, #0]\n"Ten                 "add R0, r0, #1 \ n" One                 "str r0, [%0, #4]\n" A                 "add R0, r0, #1 \ n" -                 "str r0, [%0, #8]\n" -  the://"+r" (arr)//Error -:"R"(arr) -:"R0" -         ); +  -printf"Arr[0]%d\n", arr[0]); +printf"Arr[1]%d\n", arr[1]); Aprintf"Arr[2]%d\n", arr[2]); at}

Description: The input section is an array of the first address of ARR, paying special attention.

About the back shift of the address

1#include <stdio.h>2 3 intMain ()4 {5         intarr[3] = {1,2,3};6         inti =Ten; 7         intp =0;8 9 __asm__ (Ten                 //Ldr%0, [%2]\n]//addr and value not change One                 //Ldr%0, [%2, #4]\n]//addr, Value change A                 //Ldr%0, [%2, #4]!\n]//addr Change, value change -                 "Ldr%0, [%2], #4 \ n" //addr Change, value no change -                 "mov%1,%2\n" the      -:"+r"(i),"+r"(p)//Error -:"R"(arr) -:"R0" +         ); -      +printf"i is:%d\n", i); Aprintf"P is:%x\n", p);  atprintf"arr is:%x\n", arr); -}

Description: The difference between line 11th, line 12, and line 13: for example, suppose the arr address is 0x10

11 Line: Is 0x10 + 4 is equal to the address of the 0x14, arr itself or 0x10, take the value of the 0x14 inside to I, the next arr + 4 or 0x14

12 line: More than an exclamation mark, is now the base or 0x10,+ 4, arr value becomes 0x14, the next time arr + 4 becomes 0x18

13 Line: First assign the value of the 0x10 inside to I, then add the address to the 0x14

    

Review-C language Inline compilation-Beginner (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.