어셈블 과정에서 더미코드들이 생겨나는 이유

eternalklaus의 이미지

어셈블 과정에서 어셈블러가 더미코드들을 추가하는데 그 이유가 뭘까요?
실험내용은 아래와 같습니다

a.s --assemble--> 바이너리 파일 --disassemble--> b.s

최종 디스어셈블리인 b.s를 보면, ( 제가 a.s에 추가하지 않은) 더미 인스트럭션들이 종종 보이는데 이유를 모르겠습니다.

즉, 어떤 상황이냐면,

.global main
main:
 push   %eax
 pop    %eax
 jmp    80483f1
 
LABLE1:
 mov    %eax,%ebx

a.s

위의 파일을 어셈블한 뒤에, Objdump 명령어로 디스어셈블리를 뽑아내면 아래처럼 됩니다.

080483ed <main>:
 80483ed:       50                      push   %eax
 80483ee:       58                      pop    %eax
 80483ef:       e9 fd ff ff ff          jmp    80483f1 <main+0x4>
 
080483f4 <LABLE1>:
 80483f4:       89 c3                   mov    %eax,%ebx
 80483f6:       66 90                   xchg   %ax,%ax
 80483f8:       66 90                   xchg   %ax,%ax
 80483fa:       66 90                   xchg   %ax,%ax
 80483fc:       66 90                   xchg   %ax,%ax
 80483fe:       66 90                   xchg   %ax,%ax
 8048400:       66 90                   xchg   %ax,%ax
 8048402:       90                      nop

쓸모없는 더미코드들 xchg %ax,%ax 이 추가되어 있는걸 확인할 수 있는데요.

어셈블러가 이런 쓸데없는(?)짓을 하는 이유가 뭘까요?

(퍼포먼스 측면에서나, 사이즈 측면에서나 좋을것 없을 텐데요...)

cinsk의 이미지

여유가 있다면 http://www.agner.org/optimize/optimizing_assembly.pdf 를 읽어보기 바랍니다. (Section 11.5)

좀 더 여유가 있다며, "Intel 64 and IA-32 Architectures Optimization Reference Manual"도 좋습니다. (https://software.intel.com/en-us/articles/intel-sdm 에 있음), Section 3.4.1.5, 3.5.1.10

위 질문에 대한 답변은 일반적인게 아니라 x86 CPU에 한정된 것이며,

간단히 말해, branch target (jump할 대상 label)은 16 byte 단위로 align되어 있어야, CPU가 효과적으로 instruction들을 fetch할 수 있습니다. 그래서 일부러 16 byte align을 위해 쓸모없는? padding을 집어 넣은 것이고, 이 padding은 주로 nop이나, 기타 nop에 해당하는 instruction들로 채워집니다. Intel 64 and IA-32 Architectures Optimization Reference Manual, Section 3.5.1.10을 보면 nop에 해당하는 instruction들의 목록을 볼 수 있습니다.

1-byte: XCHG EAX, EAX
2-byte: 66 NOP
3-byte: LEA REG, 0 (REG) (8-bit displacement)
4-byte: NOP DWORD PTR [EAX + 0] (8-bit displacement)
5-byte: NOP DWORD PTR [EAX + EAX*1 + 0] (8-bit displacement) 6-byte: LEA REG, 0 (REG) (32-bit displacement)
7-byte: NOP DWORD PTR [EAX + 0] (32-bit displacement)
8-byte: NOP DWORD PTR [EAX + EAX*1 + 0] (32-bit displacement) 9-byte: NOP WORD PTR [EAX + EAX*1 + 0] (32-bit displacement)

eternalklaus의 이미지

아하~ 최적화를 위한 것이였군요. 유용한 답변 정말 감사합니다.^^

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.