This is an old revision of the document!


4. Funcții

Dorim să înțelegem cum se fac apelurile de funcții.

Exemple în C

Vom scrie programe în C pe care le vom compila folosind consola MINGW pentru a putea observa assembly-ul generat.

Deschideți consola MINGW. (C:\MinGW\msys\1.0\msys.bat)

Fără apel de funcție Cu apel simplu de funcție
test1.c
#include <stdio.h>
 
void f(void) {
}
 
int main(void)
{
	//f();
 
	return 0;
}
test2.c
#include <stdio.h>
 
void f(void) {
}
 
int main(void)
{
	f();
 
	return 0;
}

Rulăm următoarele două comenzi:

$ gcc -S -O0 -masm=intel -o test1.asm test1.c
$ gcc -S -O0 -masm=intel -o test2.asm test2.c
Fără apel de funcție Cu apel simplu de funcție
test1.asm
	.file	"test.c"
	.intel_syntax
	.text
.globl _f
	.def	_f;	.scl	2;	.type	32;	.endef
_f:
	push	ebp
	mov	ebp, esp
	pop	ebp
	ret
	.def	___main;	.scl	2;	.type	32;	.endef
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	push	ebp
	mov	ebp, esp
	sub	esp, 8
	and	esp, -16
	mov	eax, 0
	add	eax, 15
	add	eax, 15
	shr	eax, 4
	sal	eax, 4
	mov	DWORD PTR [ebp-4], eax
	mov	eax, DWORD PTR [ebp-4]
	call	__alloca
	call	___main
	mov	eax, 0
	leave
	ret
test2.asm
	.file	"test.c"
	.intel_syntax
	.text
.globl _f
	.def	_f;	.scl	2;	.type	32;	.endef
_f:
	push	ebp
	mov	ebp, esp
	pop	ebp
	ret
	.def	___main;	.scl	2;	.type	32;	.endef
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	push	ebp
	mov	ebp, esp
	sub	esp, 8
	and	esp, -16
	mov	eax, 0
	add	eax, 15
	add	eax, 15
	shr	eax, 4
	sal	eax, 4
	mov	DWORD PTR [ebp-4], eax
	mov	eax, DWORD PTR [ebp-4]
	call	__alloca
	call	___main
	call	_f
	mov	eax, 0
	leave
	ret
iocla/cursuri/capitol-07/demo/04.1480784732.txt.gz · Last modified: 2016/12/03 19:05 by laura.vasilescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0