어셈 초보 질문..

imcjswo의 이미지

(gdb) disas main
Dump of assembler code for function main:
0x080482f4 <main+0>:    push   %ebp
0x080482f5 <main+1>:    mov    %esp,%ebp
0x080482f7 <main+3>:    sub    $0x8,%esp
0x080482fa <main+6>:    and    $0xfffffff0,%esp
0x080482fd <main+9>:    mov    $0x0,%eax
0x08048302 <main+14>:   sub    %eax,%esp
0x08048304 <main+16>:   mov    $0x0,%eax
0x08048309 <main+21>:   leave
0x0804830a <main+22>:   ret
0x0804830b <main+23>:   nop
End of assembler dump.

위 코드는

int main()
{
        int i;
        return 0;
}

를 디어셈 한건데요..

0x080482fd <main+9>:    mov    $0x0,%eax
0x08048302 <main+14>:   sub    %eax,%esp

이부분이 왜 필요한건지 궁금합니다.

더불어 소스중 스택의 로컬변수의 스택 크기를 잡는 부분

0x080482f7 <main+3>: sub $0x8,%esp

에서 ESP에서 -8 부분에 int i 가 위치하는게 맞는지 궁금하네요
곧 그말은 &i 에서 12(8+ebp)를 더하면 ret 위치인데...
제 생각이 맞는건지 답변 주시면 감사하겠습니다.

jyj9782의 이미지

0 -> eax
eax = esp-eax
0 -> eax

일단 기본적으로 eax 는 함수반환값인데, 저도..

esp-eax 하는 부분은 왜 하는지 잘모르겠네요.

GCC.. 2.96 버전쯤에는 없는거 같은데, 아시는분있으면 알려주세요.

Quote:

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)

$ gdb -q x
(gdb) disass main
Dump of assembler code for function main:
0x80483d0 <main>: push %ebp
0x80483d1 <main+1>: mov %esp,%ebp
0x80483d3 <main+3>: sub $0x4,%esp
0x80483d6 <main+6>: mov $0x0,%eax
0x80483db <main+11>: leave
0x80483dc <main+12>: ret
0x80483dd <main+13>: lea 0x0(%esi),%esi
End of assembler dump.
(gdb)

맞죠?

그런거 없고, eax 에 0을 넣음으로써 반환값에 0을 넣는..

하죠?

3.x 버전에서 생긴거같다니까요.

그리고 전 궁금한게 ret 뒤에는 저 lea 같은 코드는 왜 있나요?
그기서 끝날텐데.. 왜 넣어진거죠?

힘내세요.

dalekang의 이미지

>> 그리고 전 궁금한게 ret 뒤에는 저 lea 같은 코드는 왜 있나요?
>> 그기서 끝날텐데.. 왜 넣어진거죠?

실제 실행 코드가 아니라 gdb 에서 보이는 자리 맞추기 코드 입니다.
프로세서 별로 비트 정렬이 있는데 그 것을 위한 코드 입니다.

ret 가 끝 맞습니다.

jyj9782의 이미지

저도 그렇게 생각했지만..

그렇디만 lea 같은 코드는 의미가 없는건가요? 비트 정렬을 위한용도외엔..

조금만 더 자세히 알려주실래요?

dalekang wrote:
>> 그리고 전 궁금한게 ret 뒤에는 저 lea 같은 코드는 왜 있나요?
>> 그기서 끝날텐데.. 왜 넣어진거죠?

실제 실행 코드가 아니라 gdb 에서 보이는 자리 맞추기 코드 입니다.
프로세서 별로 비트 정렬이 있는데 그 것을 위한 코드 입니다.

ret 가 끝 맞습니다.

힘내세요.

feanor의 이미지

네, 자리 맞추기 외에는 아무런 의미도 없습니다.

--feanor

sangwoo의 이미지

-O 옵션을 사용하여 컴파일하신 후, disassemble 해보세요.
(제 생각에는) 쓸데없는 operation들이 많이 줄어듭니다.

----
Let's shut up and code.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.