function 에서 참조인자와 반환인자에 따라서 ..어셈블리가 다륵

catzbi의 이미지

// test.cpp
#include  <stdio.h>

int nothingR ( int& a ) { 
	return a; 
}
int nothingP ( int* p ){ 
	return *p; 
}
int& nothingRR ( int& a ) { 
	return a; 
}
int* nothingPP ( int* p ) { 
       return p; 
}       

int main() { 
	int a =1 ; 
	nothingR ( a ) ; 
	nothingP ( &a ) ;
	nothingRR (a );
	nothingPP ( &a ) ;
	
return 0 ; 

}

// test.s sh:compiled> g++ -S tets.cpp

	.file	"t.cpp"
	.version	"01.01"
gcc2_compiled.:
.text
	.align 4
.globl nothingR__FRi
	.type	 nothingR__FRi,@function
nothingR__FRi:
.LFB1:
	pushl	%ebp
.LCFI0:
	movl	%esp, %ebp
.LCFI1:
	movl	8(%ebp), %eax
	movl	(%eax), %eax
	movl	%eax, %eax
	popl	%ebp
	ret
.LFE1:
.Lfe1:
	.size	 nothingR__FRi,.Lfe1-nothingR__FRi
	.align 4
.globl nothingP__FPi
	.type	 nothingP__FPi,@function
nothingP__FPi:
.LFB2:
	pushl	%ebp
.LCFI2:
	movl	%esp, %ebp
.LCFI3:
	movl	8(%ebp), %eax
	movl	(%eax), %eax
	movl	%eax, %eax
	popl	%ebp
	ret
.LFE2:
.Lfe2:
	.size	 nothingP__FPi,.Lfe2-nothingP__FPi
	.align 4
.globl nothingRR__FRi
	.type	 nothingRR__FRi,@function
nothingRR__FRi:
.LFB3:
	pushl	%ebp
.LCFI4:
	movl	%esp, %ebp
.LCFI5:
	movl	8(%ebp), %eax
	movl	%eax, %eax
	popl	%ebp
	ret
.LFE3:
.Lfe3:
	.size	 nothingRR__FRi,.Lfe3-nothingRR__FRi
	.align 4
.globl nothingPP__FPi
	.type	 nothingPP__FPi,@function
nothingPP__FPi:
.LFB4:
	pushl	%ebp
.LCFI6:
	movl	%esp, %ebp
.LCFI7:
	movl	8(%ebp), %eax
	movl	%eax, %eax
	popl	%ebp
	ret
.LFE4:
.Lfe4:
	.size	 nothingPP__FPi,.Lfe4-nothingPP__FPi
	.align 4
.globl main
	.type	 main,@function
main:
.LFB5:
	pushl	%ebp
.LCFI8:
	movl	%esp, %ebp
.LCFI9:
	subl	$8, %esp
.LCFI10:
	movl	$1, -4(%ebp)
	subl	$12, %esp
	leal	-4(%ebp), %eax
	pushl	%eax
.LCFI11:
	call	nothingR__FRi
	addl	$16, %esp
	subl	$12, %esp
	leal	-4(%ebp), %eax
	pushl	%eax
	call	nothingP__FPi
	addl	$16, %esp
	subl	$12, %esp
	leal	-4(%ebp), %eax
	pushl	%eax
	call	nothingRR__FRi
	addl	$16, %esp
	subl	$12, %esp
	leal	-4(%ebp), %eax
	pushl	%eax
	call	nothingPP__FPi
	addl	$16, %esp
	movl	$0, %eax
	leave
	ret
.LFE5:
.Lfe5:
	.size	 main,.Lfe5-main
	.ident	"GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.2 2.96-112.7.2)"

참조, 포인터 입력과 출력에 관한 부분이 궁금했습니다. 우선은 올려놓겠습니당

mov (%eax ) , %eax ; 이렇게 되어있는데요..
대충은 감이 오는데 그부분만 설명 부닥 드립니다.

alsong의 이미지

Assembly Example (이호)
리눅스에서 사용하는 어셈블리 문법에 대해서 (특히 x86에서) 간략히 요약한 글입니다

Quote:
메모리 참조 : AT&T와 Intel에서 메모리 주소를 참조하는 방법(indexing, indirection)에는 표기법상 약간의 차이가 있다.
AT&T : section:immed32(base, index, scale)
Intel : sectioin:[base + index * scale + immed32]

이는 base + index * scale + immed32 주소를 나타내게 된다. 이들 모두를 반드시 지정해야 하는 것은 아니지만 immed32나 base 중의 하나는 반드시 지정해야 한다. 주소 지정의 예를 들어보자. eax 레지스터가 가리키는 주소의 값을 참조하는 경우 :

AT&T : (%eax)
Intel : [eax]

레지스터에 변수(var)의 옵셋을 합한 주소를 참조하는 경우 :

AT&T : var(%eax)
Intel : [eax + var]

4바이트 단위로 된 정수 배열(int array[])의 eax번째 값을 참조하는 경우 (int array[eax]) :

AT&T : array(, %eax, 4)
Intel : [eax * 4 + array]

위 배열에서 ebx 인덱스에서 시작하여 eax번째 값을 참조하는 경우 (int array[ebx + eax]) :

AT&T : array(%ebx, %eax, 4)
Intel : [ebx + eax * 4 + array]

그나저나 백수 언제 탈출하냐... ㅡㅡ; 배고파라.