diff --git a/Writerside/images_architecture/a6-4-1.png b/Writerside/images_architecture/a6-4-1.png new file mode 100644 index 0000000..f388488 Binary files /dev/null and b/Writerside/images_architecture/a6-4-1.png differ diff --git a/Writerside/topics/Computer-Architecture.topic b/Writerside/topics/Computer-Architecture.topic index c001026..c2e501d 100644 --- a/Writerside/topics/Computer-Architecture.topic +++ b/Writerside/topics/Computer-Architecture.topic @@ -2546,7 +2546,7 @@ Conversion"/> movq Operand Combinations -

Simple Memory Addressing Mode

+

Memory Addressing Mode

  • Normal (R) => pointer dereferencing in C

    @@ -2557,9 +2557,208 @@ Conversion"/>
  • Displacement D(R) => accessing data at a fixed offset from a base address

    +

    movq 8(%rbp), %rdx equals:

    + + rdx = *(((char*)rbp) + 8) + +
  • +
  • +

    Complete Mode: D(Rb,Ri,S)

    + +
  • +

    D: Constant "displacement" 1, 2, or 4 bytes

    +
  • +
  • +

    Rb: Base register: Any of 16 integer registers

    +
  • +
  • +

    Ri: Index register: Any, except for %rsp

    +
  • +
  • +

    S: Scale: 1, 2, 4, or 8

    +
  • +
    +

    Example: movl (%rbx,%rdi,4), %eax

    + +
  • +

    Calculate the address: %rbx + (4 * %rdi) (e.g., base address + 4 * 5 = base address + + 20).

    +
  • +
  • +

    Access the 32-bit value at that calculated address.

    +
  • +
  • +

    Move the retrieved value into the %eax register.

    +
  • +
    + +

    leaq src, dst

    + + leaq (%rdi,%rdi,2), %rax ; Calculate the address of the element at index rdx in an array of integers pointed to by rsi (assuming 4 bytes per integer) + +

    It can also be used for arithmetic expressions.

    + + leaq (%rdi,%rdi,2), %rax ; Calculate rax = rdi + (rdi * 2) = 3 * rdi + +
    + +

    Two Operand Instructions:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    FormatComputation
    addq src, destdest = dest + src
    subq src, destdest = dest - src
    imulq src, destdest = dest * src
    salq src, destdest = dest << src
    sarq src, dest (Arithmetic Shift)dest = dest >> src
    shrq src, dest (Logical Shift)dest = dest >> src
    xorq src, destdest = dest ^ src
    andq src, destdest = dest & src
    orq src, destdest = dest | src
    + +

    Watch out for the argument order!

    +

    Intel docs use "op dest, src"!

    +
    +

    One Operand Instructions:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    FormatComputation
    incq destdest = dest + 1
    decq src, destdest = dest - 1
    negq src, destdest = - dest
    notq destdest = ~ dest
    sarq src, dest (Arithmetic Shift)dest = dest >> src
    shrq src, dest (Logical Shift)dest = dest >> src
    xorq src, destdest = dest ^ src
    andq src, destdest = dest & src
    orq src, destdest = dest | src
    +

    Example

    + + long arith(long x, long y, long z) { + long t1 = x + y; + long t2 = z + t1; + long t3 = x + 4; + long t4 = y * 48; + long t5 = t3 + t4; + long rval = t2 * t5; + return rval; + } + + + 0000000000001149 <arith>: + 1149: f3 0f 1e fa endbr64 + 114d: 55 push %rbp + 114e: 48 89 e5 mov %rsp,%rbp + 1151: 48 89 7d c8 mov %rdi,-0x38(%rbp) # Store x on the stack + 1155: 48 89 75 c0 mov %rsi,-0x40(%rbp) # Store y on the stack + 1159: 48 89 55 b8 mov %rdx,-0x48(%rbp) # Store z on the stack + 115d: 48 8b 55 c8 mov -0x38(%rbp),%rdx # Load x into rdx + 1161: 48 8b 45 c0 mov -0x40(%rbp),%rax # Load y into rax + 1165: 48 01 d0 add %rdx,%rax # t1 = x + y + 1168: 48 89 45 d0 mov %rax,-0x30(%rbp) # Store t1 on the stack + 116c: 48 8b 55 b8 mov -0x48(%rbp),%rdx # Load z into rdx + 1170: 48 8b 45 d0 mov -0x30(%rbp),%rax # Load t1 into rax + 1174: 48 01 d0 add %rdx,%rax # t2 = z + t1 + 1177: 48 89 45 d8 mov %rax,-0x28(%rbp) # Store t2 on the stack + 117b: 48 8b 45 c8 mov -0x38(%rbp),%rax # Load x into rax + 117f: 48 83 c0 04 add $0x4,%rax # t3 = x + 4 + 1183: 48 89 45 e0 mov %rax,-0x20(%rbp) # Store t3 on the stack + 1187: 48 8b 55 c0 mov -0x40(%rbp),%rdx # Load y into rdx + 118b: 48 89 d0 mov %rdx,%rax # Copy y into rax (for multiplication) + 118e: 48 01 c0 add %rax,%rax # Multiply y by 2 (left shift) + 1191: 48 01 d0 add %rdx,%rax # Add y (original value) so now its 3y + 1194: 48 c1 e0 04 shl $0x4,%rax # Multiply by 16 (left shift 4) 3y*16 = 48y + 1198: 48 89 45 e8 mov %rax,-0x18(%rbp) # Store t4 = 48 * y on the stack + 119c: 48 8b 55 e0 mov -0x20(%rbp),%rdx # Load t3 into rdx + 11a0: 48 8b 45 e8 mov -0x18(%rbp),%rax # Load t4 into rax + 11a4: 48 01 d0 add %rdx,%rax # t5 = t3 + t4 + 11a7: 48 89 45 f0 mov %rax,-0x10(%rbp) # Store t5 on the stack + 11ab: 48 8b 45 d8 mov -0x28(%rbp),%rax # Load t2 into rax + 11af: 48 0f af 45 f0 imul -0x10(%rbp),%rax # rval = t2 * t5 + 11b4: 48 89 45 f8 mov %rax,-0x8(%rbp) # Store rval on the stack + 11b8: 48 8b 45 f8 mov -0x8(%rbp),%rax # Move rval into rax (return value) + 11bc: 5d pop %rbp # Restore the caller's base pointer + 11bd: c3 ret # Return from the function + +
    + + +

    Compile with the following code (debugging-friendly):

    + + gcc –Og p1.c -o p + + C, Assembly & Machine Code +

    Use the following command to generate the assembly code:

    + + gcc –Og –S p1.c + +

    Use the following command to disassembly the machine code:

    + + objdump –d p1 +