'symbol'에 해당되는 글 1건

test.c 에 다음과 같은 함수가 있습니다.
코드:
int a(void)
{
 return(0);
}


test.s 에서 어셈블리로 a함수를 호출하려면 어떻게 해야 할까요?

1번:
코드:
...
call a
...


2번:
코드:
...
call _a
...


오랜기간 어셈블리를 접해보지 않으신분이라면 2번의 경우는 말도 안된다고 예기할수도 있을법합니다. 왜 2번 보기를 보여야 하는지는 설명하지 않겠습니다.

간략히 이를 매우 명확히 명시하기 위해서 다음과 같이 하는 경우를 보셨을겁니다.

test.c
코드:
int a(void) __asm__("myasm_func_a");

int a(void)
{
 return(0);
}


test.s
코드:
...
call myasm_func_a
...





이것은 함수에만 해당하는 경우가 아닙니다. 아래는 이를 종합적으로 표현하고자 만들어본 예제입니다. 이것은 매우 유용한것이니 꼭 모두 한번쯤 직접 해보시기를 권합니다.
혹시나 하는 마음에 예기하지만 이것은 Linux환경에서 gcc, gas를 사용한 예제입니다.

test.c
코드:
#include <stdio.h>
int g_my_val __asm__("myasm_dword_val") = 0;
extern void test_test_test(void) __asm__("test");
int a(void) __asm__("myasm_func_a");
int a(void)
{
 (void)fprintf(stdout, "hello %#x\n", g_my_val);
 return(0);
}
int main(void)
{
 test_test_test();
 return(0);
}


asm.s
코드:
.code32
.extern myasm_func_a
.extern myasm_dword_val
.text
test:
.globl test
movl $0x12345678, myasm_dword_val
call myasm_func_a
ret
# End of source


Makefile
코드:
all: test
clean: ; rm -f test test.o asm.o
test: test.o asm.o ; gcc -o $@ $^
test.o: test.c ; gcc -Wall -Werror -c -o $@ $<
asm.o: asm.s ; as -o $@ $<

크리에이티브 커먼즈 라이센스
Creative Commons License
Posted by minzkn

트랙백 주소 :: http://blog.minzkn.com/trackback/139

댓글을 달아 주세요