1

Topic: gcc inline assembler

Kind time of days! While there are holidays, decided to esteem about gcc. Downloaded Using the GNU Compiler Collection and it was picked in  functions. They cool But when business reached to inlajn-asseblera, business rose and does not move three days already. On pure gas all is written and works, and as I work foolish inlajn-asm I can not understand. I try to implement function swap: void swap (uint32_t* a, uint32_t* b); No more that but where that to write I will not understand in this construction: asm ("bla-bla": output: input: clobber); That is it is not clear that whence where gets. As to me to address to an and b, which pointers. Everything that at me meanwhile it turned out - to make incl local . Can explain a little that to what? Or to implement swap? From me huge human thanks

2

Re: gcc inline assembler

Here quite good article: https://www.ibm.com/developerworks/ru/l … kernel_15/

3

Re: gcc inline assembler

Hello, plastictown, you wrote: P> to implement swap? Begin so: void swap (uint32_t* a, uint32_t* b) {asm ("xchg %0, %1": "+r" (*a), "+g" (*b));} or it is better so: void swap (uint32_t* a, uint32_t* b) {asm ("xchg %0, %1": "+r, m" (*a), "+rm, r" (*b));} P> As to me to address to an and b, which pointers. Here only for swap these pointers also are not especially necessary. Much easier at once from to dereference. Look an example above. But if so it would be desirable pointers try something like void swap_p (uint32_t* a, uint32_t* b) {uint32_t tmp; asm volatile ("mov (%1), %0 \n\t" "xchg %0, (%2) \n\t" "mov %0, (%1)": "=&r" (tmp): "r" (a), "r" (b): "memory");}

4

Re: gcc inline assembler

Hello, watchmaker, you wrote: W> void swap_p (uint32_t* a, uint32_t* b) {W> uint32_t tmp; W> asm volatile (W> "mov (%1), %0 \n\t" W> "xchg %0, (%2) \n\t" W> "mov %0, (%1)" W>: "=&r" (tmp) W>: "r" (a), "r" (b) W>: "memory" W>); W>} Thanks! You could not explain this part: W>: "=&r" (tmp) W>: "r" (a), "r" (b) W>: "memory" Or where to esteem about the such: "=&r", "+g" pieces. Examples reconsidered much and for some reason it is meant that to me it is clear. And me it is not clear

5

Re: gcc inline assembler

Hello, plastictown, you wrote: P> Or where to esteem about the such: "=&r", "+g" pieces. Constraints P> to explain this part: P> W>>: "=&r" (tmp) W>>: "r" (a), "r" (b) W>>: "memory" P> Here it is told that input consists of two registers which values an and b accordingly lie." memory "in lobber list specifies that the code writes to storage in places which are not enumerated among output parameters. Record"=&r"in output says that it is necessary to reserve the register. Thus ' = ' says that initiating value is not important since it will all the same be re-recorded. And ' and ' says that in this register there is a record before are read all input the data, it means simply that this register should not coincide with any of the input. Well and volatile specifies that this code cannot be thrown out at compilation even if any output is not used (and here it especially only one output - tmp, and it precisely is not used). And the first example swap with dereferencing nevertheless : in it it is not necessary to specify temporal registers, neither clobber list, nor volatile - the compiler can deduce all automatically from record"+r"which means that the data the parameter is used both on reading and on record. And as the compiler delivers from storage value to this address - a compiler problem.

6

Re: gcc inline assembler

Hello, watchmaker, you wrote: W> Here it is told that input consists of two registers which values an and b accordingly lie. W> "memory" in lobber list specifies that the code writes to storage in places which are not enumerated among output parameters. W> Record "=&r" in output says that it is necessary to reserve the register. Thus ' = ' says that initiating value is not important since it will all the same be re-recorded. And ' and ' says that in this register there is a record before are read all input the data, it means simply that this register should not coincide with any of the input. W> well and volatile specifies that this code cannot be thrown out at compilation even if any output is not used (and here it especially only one output - tmp, and it precisely is not used). W> And the first example swap with dereferencing nevertheless : In it it is not necessary to specify temporal registers, neither clobber list, nor volatile - the compiler can deduce all automatically from record "+r" which means that the data the parameter is used both on reading and on record. And as the compiler delivers from storage value to this address - a compiler problem. To you the documentation to write Thanks! I will pick further.

7

Re: gcc inline assembler

Just in case I will add what to implement swap it is possible also thus: void swap (uint32_t* a, uint32_t* b) {asm ("": "=rm" (*a), "=rm" (*b): "0" (*b), "1" (*a));} That is at all without using any instructions. Therefore it is better to train on any less trivial function smile