c文件匯編后函數(shù)參數(shù)傳遞的不同之處
mac下clang編譯后函數(shù)的參數(shù)先保存在寄存器中(以一定的規(guī)則保存),然后在函數(shù)中壓入棧里,
以待后用。例如上篇例子,紅色部分:
.global _decToBin
_decToBin:
pushq %rbp
movq %rsp,%rbp
movq %rdi,-8(%rbp) #第一個參數(shù),保存在rdi中
movq %rsi,-16(%rbp) #第二個參數(shù),保存在rsi中
movq -8(%rbp),%rax
movq -16(%rbp),%rbx
movq $63,%rcx
......
popq %rbp
ret
而我在w7下使用cygwin安裝的gcc編譯test.c文件:
test.c:
int hello(int a,int b,int c,int d)
{
return b;
}
test.c
.file "test.c"
.text
.globl _hello
.def _hello; .scl 2; .type 32; .endef
_hello:
pushl %ebp
movl %esp, %ebp
movl 12(%ebp), %eax #說明參數(shù)是函數(shù)在使用其值之前就已經(jīng)壓入棧中
popl %ebp
ret
這說明clang與gcc使用了兩種不同的規(guī)則(網(wǎng)上有很多介紹函數(shù)值傳遞的不同規(guī)則的,我就不介紹了)。
所以不同的平臺不同的編譯器要不同的對待。以上算是上次的不足補充吧。
下面來看看數(shù)組:
test.c例子:
void hello1()
{
int a[3]={1,2,3};
int b=a[1];
}
void hello2()
{
int a[3]={1,2,3};
int b=*(a+1);
}
void hello3()
{
int a[3]={1,2,3};
int b=1[a]; //這也對?
}
如果看的夠仔細的話,三個函數(shù)沒什么不同就是對數(shù)組a[1]的不同(當然函數(shù)名除外).
gcc -S test.c 后:
.file "test.c"
.data
.align 4
LC0:
.long 1
.long 2
.long 3
.text
.globl _hello1
.def _hello1; .scl 2; .type 32; .endef
_hello1:
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
subl $16, %esp
leal -28(%ebp), %edx
movl $LC0, %ebx
movl $3, %eax
movl %edx, %edi
movl %ebx, %esi
movl %eax, %ecx
rep movsl
movl -24(%ebp), %eax
movl %eax, -16(%ebp)
addl $16, %esp
popl %ebx
popl %esi
popl %edi
popl %ebp
ret
.globl _hello2
.def _hello2; .scl 2; .type 32; .endef
_hello2:
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
subl $16, %esp
leal -28(%ebp), %edx
movl $LC0, %ebx
movl $3, %eax
movl %edx, %edi
movl %ebx, %esi
movl %eax, %ecx
rep movsl
leal -28(%ebp), %eax
movl 4(%eax), %eax
movl %eax, -16(%ebp)
addl $16, %esp
popl %ebx
popl %esi
popl %edi
popl %ebp
ret
.globl _hello3
.def _hello3; .scl 2; .type 32; .endef
_hello3:
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
subl $16, %esp
leal -28(%ebp), %edx
movl $LC0, %ebx
movl $3, %eax
movl %edx, %edi
movl %ebx, %esi
movl %eax, %ecx
rep movsl
movl -24(%ebp), %eax
movl %eax, -16(%ebp)
addl $16, %esp
popl %ebx
popl %esi
popl %edi
popl %ebp
ret
只要看紅色的行,我們可以看到25-27行與74-76行一樣,說明hello1與hello3沒什么不同,
效率一樣。而49-52行比他們多了一行,所以*(a+1)比a[1]和1[a]要低一點。
但是我們看下面的例子。
test1.c與test2.c:
//1--------------
#include <stdlib.h>
void hello()
{
int *a=(int*)malloc(sizeof(int)*3);
int b=*(a+1);
free(a);
}
//2--------------
#include <stdlib.h>
void hello()
{
int *a=(int*)malloc(sizeof(int)*3);
int b=a[1];
free(a);
}
匯編后完全一樣:
.file "main.c"
.text
.globl _hello
.def _hello; .scl 2; .type 32; .endef
_hello:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
movl $12, (%esp)
call _malloc
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
movl 4(%eax), %eax
movl %eax, -16(%ebp)
leave
ret
.def _malloc; .scl 2; .type 32; .endef
所以在堆中使用*(a+n)與a[n]沒什么不同,只用在棧中才會有所不同。
學習匯編不是必要,但是它可以讓我們知道效率。
欄 目:C語言
下一篇:STL 的string類怎么啦
本文標題:c文件匯編后函數(shù)參數(shù)傳遞的不同之處
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3922.html
您可能感興趣的文章
- 01-10fatal error LNK1104: 無法打開文件“l(fā)ibc.lib”的解決方法
- 01-10顯示任何進程加載的DLL文件的代碼
- 01-10深入探討linux下進程的最大線程數(shù)、進程最大數(shù)、進程打開的文
- 01-10使用Inotify 監(jiān)控目錄與文件的方法詳解
- 01-10用c 獲取文件MD5值的實現(xiàn)方法
- 01-10用C實現(xiàn)添加和讀取配置文件函數(shù)
- 01-10在vs2010中,輸出當前文件路徑與源文件當前行號的解決方法
- 01-10C++中簡單讀寫文本文件的實現(xiàn)方法
- 01-10C語言文件操作函數(shù)大全(超詳細)
- 01-10深入探討:linux中遍歷文件夾下的所有文件


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05織夢dedecms什么時候用欄目交叉功能?