Linux下組合語言------計算n的階乘

來源:互聯網
上載者:User

標籤:sub   -o   跳轉指令   寫法   linux   imu   filename   echo   section   

用進階語言比方C語言寫個階乘非常easy,那如今就來熟悉下Linux使用AT&T彙編格式寫一個程式來計算階乘吧

寫法一是 使用跳轉指令實現。寫法二是使用函數來實現

約定:本程式沒有將結果列印在標準輸出上。

須要使用GDB調試查看

寫法一
#as filename.s -o filename.o#ld filename.o -o filename # gdb filename.section .data.section .text.globl _start_start:movl $1, %eax #result = 1movl $6, %ecx # n = 5cmpl $1, %ecxjle .loop_exit.start_loop:imull %ecx, %eaxsubl  $1, %ecxcmpl $1, %ecxjge .start_loopmovl %eax, %ebxmovl $1, %eaxint $0x80 .loop_exit:movl $1, %eaxint $0x80
--------------------函數寫法待更新-------------------------寫法二:函數遞迴
.section .data.section .text.globl _start_start:pushl $5call .JCmovl %eax, %ebxmovl $1, %eaxint $0x80.JC:pushl %ebpmovl %esp, %ebppushl %ebxsubl $4, %espmovl 8(%ebp), %ebx #get n = 5;movl $1, %eax      #result = 1;cmpl $1, %ebxjle .L1leal -1(%ebx), %eaxmovl %eax, (%esp)call .JC imull %ebx, %eax.L1:addl $4, %esppopl %ebxpopl %ebpret 

查看結果:echo $?當然使用gdb來查看,會更明確。

使用 echo $? 來查看的話,貌似n超過6了結果就顯示不對,所以還是用gdb保險

Linux下組合語言------計算n的階乘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.