Lecture 26 - x86-64

Goals

  • Look at how functions work on the x86-64 architecture

x86

Before we go any further, I want to bring us back to x86 assembly. I want to get us to the point where we can use assembly as a debugging tool, so I want you to start getting familiar with the dialect that is actually used for our machine.

See the slides for an overview of the registers, addressing modes, and instructions

Let's take a look at our last example again -- this time in x86

0000000000000000 <f>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d ec                mov    %edi,-0x14(%rbp)
   7:   89 75 e8                mov    %esi,-0x18(%rbp)
   a:   89 55 e4                mov    %edx,-0x1c(%rbp)
   d:   89 4d e0                mov    %ecx,-0x20(%rbp)
  10:   44 89 45 dc             mov    %r8d,-0x24(%rbp)
  14:   44 89 4d d8             mov    %r9d,-0x28(%rbp)
  18:   8b 55 ec                mov    -0x14(%rbp),%edx
  1b:   8b 45 e8                mov    -0x18(%rbp),%eax
  1e:   01 c2                   add    %eax,%edx
  20:   8b 45 e4                mov    -0x1c(%rbp),%eax
  23:   01 c2                   add    %eax,%edx
  25:   8b 45 e0                mov    -0x20(%rbp),%eax
  28:   01 c2                   add    %eax,%edx
  2a:   8b 45 dc                mov    -0x24(%rbp),%eax
  2d:   01 c2                   add    %eax,%edx
  2f:   8b 45 d8                mov    -0x28(%rbp),%eax
  32:   01 c2                   add    %eax,%edx
  34:   8b 45 10                mov    0x10(%rbp),%eax
  37:   01 c2                   add    %eax,%edx
  39:   8b 45 18                mov    0x18(%rbp),%eax
  3c:   01 d0                   add    %edx,%eax
  3e:   89 45 fc                mov    %eax,-0x4(%rbp)
  41:   8b 45 fc                mov    -0x4(%rbp),%eax
  44:   5d                      pop    %rbp
  45:   c3                      ret

0000000000000046 <main>:
  46:   55                      push   %rbp
  47:   48 89 e5                mov    %rsp,%rbp
  4a:   48 83 ec 20             sub    $0x20,%rsp
  4e:   89 7d ec                mov    %edi,-0x14(%rbp)
  51:   48 89 75 e0             mov    %rsi,-0x20(%rbp)
  55:   6a 07                   push   $0x7
  57:   6a 06                   push   $0x6
  59:   41 b9 05 00 00 00       mov    $0x5,%r9d
  5f:   41 b8 04 00 00 00       mov    $0x4,%r8d
  65:   b9 03 00 00 00          mov    $0x3,%ecx
  6a:   ba 02 00 00 00          mov    $0x2,%edx
  6f:   be 01 00 00 00          mov    $0x1,%esi
  74:   bf 00 00 00 00          mov    $0x0,%edi
  79:   e8 00 00 00 00          call   7e <main+0x38>
  7e:   48 83 c4 10             add    $0x10,%rsp
  82:   89 45 fc                mov    %eax,-0x4(%rbp)
  85:   8b 45 fc                mov    -0x4(%rbp),%eax
  88:   c9                      leave
  89:   c3                      ret

We can see that it pretty much follows the same pattern as the ARM assembly did

Some differences beyond syntax and instructions:

  • there is no LR. call always pushes the return address on the stack and ret always pops it
  • for f, the compiler didn't both updating the stack pointer
  • x86 can handle six parameters in registers before resorting to memory

Mechanical level

vocabulary

Skills


Last updated 05/12/2023