亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 請教一個擴(kuò)展匯編的問題 [打印本頁]

作者: flw2    時間: 2007-06-20 14:32
標(biāo)題: 請教一個擴(kuò)展匯編的問題
我在內(nèi)核代碼看匯編的時候,不明白'=&r'和'=r'是什么區(qū)別,看了n多資料都一樣的,就幾句話,還是沒看懂
作者: 帥絕人寰    時間: 2007-06-20 15:18
Unless an output operand has the `&' constraint modifier, GCC may
allocate it in the same register as an unrelated input operand, on the
assumption the inputs are consumed before the outputs are produced.
This assumption may be false if the assembler code actually consists of
more than one instruction.  In such a case, use `&' for each output
operand that may not overlap an input.  *Note Modifiers::.


`&'
     Means (in a particular alternative) that this operand is an
     "earlyclobber" operand, which is modified before the instruction is
     finished using the input operands.  Therefore, this operand may
     not lie in a register that is used as an input operand or as part
     of any memory address.

     `&' applies only to the alternative in which it is written.  In
     constraints with multiple alternatives, sometimes one alternative
     requires `&' while others do not.  See, for example, the `movdf'
     insn of the 68000.

     An input operand can be tied to an earlyclobber operand if its only
     use as an input occurs before the early result is written.  Adding
     alternatives of this form often allows GCC to produce better code
     when only some of the inputs can be affected by the earlyclobber.
     See, for example, the `mulsi3' insn of the ARM.

     `&' does not obviate the need to write `='.
作者: flw2    時間: 2007-06-20 15:52
這個我看了,能解釋一下嗎?不明白用與不用的區(qū)別
我把一些代碼翻譯了一下,有沒有&都是一樣的,一個字的區(qū)別都沒有
作者: albcamus    時間: 2007-06-20 16:03
Unless an output operand has the `&' constraint modifier, GCC may
allocate it in the same register as an unrelated input operand, on the
assumption the inputs are consumed before the outputs are produced.
This assumption may be false if the assembler code actually consists of
more than one instruction.  In such a case, use `&' for each output
operand that may not overlap an input.
__________________

翻譯一下:

&只是用來約束輸出部的。  如果不指定&約束符, 則GCC有可能為一個輸入操作數(shù)和該輸出操作數(shù)使用同一個寄存器, 這是基于以下的假設(shè): 在產(chǎn)生輸出之前(把輸出寫入到寄存器), 作為輸入的寄存器內(nèi)容已經(jīng)不再需要了。   有時侯嵌入?yún)R編有很多條指令,這種假設(shè)可能是不對的, 有可能過早產(chǎn)生了輸出, 從而覆蓋了輸入。  加上&約束,就會告訴gcc使用另一個寄存器, 不要和輸入寄存器使用同一個。
作者: flw2    時間: 2007-06-20 17:43
有一丁點頭緒,如果我理解對了,那么就是說'&'有時候是必須的,因為否則輸入和輸出的順序在代碼中要保持先后。
如果寄存器不夠用,那么使用了'&'就表示在使用這個寄存器之前需要保存到某個內(nèi)存中.
作者: albcamus    時間: 2007-06-20 18:01
>> 如果寄存器不夠用,那么使用了'&'就表示在使用這個寄存器之前需要保存到某個內(nèi)存中.

(把原來的錯誤理解刪除)

如果不加&符號, 舉例來說, 如果寄存器不夠用,GCC很可能為 輸出操作數(shù)"=b"和輸入操作數(shù)"a"使用同一個寄存器, 因為它假設(shè): 在產(chǎn)生輸出之前, 輸入操作數(shù)就已經(jīng)用完了,不需要了。

輸出約束改成"=&b"能改變這一默認(rèn)的優(yōu)化。 "=&b"表示,這是一個"earlyclobber",可能會過早的損壞其他操作數(shù),所以你為我單獨(dú)分配個寄存器吧。    info手冊說, 加上&只可能讓gcc產(chǎn)生更優(yōu)代碼,沒有副作用。



這個問題比較難在代碼中試驗,不過理論應(yīng)該就是這樣的。

[ 本帖最后由 albcamus 于 2007-6-21 09:38 編輯 ]
作者: albcamus    時間: 2007-06-22 11:05
樓主弄明白這個問題沒有?
作者: flw2    時間: 2007-11-21 22:50
今天翻精華,看見這個帖子,后來忘記這個帖子了
說實話,其實我還是沒有完全理解,但是估計就是我現(xiàn)在理解的這個意思了

#include <stdio.h>
int main()
{
        unsigned short cs = 0;
        volatile unsigned short a,b,c,d,e,f;
        asm ("movw %%cs,%0\n\t"
                        :"=&r"(cs):"r"(a),"r"(b),"r"(c),"r"(d),"r"(e),"r"(f));
        printf("%x\n",cs);
}


這個代碼編譯出錯,因為沒有通用寄存器可用
但是如果沒有&,則可以重用
        .file        "cs.c"
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string        "%x\n"
        .text
        .p2align 4,,15
.globl main
        .type        main, @function
main:
        leal        4(%esp), %ecx
        andl        $-16, %esp
        pushl        -4(%ecx)
        pushl        %ebp
        movl        %esp, %ebp
        subl        $40, %esp
        movl        %ecx, -16(%ebp)
        movzwl        -18(%ebp), %edx/////
        movl        %ebx, -12(%ebp)
        movl        %esi, -8(%ebp)
        movl        %edi, -4(%ebp)
        movzwl        -20(%ebp), %edi
        movzwl        -22(%ebp), %esi
        movzwl        -24(%ebp), %ebx
        movzwl        -26(%ebp), %ecx
        movzwl        -28(%ebp), %eax
#APP
        movw %cs,%dx//dx是輸入
      ####如果這個后面還有代碼要使用a的值(dx),那么將不是a原始的值
       
#NO_APP
        movzwl        %dx, %edx
        movl        %edx, 4(%esp)
        movl        $.LC0, (%esp)
        call        printf
        movl        -16(%ebp), %ecx
        movl        -12(%ebp), %ebx
        movl        -8(%ebp), %esi
        movl        -4(%ebp), %edi
        movl        %ebp, %esp
        popl        %ebp
        leal        -4(%ecx), %esp
        ret
        .size        main, .-main
        .ident        "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
        .section        .note.GNU-stack,"",@progbits
作者: albcamus    時間: 2007-11-22 10:36
我見過最好的兩篇gcc inline asm文檔
第一篇是很多人都看過的GCC inline Assembly HOWTO:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

第二篇寫的更好,是1998年LKML上的郵件:
http://groups.google.com/group/m ... ad/ca8860dd41ddd42b
作者: flw2    時間: 2007-11-22 10:50
第一篇看過,在你的博客中看到過,有時間看看




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2