written by yechoi

[어셈블리] AMD64 함수호출규약(calling convention) 본문

Born 2 Code/Assembly

[어셈블리] AMD64 함수호출규약(calling convention)

yechoi 2020. 6. 7. 14:02
반응형

 

함수호출규약은 함수를 호출하는 방식이다. 즉, 어떻게 리턴하고, 어떻게 지워지는 등을 결정하는 규약.

macos를 포함한 system V AMD64에서는 스택 클린업을 caller가 담당한다.

이때, 스택은 16바이트로 정렬돼야 한다.

함수를 호출할 때 돌아가기 위한 주소 8바이트가 쌓이므로, 함수 호출 전에 미리 8바이트를 쌓아 16바이트 정렬을 해준다.

sub rsp, 8
call _ft_strcpy
add rsp, 8

이런식으로. 이밖에도 push를 이용하면 이처럼 8바이트를 미리 쌓는 효과를 볼 수 있다.

push rdi
call _ft_strlen

rdi 레지스터 값을 백업할 목적으로 push를 해주었더니, 다음에 호출된 ft_strlen 함수가 rsp를 옮겨주지 않고도 정상 작동했다.

 

 

x86 calling conventions - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search This article describes the calling conventions used when programming x86 architecture microprocessors. Calling conventions describe the interface of called code: The order in which ato

en.wikipedia.org

 

반응형