低レイヤー勉強会 第 4 回 - 個人メモ

hackermeshi.com

コンピュータの5大要素

  • 入力装置
  • 演算装置
  • 制御装置
  • 記憶装置
  • 出力装置

CPU を構成する4大構成

クロックとは

  • CPU が動作するタイミングとなるクロック信号を発生させる
  • 電気信号がバラバラだとCPUの効率が落ちるため

レジスタ

  • 処理対象となる命令やデータを一時的に格納する場所
  • プログラムはレジスタを対象に記述される

マシン語

  • CPUが解釈・実行できる2進数の言語

プログラムに関して

  • 実行時のプログラムはメモリに格納される
  • プログラムを解釈・実行するのはCPU
  • コードを書く上で意識しなくちゃいけないのはレジスタ

プログラムの流れ

  1. プログラムがコードを書く
  2. コードをビルドしてマシン後の実行ファイルを作る
  3. ハードディスクに保存されているマシン語のネイティブコードのコピーがメモリに保存される
  4. CPU がメモリ上のネイティブコードを解釈して命令を実行する

ウォーミングアップ

ハンズオン

  • サンプルプログラムを作成
#include <stdio.h>

int main(void){
    int a = 1;
    int b = 2;
    int c = a + b;
    printf("c = %d\n", c);
    return 0;
}
$ gcc -o test test.c
  • 実行
$ ./test       
c = 3
$ gcc -S test.c
 .section    __TEXT,__text,regular,pure_instructions
    .build_version macos, 12, 0 sdk_version 12, 1
    .globl  _main                           ; -- Begin function main
    .p2align    2
_main:                                  ; @main
    .cfi_startproc
