MINZKN의 블로그 http://www.minzkn.com/
BLOG main image

전체 (421)
minzkn™ (63)
Programming (276)
사용법/팁 (26)
타국에서 (15)
사진 (1)
퍼온글 (39)
조사 (0)
낙서장 (0)
서식/Template (0)
비밀글 (0)
Total : 270632
Today : 100
Yesterday : 188
6명이 RSS를 구독하고 있습니다.
Startup code for linux
Programming/Unix/Linux | 2007/05/07 11:02
2007/05/07 11:02 2007/05/07 11:02
span class=postbody리눅스에서의 argument 전달의 원리를 이해하기 위한 예제로 만들었던 겁니다. br / br / 이 소스는 Linux에서 간략하게 구현된 startup코드입니다. br / 이것은 인자를 받기 위해서 DOS처럼 공백을 구분해서 만들어야 하는 br / 불편함이 없습니다. br / Linux에서는 프로그램이 구동되면 마치 하나의 함수를 호출한것같이 br / 스택에 해당 PSP(Linux에서도 이렇게 부르는지는 잘 모르겠네요.)를 br / 넘겨주게 됩니다. 때문에 DOS 보다는 상당히 편하게 사용할수 있습니다. br / 그리고 아래의 예제에서 env는 인자로 넘겨주지 않았습니다. br / 사실 저는 쓸일이 없어서 예제를 이따위로 만들어 놓았습니다. br / 너그럽게 용서하시길 바라면서 다음과 같이 예제를 올려봅니다. br / br / Startup 코드는 Linux에서 C를 할때 링크과정에서 항상 포함되는 br / crtlt;0..1gt;.o 를 말합니다. br / 즉, 이것이 하는 일은 프로그램이 기동되고 몇가지 기초과정 br / (아래에서는 빠졌지만 이것보다 훨씬 많은 일을 해야 합니다.)를 br / 초銹?하고 마지막으로 main을 호출하게 됩니다. br / 그리고 main에서 return되었을때 exit(0)함수를 호출하는 것과 같은 br / 역할의 코드를 수행해주도록 합니다. br / br / /spantable align=center border=0 cellpadding=3 cellspacing=1 width=90%tbodytr tdspan class=genmedb코드:/b/span/td /tr tr td class=code br / # br / # [ GPL ] br / # br / # File : mzstartup.c br / # Code : JaeHyuk Cho lt;minzkn@infoeq.comgt; br / # Date : 2002.12.23 MON br / br / .code32 br / br / .globl mzstartup /* 외부(Linker)에서 Startup entry를 기본값으로 _start를 호출하게 됩니다. 하지만 이 예제에서는 mzstartup이라는 라벨을 br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; 개인적인 편의로 사용하는것으로 Linker에 의해서 Link과정을 수행할때 이 라벨을 지정해줘야 합니다. */ br / br / /* Global로 이 변수를 갖게 하여 굳이 main함수 내에서만 참조할수 있는것이 아니고 다른 함수에서도 이 전역변수를 br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;통해서 접근할수 있게 편의상 public으로 선언합니다. */ br / .globl g_Argc br / .globl g_Argv br / br / /* 이것이 외부참조되는 C의 main함수를 가르킵니다. C++의 경우는 _main이 될수도 있고 __main도 될수 있는데 이것은 따로 br / nbsp; nbsp;알아보시기 바랍니다. (컴파일러마다 다를수 있다는 예기) */ br / .extern main br / br / .org 0x0000 br / mzstartup: br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;/* 이 시점부터 스택에 초기화 되어 있는 상태이고 마치 mzstartup부분을 호출해준것과 같은 스택 구조를 갖게 됩니다. br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; 그래서 일반적은 스택프레임을 다루듯 argc, argv를 복사하여 가져오게 되면 됩니다. */ br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;pushl %ebp br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;movl %esp, %ebp br / br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;leal 0x0008(%ebp), %eax br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;movl %eax, g_Argv br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;pushl %eax br / br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;movl 0x0004(%ebp), %eax br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;movl %eax, g_Argc br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;pushl %eax br / br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;/* Register 초기화 : 꼭 필요한 과정은 아니지만 간혹 문제(Linux의 버그를 말하는 것이 아님)를 발생할수도 있어 초기화를 수행합니다. */ br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %eax, %eax br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %ebx, %ebx br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %ecx, %ecx br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %edx, %edx br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %ebp, %ebp br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %esi, %esi br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;xorl %edi, %edi br / br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;call main br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;addl $4+4, %esp br / br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;movl %eax, %ebx br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;movl $1, %eax br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;int $0x80 br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;/* 이 이후의 코드가 실행된다면 이것은 Linux kernel이 정상이 아닙니다. 즉, 이 이후에 나오는 명령은 의미가 크게 없습니다. */ br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;hlt br / br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;popl %ebp br / nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;ret br / br / .data br / g_Argc:nbsp; nbsp; nbsp; nbsp; .long 0 br / g_Argv:nbsp; nbsp; nbsp; nbsp; .long 0 br / br / # End of source /td/tr/tbody/table
크리에이티브 커먼즈 라이센스
Creative Commons License

태그 : , , , ,
트랙백0 | 댓글0
이 글의 관련글(트랙백) 주소 :: http://blog.minzkn.com/trackback/267

이름 :
비밀번호 :
홈페이지 :
  비밀글로 등록
내용 :
 



[PREV] [1] ... [310][311][312][313][314][315][316][317][318] ... [421] [NEXT]
위치로그 : 태그 : 방명록 : 관리자 : rss
장인정신’s Blog is powered by Tattertools.com / Designed by Lefttoe.Net
네. 이제 결혼했습니다.
  06/27 - 장인정신
네. 거기 맞아요. 그때 만나...
  06/27 - 장인정신
결혼하시나봐요~ 축하드립니다~
  06/25 - MasterQ
저희집 바로 옆으로 지나가셨...
  06/25 - MasterQ
아우~ 정말 소중한 자료 감사...
  03/09 - 착한청년
신기전
Savvy의 블로그
network+is+unreachable-으로...
blogring.org
서버의 Local IP 가져오기, 단상
엘레노아의 작업로그

Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.