C語言原始檔到組合語言的編譯

2021-07-24 08:55:33 字數 1316 閱讀 8306

這是我們經常寫的最簡單的程式:

#include

int main()

在我們使用gcc指令來編譯c語言原始檔的時候,在命令列輸入下面命令:

[root@localhost kangkang]# gcc -o hello hello.c   //#編譯c**生成目標檔案為hello。

預設情況下,gcc指令將編譯連線過程一步完成,使用適當的選項可以將編譯過程分步驟完成。使用「-e」選項僅完成編譯預處理操作:

[root@localhost kangkang]# gcc -e -o hello.i hello.c   //#僅執行編譯預處理,指定預處理後生成的檔案。

接下來,使用「-s」選項生成彙編**。在命令列中輸入下面的命令:

[root@localhost kangkang]# gcc -s -o hello.s hello.i    //#將c語言轉換為彙編源**檔案「hello.s」  。

最後,使用cat指令檢視生成的「.s」檔案:

[root@localhost kangkang]# cat hello.s

.file   "hello.c"

.section        .rodata

.lc0:

.string "hello,world!"

.text

.globl main

.type   main, @function

main:

leal    4(%esp), %ecx

andl    $-16, %esp

pushl   -4(%ecx)

pushl   %ebp

movl    %esp, %ebp

pushl   %ecx

subl    $4, %esp

movl    $.lc0, (%esp)

call    puts

movl    $0, %eax

addl    $4, %esp

popl    %ecx

popl    %ebp

leal    -4(%ecx), %esp

ret.size   main, .-main

.ident  "gcc: (gnu) 4.1.2 20070626 (red hat 4.1.2-14)"

.section        .note.gnu-stack,"",@progbits

使用as指令的「-o」選項可以將彙編**檔案編譯為目標檔案:

[root@localhost kangkang]# as -o hello.o hello.s      //#將彙編檔案編譯為目標檔案。

組合語言 編譯器

乙個組合語言程式從寫出到最終執行的簡要過程 編寫 編譯 連線 執行 notepad 選擇assembly assume cs abc abc被我們當做 段來使用,so要把它和cs聯絡起來 abc segment 定義乙個段 abc 到ends結束 start mov ax,2 給程式乙個起始位置 a...

從機器語言到組合語言

機器語言是計算機唯一能接受和執行的語言。機器語言由二進位製碼組成,每一串二進位製碼叫做一條指令。一條指令規定了計算機執行的乙個動作。一台計算機所能懂得的指令的全體,叫做這個計算機的指令系統。不同型號的計算機的指令系統不同。指令通常由幾個位元組組成,第乙個位元組是操作碼,它規定了計算機要執行的基本操作...

C語言轉成MIPS組合語言

功能 隨機輸入兩個整數,然後計算這兩個數的最小公倍數和最大公約數,並作為計算結果輸出。include stdio.h include conio.h int main a num1 b num2 while b 0 一直重複賦值和計算,直到找到正確結果 printf gongyueshu d n a...