; %bb.0:
    sub sp, sp, #48                     ; =48
    stp x29, x30, [sp, #32]             ; 16-byte Folded Spill
    add x29, sp, #32                    ; =32
    .cfi_def_cfa w29, 16
    .cfi_offset w30, -8
    .cfi_offset w29, -16
    mov w8, #0
    str w8, [sp, #12]                   ; 4-byte Folded Spill
    stur    wzr, [x29, #-4]
    mov w8, #1
    stur    w8, [x29, #-8]
    mov w8, #2
    stur    w8, [x29, #-12]
    ldur    w8, [x29, #-8]
    ldur    w9, [x29, #-12]
    add w8, w8, w9
    str w8, [sp, #16]
    ldr w9, [sp, #16]
                                        ; implicit-def: $x8
    mov x8, x9
    adrp    x0, l_.str@PAGE
    add x0, x0, l_.str@PAGEOFF
    mov x9, sp
    str x8, [x9]
    bl  _printf
    ldr w0, [sp, #12]                   ; 4-byte Folded Reload
    ldp x29, x30, [sp, #32]             ; 16-byte Folded Reload
    add sp, sp, #48                     ; =48
    ret
    .cfi_endproc
                                        ; -- End function
    .section    __TEXT,__cstring,cstring_literals
l_.str:                                 ; @.str
    .asciz  "c = %d\n"

.subsections_via_symbols
$ gcc -S -O3 test.c
 .section    __TEXT,__text,regular,pure_instructions
    .build_version macos, 12, 0 sdk_version 12, 1
    .globl  _main                           ; -- Begin function main
    .p2align    2
_main:                                  ; @main
    .cfi_startproc
; %bb.0:
    sub sp, sp, #32                     ; =32
    stp x29, x30, [sp, #16]             ; 16-byte Folded Spill
    add x29, sp, #16                    ; =16
    .cfi_def_cfa w29, 16
    .cfi_offset w30, -8
    .cfi_offset w29, -16
    mov w8, #3
    str x8, [sp]
Lloh0:
    adrp    x0, l_.str@PAGE
Lloh1:
    add x0, x0, l_.str@PAGEOFF
    bl  _printf
    mov w0, #0
    ldp x29, x30, [sp, #16]             ; 16-byte Folded Reload
    add sp, sp, #32                     ; =32
    ret
    .loh AdrpAdd    Lloh0, Lloh1
    .cfi_endproc
                                        ; -- End function
    .section    __TEXT,__cstring,cstring_literals
l_.str:                                 ; @.str
    .asciz  "c = %d\n"

.subsections_via_symbols
$ objdump -D test  

test:   file format mach-o arm64


Disassembly of section __TEXT,__text:

0000000100003f40 <_main>:
100003f40: ff c3 00 d1  sub sp, sp, #48
100003f44: fd 7b 02 a9  stp x29, x30, [sp, #32]
100003f48: fd 83 00 91  add x29, sp, #32
100003f4c: 08 00 80 52  mov w8, #0
100003f50: e8 0f 00 b9  str w8, [sp, #12]
100003f54: bf c3 1f b8  stur    wzr, [x29, #-4]
100003f58: 28 00 80 52  mov w8, #1
100003f5c: a8 83 1f b8  stur    w8, [x29, #-8]
100003f60: 48 00 80 52  mov w8, #2
100003f64: a8 43 1f b8  stur    w8, [x29, #-12]
100003f68: a8 83 5f b8  ldur    w8, [x29, #-8]
100003f6c: a9 43 5f b8  ldur    w9, [x29, #-12]
100003f70: 08 01 09 0b  add w8, w8, w9
100003f74: e8 13 00 b9  str w8, [sp, #16]
100003f78: e9 13 40 b9  ldr w9, [sp, #16]
100003f7c: e8 03 09 aa  mov x8, x9
100003f80: 00 00 00 90  adrp    x0, 0x100003000 <_main+0x40>
100003f84: 00 c0 3e 91  add x0, x0, #4016
100003f88: e9 03 00 91  mov x9, sp
100003f8c: 28 01 00 f9  str x8, [x9]
100003f90: 05 00 00 94  bl  0x100003fa4 <_printf+0x100003fa4>
100003f94: e0 0f 40 b9  ldr w0, [sp, #12]
100003f98: fd 7b 42 a9  ldp x29, x30, [sp, #32]
100003f9c: ff c3 00 91  add sp, sp, #48
100003fa0: c0 03 5f d6  ret

Disassembly of section __TEXT,__stubs:

0000000100003fa4 <__stubs>:
100003fa4: 10 00 00 b0  adrp    x16, 0x100004000 <__stubs+0x4>
100003fa8: 10 02 40 f9  ldr x16, [x16]
100003fac: 00 02 1f d6  br  x16

Disassembly of section __TEXT,__cstring:

0000000100003fb0 <__cstring>:
100003fb0: 63 20 3d 20  <unknown>
100003fb4: 25 64 0a 00  <unknown>

Disassembly of section __TEXT,__unwind_info:

0000000100003fb8 <__unwind_info>:
100003fb8: 01 00 00 00  udf #1
100003fbc: 1c 00 00 00  udf #28
100003fc0: 00 00 00 00  udf #0
100003fc4: 1c 00 00 00  udf #28
100003fc8: 00 00 00 00  udf #0
100003fcc: 1c 00 00 00  udf #28
100003fd0: 02 00 00 00  udf #2
100003fd4: 40 3f 00 00  udf #16192
100003fd8: 34 00 00 00  udf #52
100003fdc: 34 00 00 00  udf #52
100003fe0: a5 3f 00 00  udf #16293
100003fe4: 00 00 00 00  udf #0
100003fe8: 34 00 00 00  udf #52
100003fec: 03 00 00 00  udf #3
100003ff0: 0c 00 01 00  <unknown>
100003ff4: 10 00 01 00  <unknown>
100003ff8: 00 00 00 00  udf #0
100003ffc: 00 00 00 04  <unknown>

Disassembly of section __DATA_CONST,__got:

0000000100004000 <__got>:
100004000: 00 00 00 00  udf #0
100004004: 00 00 00 80  <unknown>