gcc 內嵌彙編的學習筆記 II

來源:互聯網
上載者:User

                  gcc 內嵌彙編的學習筆記 II
                                    --一個簡單的條件判斷
  作者:ShellEx. 
  ShellEx.cn && blog.csdn.net/shellex 著作權
 真不好意思,用進階語言的想法類比彙編。畢竟是學進階語言出來
的其實就是對指令的學習了。結合彙編出來的代碼,條件判斷使用了cmp
指令。cmp的結果放在某個地方...(-_-b),貌似是蝦米標誌位...
具體哪個地方還得參考下書本。

下面是我寫的內嵌彙編代碼
#include <stdio.h>
int main() {
 int in1 = 0, in2 = 0, out =0;
 printf("PLZ input 2 Number like this: (x1 , x2) /n");
 scanf("%d , %d", &in1, &in2);
 asm volatile(
  "nop /n/t"
  "cmp  %2, %1/n/t"        //比較in1 和 in2
  "jne  FAIL/n/t"                //如果不等,跳到FAIL
  "movl  $1, %0/n/t"        //給out賦值為1
  "jmp  END_LABEL/n" //跳到結尾
  "FAIL: /n/t"    //FAIL標號
  "movl  $0, %0/n"           //給out賦值為0
  "END_LABEL:/n/t"        //結尾標號
  :"+r"(out)
  :"r"(in1),"r"(in2)
  :"ebx"     //用到ebx,所以告訴gcc表再用它
 );
 printf("out = %d",out);
 return 0;
}
///////////////////////////////////////////////////////////////

shellex.cn && blog.csdn.net/shellex 著作權 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.