More Related Content
Similar to ハードウェアエンジニアとRuby (11)
ハードウェアエンジニアとRuby
- 4. プログラミング言語に潜む抽象性
; hello.asm
[BITS 16] ; リアルモード
[ORG 0x7C00] ; 開始位置
start:
mov si, msg ; msg の先頭位置を SI レジスタに設定する。
call print_string
fn:
hlt
jmp fn
print_char:
mov ah, 0x0E ; BIOSに一文字表示を伝える
mov bh, 0x00
mov bl, 0x07 ; 文字色(白)
int 0x10 ; BIOSの機能を呼び出す。Call video interrupt.
ret
print_string:
next:
mov al, [si] ; 文字列から一文字を取得し AL レジスタに設定する
inc si ; SI レジスタをインクリメントする
or al, al
jz exit
call print_char
jmp next
exit:
ret
msg db 'Hello World!', 0
times 510 - ($ - $$) db 0 ; ブートシグニチャまで0埋めする
dw 0xaa55 ; ブートシグニチャ
参照元Qiita
https://qiita.com/yoshi-naoyuki/items/fb958e3c914c56baef40
#include <stdio.h>
int main(int argc, char *args[])
{
printf("Hello, world!n");
return 0;
}
アセンブラだと
C言語だと
Hello world をアセンブラとC言語で書くと
- 9. 非決定性有限オートマトン
require ‘set’
class NFARulebook<Struct.new(:rules)
def next_state(state,character)
rule_for(state,character).follow
end
def follow_rules_for(state,character)
rules_for(state,character).map(&:follow)
end
def rule_for(state,character)
rules.detect{|rule| rule.applies_to?(state,character)}
end
end
与えられた状態と入力に対して複数の規則を持つ
遷移先が状態の集合
1 2 3 4
a,b
b a,b a,b
a b
→1 {1} {1,2}
2 {3} {3}
3 {4} {4}
*4 Φ Φ
遷移表
規則集
- 12. 順序回路
module REG4(
input CLK,RST,
input [3:0] D,
output [3:0] Q
);
always @(posedge CLK,posedge RST)begin
if(RST==1’b1)
Q<=4’h0;
else
Q<=D;
end
endmodule
現在の入力値だけでなく過去に入力された値によって出力値を決定する
記憶素子を内蔵している。
順序処理
- 15. チューリングマシン
DTMRulebook.new([
TMRule.new(1,’0’,2,’1’,:right),
TMRule.new(1,’1’,1,’0’,:left),
TMRule.new(1,’_’,2,’1’,:right),
TMRule.new(2,’0’,2,’0’,:right),
TMRule.new(2,’1’,2,’1’,:right),
TMRule.new(2,’_’,3,’_’,:left),
])
1 2 3
1/0;L
0/1;R
_/1;R _/_;L
0/0;R
1/1;R
2進数をインクリメントする。(1011をインクリメントする)
- 16. 加算チューリングマシン
p rulebook=DTMRulebook.new([
TMRule.new(1,'0',1,'0',:right),
TMRule.new(1,'1',2,'1',:right),
TMRule.new(2,'0',3,'1',:right),
TMRule.new(2,'1',2,'1',:right),
TMRule.new(3,'0',4,'1',:left),
TMRule.new(3,'1',3,'1',:right),
TMRule.new(4,'1',4,'0',:right),
TMRule.new(4,'0',4,'0',:left)
])
加算1(01)+2(011)→3(01110)ちょっと適当
1 2 3 4
0,0;R
1/1;R 0/1;R 0/1;L
1/1;R 1/0;R 1/1;L
参考
http://wiki.bmoon.jp/wiki.cgi/Programming?page=
%A5%B3%A5%F3%A5%D4%A5%E5%A1%BC%A5%BF%A4%CE%BB
%C5%C1%C8%A4%DF
(01)+(011)→(01110)
2章始めのプログラムの簡約を使う
ある意味ノイマン型?