4
시시시 시시시시시 시시 ASSEMBLY 1

시스템 프로그래밍 실습

  • Upload
    avak

  • View
    33

  • Download
    4

Embed Size (px)

DESCRIPTION

시스템 프로그래밍 실습. ASSEMBLY 1. C 코드와 ASSEMBLY 코드의 상호 참조. EXAMPLE 1. ( c_source.c ) #include #include int main(int argc, char* argv[]) { int num, exp, res; num = atoi(argv[1]); exp = atoi(argv[2]); num = myfunc(num, exp); - PowerPoint PPT Presentation

Citation preview

Page 1: 시스템 프로그래밍 실습

시스템 프로그래밍 실습

ASSEMBLY 1

Page 2: 시스템 프로그래밍 실습

EXAMPLE 1• C 코드와 ASSEMBLY 코드의 상호 참조

( c_source.c )

#include <stdio.h>#include <stdlib.h>

int main(int argc, char* argv[]){

int num, exp, res;num = atoi(argv[1]);exp = atoi(argv[2]);num = myfunc(num, exp);printf(“%d exp %d = %d\n”, num,

exp, res);return 0;

}

int print(int num, int exp, int res){

printf(“%d exp %d = %d\n”, num, exp, res);

return 0;}

Page 3: 시스템 프로그래밍 실습

EXAMPLE 1( asm_source.s )

.globl myfuncmyfunc:

movl 4(%esp),%eaxmovl 8(%esp),%ebxmovl $1, %ecxmovl $1, %edx

L1: cmpl %edx, %ebxje L2imull %eax, %ecxpushl %ecxpushl %edxpushl %eaxcall printpopl %eaxpopl %edxpopl %ecxincl %edxjmp L1

L2: imull %eax, %ecxmovl %ecx, %eaxret

컴파일 방법gcc –o ex1 c_sources.c asm_source.s

Page 4: 시스템 프로그래밍 실습

EXAMPLE 2• INLINE ASSEMBLY

( c_source.c )

int iasm_test_func(int arg);void print(char* str, int num){

printf(“STRING : %s NUMBER : %d\n”, str, num);}static void __iasm_func_dummy(void){

__asm__ __volatile__(“.globl iasm_test_func

\n\t”“iasm_test_func: \n\t”“pushl 4(%%esp) \n\t”“pushl %0 \n\t”“call print \n\t”“addl $8, %%esp \n\t”“movl 4(%%esp), %

%eax \n\t”“ret “:: “I” (“hello world..”)

);}

int main(){

int ret;ret = iasm_test_func(10);printf(“RETURN = %d\n”,

ret);return 0;

}