コンピューター理論、チョットデキルようになりたい

コンピュータサイエンスの学び直しです

学び直し六日目:2章 ブール算術②

記録

  • 半加算器、全加算器、加算器、インクリメンタ、ALU 仕様確認。
  • 半加算器、全加算器、加算器、インクリメンタの実装。

半加算器

真理値表を見たら、Xor と And で書ける。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/02/HalfAdder.hdl

/**
 * Computes the sum of two bits.
 */

CHIP HalfAdder {
    IN a, b;    // 1-bit inputs
    OUT sum,    // Right bit of a + b 
        carry;  // Left bit of a + b

    PARTS:
    // Put you code here:
    Xor(a=a, b=b, out=sum);
    And(a=a, b=b, out=carry);
}

全加算器

半加算器2つ。Or 1つで書ける。

Or を使う直感的な理解としては、3つのビットの和を求める場合、桁上がりは最多で1回しか起こらない。たとえば、a+b のとき桁上りがあれば、sum1(= a+b) + c のとき桁上りは発生しない。

(2020/1/22修正)最後のOrの Out をつなぐ先が間違えていたので修正。このおかげで ALU の実装でエラーが取れず2時間くらいうなっていた。。。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/02/FullAdder.hdl

/**
 * Computes the sum of three bits.
 */

CHIP FullAdder {
    IN a, b, c;  // 1-bit inputs
    OUT sum,     // Right bit of a + b + c
        carry;   // Left bit of a + b + c

    PARTS:
    // Put you code here:
    HalfAdder(a=a, b=b, sum=s1, carry=c1);
    HalfAdder(a=c, b=s1, sum=sum, carry=c2);
    Or(a=c1, b=c2, out=carry);
}

加算器

LSB だけ桁上りを受ける必要はないので半加算器で。ほかは全加算器でビットごとに和を求める。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/02/Adder16.hdl

/**
 * Adds two 16-bit values.
 * The most significant carry bit is ignored.
 */

CHIP Add16 {
    IN a[16], b[16];
    OUT out[16];

    PARTS:
   // Put you code here:
    HalfAdder(a=a[0], b=b[0], sum=out[0], carry=c0);
    FullAdder(a=a[1], b=b[1], c=c0, sum=out[1], carry=c1);
    FullAdder(a=a[2], b=b[2], c=c1, sum=out[2], carry=c2);
    FullAdder(a=a[3], b=b[3], c=c2, sum=out[3], carry=c3);
    FullAdder(a=a[4], b=b[4], c=c3, sum=out[4], carry=c4);
    FullAdder(a=a[5], b=b[5], c=c4, sum=out[5], carry=c5);
    FullAdder(a=a[6], b=b[6], c=c5, sum=out[6], carry=c6);
    FullAdder(a=a[7], b=b[7], c=c6, sum=out[7], carry=c7);
    FullAdder(a=a[8], b=b[8], c=c7, sum=out[8], carry=c8);
    FullAdder(a=a[9], b=b[9], c=c8, sum=out[9], carry=c9);
    FullAdder(a=a[10], b=b[10], c=c9, sum=out[10], carry=c10);
    FullAdder(a=a[11], b=b[11], c=c10, sum=out[11], carry=c11);
    FullAdder(a=a[12], b=b[12], c=c11, sum=out[12], carry=c12);
    FullAdder(a=a[13], b=b[13], c=c12, sum=out[13], carry=c13);
    FullAdder(a=a[14], b=b[14], c=c13, sum=out[14], carry=c14);
    FullAdder(a=a[15], b=b[15], c=c14, sum=out[15], carry=c15);
}

インクリメンタ

加算器を使って、与えられたビット列に1を足す。固定値の表現は、1 = true, 0 = false で表現すること。この表現慣れとかんとね。

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/02/Inc16.hdl

/**
 * 16-bit incrementer:
 * out = in + 1 (arithmetic addition)
 */

CHIP Inc16 {
    IN in[16];
    OUT out[16];

    PARTS:
   // Put you code here:
    Add16(a=in, b[0]=trueb[1..15]=falseout=out);
}

今日の筋トレ

今日はBメニューの日

  • スクワット:10回x2set、ダンベル20kg
  • カーフレイズ:10回x2set、ダンベル20kg
  • サイドベント:10回x2set
  • シットアップ:10回x2set
  • スタンディングツイスト:30秒x3set
  • リングフィットアドベンチャーで足、腹筋中心にエクササイズ