Kısa cevap, C tarafından desteklenen dil yapılarının çoğu aynı zamanda hedef bilgisayarın mikro işlemcisi tarafından da desteklenmektedir, bu nedenle derlenmiş C kodu mikroişlemcinin montaj diline çok güzel ve verimli bir şekilde çevrilir ve böylece daha küçük kod ve daha küçük bir ayak izi ile sonuçlanır.
Daha uzun cevap, biraz derleme dili bilgisi gerektirir. C'de şöyle bir ifade:
int myInt = 10;
montajda böyle bir şeye çevrilir:
myInt dw 1
mov myInt,10
Bunu C ++ gibi bir şeyle karşılaştırın:
MyClass myClass;
myClass.set_myInt(10);
Elde edilen derleme dili kodu (MyClass () değerinin ne kadar büyük olduğuna bağlı olarak), yüzlerce derleme dili satırı ekleyebilir.
Aslında assembly dilinde programlar oluşturmadan, saf C muhtemelen içinde bir program yapabileceğiniz "en ince" ve "en sıkı" koddur.
DÜZENLE
Cevabımla ilgili yorumlar göz önüne alındığında, sadece kendi akıl sağlığım için bir test yapmaya karar verdim. "Test.c" adlı bir program oluşturdum, şuna benzer:
#include <stdio.h>
void main()
{
int myInt=10;
printf("%d\n", myInt);
}
Bunu gcc kullanarak derlemeye kadar derledim. Derlemek için aşağıdaki komut satırını kullandım:
gcc -S -O2 test.c
İşte ortaya çıkan montaj dili:
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d\n"
.section .text.unlikely,"ax",@progbits
.LCOLDB1:
.section .text.startup,"ax",@progbits
.LHOTB1:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB24:
.cfi_startproc
movl $10, %edx
movl $.LC0, %esi
movl $1, %edi
xorl %eax, %eax
jmp __printf_chk
.cfi_endproc
.LFE24:
.size main, .-main
.section .text.unlikely
.LCOLDE1:
.section .text.startup
.LHOTE1:
.ident "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
.section .note.GNU-stack,"",@progbits
Daha sonra bir sınıfı tanımlayan ve "test.c" ile aynı şeyi veren "test.cpp" adlı bir dosya oluşturuyorum:
#include <iostream>
using namespace std;
class MyClass {
int myVar;
public:
void set_myVar(int);
int get_myVar(void);
};
void MyClass::set_myVar(int val)
{
myVar = val;
}
int MyClass::get_myVar(void)
{
return myVar;
}
int main()
{
MyClass myClass;
myClass.set_myVar(10);
cout << myClass.get_myVar() << endl;
return 0;
}
Bu komutu kullanarak aynı şekilde derledim:
g++ -O2 -S test.cpp
İşte ortaya çıkan montaj dosyası:
.file "test.cpp"
.section .text.unlikely,"ax",@progbits
.align 2
.LCOLDB0:
.text
.LHOTB0:
.align 2
.p2align 4,,15
.globl _ZN7MyClass9set_myVarEi
.type _ZN7MyClass9set_myVarEi, @function
_ZN7MyClass9set_myVarEi:
.LFB1047:
.cfi_startproc
movl %esi, (%rdi)
ret
.cfi_endproc
.LFE1047:
.size _ZN7MyClass9set_myVarEi, .-_ZN7MyClass9set_myVarEi
.section .text.unlikely
.LCOLDE0:
.text
.LHOTE0:
.section .text.unlikely
.align 2
.LCOLDB1:
.text
.LHOTB1:
.align 2
.p2align 4,,15
.globl _ZN7MyClass9get_myVarEv
.type _ZN7MyClass9get_myVarEv, @function
_ZN7MyClass9get_myVarEv:
.LFB1048:
.cfi_startproc
movl (%rdi), %eax
ret
.cfi_endproc
.LFE1048:
.size _ZN7MyClass9get_myVarEv, .-_ZN7MyClass9get_myVarEv
.section .text.unlikely
.LCOLDE1:
.text
.LHOTE1:
.section .text.unlikely
.LCOLDB2:
.section .text.startup,"ax",@progbits
.LHOTB2:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB1049:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $10, %esi
movl $_ZSt4cout, %edi
call _ZNSolsEi
movq %rax, %rdi
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
xorl %eax, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1049:
.size main, .-main
.section .text.unlikely
.LCOLDE2:
.section .text.startup
.LHOTE2:
.section .text.unlikely
.LCOLDB3:
.section .text.startup
.LHOTB3:
.p2align 4,,15
.type _GLOBAL__sub_I__ZN7MyClass9set_myVarEi, @function
_GLOBAL__sub_I__ZN7MyClass9set_myVarEi:
.LFB1056:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
addq $8, %rsp
.cfi_def_cfa_offset 8
jmp __cxa_atexit
.cfi_endproc
.LFE1056:
.size _GLOBAL__sub_I__ZN7MyClass9set_myVarEi, .-_GLOBAL__sub_I__ZN7MyClass9set_myVarEi
.section .text.unlikely
.LCOLDE3:
.section .text.startup
.LHOTE3:
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I__ZN7MyClass9set_myVarEi
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.hidden __dso_handle
.ident "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
.section .note.GNU-stack,"",@progbits
Açıkça görebileceğiniz gibi, ortaya çıkan montaj dosyası C ++ dosyasında çok daha büyüktür, ardından C dosyasındadır. Tüm diğer şeyleri kesip sadece C "ana" ı C ++ "ana" ile karşılaştırsanız bile, fazladan birçok şey vardır.