Review-C language Inline compilation-Beginner (1)

Source: Internet
Author: User
Tags goto

Print Hello World and change the value of the variable i

1# include <stdio.h>2 3 intMain ()4 {5         inti =0;6 __asm__ (7                 "mov%0, #4 \ n"8:"=r"(i)//Output9://inputTen:"R0"           //Protection One         ); Aprintf"Hello world!%d\n", i); -}

Note: mov can upload an immediate number to the register, you can pass the value of one register to another register, but you cannot deal with memory, dealing with memory generally with LDR,STR.

The eighth line explains: "=r"-----------------> Register with the output portion, the output is also available.

"=&r"------------------> only use the output section, input is not for anyone.

"+r"---------------------> Input and output are used, for beginners generally use this.

The Tenth Line of Protection section: First __asm__ (...); This statement is embedded in the C language and is treated as an instruction; protection is the state before the R0, that is, the state before the __asm__, and the program is ultimately to be run by the Assembly, __asm__ (); R0 before? The protection is this R0, but __asm__ (...); It can also be used, such as before the R0 is 100,asm instructions or 100, midway with can be used casually.

Simple subtraction.

1#include <stdio.h>2 3 intMain ()4 {5         inti =Ten;6         intj = -;7         intsum =0;8         //sum = i + j;9 __asm__ (Ten                 "add%0,%1,%2\n" One:"=r"(sum)//Output A:"R"(i),"R"(j)//input -         ); -         //Sub%0,%2,%1 the         //Mul%0,%1,%2 -printf"sum%d\n", sum); -}

Simply do a comparison

1#include <stdio.h>2 3 intMain ()4 {5         inti = the;6         intj = -;7         intsum =0;8 #if09         if(I >j)Tensum =i; One         Else Asum =J; - #endif - __asm__ ( the                 "cmp%1,%2\n" -                 "movgt%0,%1\n" -                 "Movle%0,%2\n" -:"=r"(sum)//Output +:"R"(i),"R"(j)//input -         ); +printf"sum%d\n", sum); A}

Description: GT---------------->

Lt----------------<

GE---------------->=

Le----------------<=

EQ----------------= =

ne! ----------------      !=

Simple accumulation and

1#include <stdio.h>2 3 intMain ()4 {5         inti =1;6         intsum =0;7 8 #if09 Bunfly:Tensum = sum +i; Onei++; A         if(i = = -) -                 Gotohaha; -         GotoBunfly; the haha: -          - #else - __asm__ ( +                 "mov%0, #0 \ n" -                 "mov%1, #1 \ n" +                 "bunfly:\n" A                 "add%0,%0,%1\n" at                 "add%1,%1, #1 \ n" -                 "cmp%1, #100 \ n" -                 "beq haha\n" -                 "bne bunfly\n" -                 "haha:\n" -  in:"=&r"(sum)//Output -:"R"(i)//input to         ); + #endif -  theprintf"sum%d\n", sum); *}

Description: Assembler function can refer to the above C language is easy to understand the meaning of the Assembly, it should be noted that 19, 202 lines of initialization and 29 lines of "=&r".

These programs are re-tiny4412 the board running, of course using cross-compiling, example: ARM-NONE-LINUX-GNUEABI-GCC 4.c-o 4

Review-C language Inline compilation-Beginner (1)

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.