目次

プログラミングアシスト

 アセンブリ言語の場合少ないニモニックを利用して
 コードを作成していきます。

 アセンブリ言語のニモニックコードを覚えるのは無味乾燥で
 自作したプロセッサのニモニックを汎用アセンブラのために
 変換するのも面倒です。

 アセンブリ言語ソースコードを入力するために
 スクリプトアシストをTcl/Tkで作成しました。



 ニモニックとレジスタの候補を並べておいて
 ボタンをクリックすると、1行コードを生成
 します。



 これで、プログラム入力の負担を減らせると同時に
 タイプミスを少なくできます。

 一度プログラムを入力すれば、ファイルに保存
 できるので、後からの加筆修正は楽になります。

 Load、Saveボタンを用意し、ファイルへ出力あるいは
 ファイルから入力できるようにしました。



 Tcl/Tkのソースコードは以下。

#!/usr/local/bin/wish

wm title . "Script assist"

set theFileName ""
set fd_in       ""
set fd_out      ""
set xvalue      "0"

#######################################
# define objects
#######################################
#----- label -----
label .lblFileNameLabel -text "FileName"
label .lblFileName      -textvariable theFileName
label .lblCode          -text "code"
label .lblRegister      -text "register"
label .lblRegisterx     -text "register"
label .lblValue         -text "value"

#----- list box -----
listbox .lstCode -yscrollcommand ".scbLadderV set" -width 16

#----- scroll bar -----
scrollbar .scbLadderV -orient vertical -command ".lstCode yview"

#----- entry -----
entry .txtValue -width 6 -textvariable xvalue

#----- spin box -----
spinbox .sbxWreg  -value {"WA" "WB" "DPR" "SMR" "RMR" "LMR" "SR" "RRE" "LRE"} -state readonly -width 6
spinbox .sbxWregx -value {"WA" "WB" "DPR" "SMR" "RMR" "LMR" "SR" "RRE" "LRE"} -state readonly -width 6
spinbox .sbxSel   -value {"value" "register"} -state readonly -width 6

#----- button -----
button .btnExit -text "Exit" -command "exit"

button .btnLoad -text "Load" -command {
  # tk_messageBox -type ok -message "Load"
  set theFileName [tk_getOpenFile -filetypes {{"assemble source file" {.asm}}}]
  # clear list box
  .lstCode delete 0 end
  # open 
  set fd_in [open $theFileName "r"]
  # judge 
  if { $fd_in != "" } {
    # read context
    set i 0
    while { [gets $fd_in sbuf] >= 0 } {
      # show
      .lstCode insert end $sbuf
    }
    # close
    close $fd_in
  }
}

button .btnSave -text "Save" -command {
  # tk_messageBox -type ok -message "Save"
  set theFileName [tk_getSaveFile -filetypes {{"assemble source file" {.asm}}}]
  # add extension
  set theFileName "$theFileName.asm"
  # open 
  set fd_out [open $theFileName "w"]
  # write context
  set last [.lstCode index end]
  for {set i 0} {$i < $last} {incr i} {
    set tmp [.lstCode get $i]
    puts $fd_out $tmp
  }
  # close
  close $fd_out
}

button .btnClear -text "Clear" -command {
  .lstCode delete 0 end
}

button .btnLD -text "LD" -width 8 -command {
  # clear string
  set tmp "" 
  # judge
  if { [.sbxSel get] == "value" } {
    set tmp "LD [.sbxWreg get],$xvalue"
  } else {
    set tmp "LD [.sbxWreg get],[.sbxWregx get]"
  }
  # store
  .lstCode insert end "$tmp"
}

button .btnST -text "ST" -width 8 -command {
  # clear string
  set tmp ""
  # judge
  if { [.sbxSel get] == "value" } {
    set tmp "ST [.sbxWreg get],$xvalue"
  } else {
    set tmp "ST [.sbxWreg get],[.sbxWregx get]"
  }
  # store 
  .lstCode insert end "$tmp"
}

button .btnJNG -text "JNG" -width 8 -command {
  .lstCode insert end "JNG $xvalue"
}

button .btnJZE -text "JZE" -width 8 -command {
  .lstCode insert end "JZE $xvalue"
}

button .btnJMP -text "JMP" -width 8 -command {
  .lstCode insert end "JMP $xvalue"
}

button .btnMOV -text "MOV" -width 8 -command {
  .lstCode insert end "MOV [.sbxWreg get],[.sbxWregx get]"
}

button .btnMVI -text "MVI" -width 8 -command {
  .lstCode insert end "MVI [.sbxWreg get],$xvalue"
}

button .btnEQ -text "EQ" -width 8 -command {
  .lstCode insert end "EQ [.sbxWreg get],$xvalue"
}

button .btnINC -text "INC" -width 8 -command {
  .lstCode insert end "INC [.sbxWreg get]"
}

button .btnDEC -text "DEC" -width 8 -command {
  .lstCode insert end "DEC [.sbxWreg get]"
}

#######################################
# window area placing
#######################################
grid .lblFileNameLabel -column 0 -row 0
grid .lblFileName      -column 0 -row 1
grid .lstCode          -column 0 -row 2
grid .lblCode          -column 0 -row 6
grid .lblRegister      -column 2 -row 6
grid .lblValue         -column 2 -row 9
grid .lblRegisterx     -column 2 -row 11

grid .scbLadderV -column 1 -row 2 -sticky ns

grid .btnLoad  -column 2 -row 0
grid .btnSave  -column 2 -row 1
grid .btnClear -column 2 -row 2

grid .btnLD   -column 0 -row 8
grid .btnST   -column 0 -row 9
grid .btnJNG  -column 0 -row 10
grid .btnJZE  -column 0 -row 11
grid .btnJMP  -column 0 -row 12
grid .btnMOV  -column 0 -row 13
grid .btnMVI  -column 0 -row 14
grid .btnEQ   -column 0 -row 15
grid .btnINC  -column 0 -row 16
grid .btnDEC  -column 0 -row 17

grid .sbxWreg  -column 2 -row 8
grid .txtValue -column 2 -row 10
grid .sbxWregx -column 2 -row 12

grid .sbxSel   -column 3 -row 8

grid .btnExit  -column 3 -row 20

 Tcl/Tkは、OSとしてUnixを使っていれば、標準でバンドリング
 されています。また、MS-Windowsならば、ActiveTclを新規に
 インストールすると、使えます。

 Unixの場合、Tcl/Tkの環境がどこにインストールされているのかを
 スクリプトの先頭で指定しなければなりません。

 #!/usr/local/bin/wishを利用している環境に合わせて
 書き直します。
 Tcl/Tkの環境がどこにあるのかは、次のコマンドを入力すれば
 OSが教えてくれます。

 which wish{enter}

 Tcl/Tkは、MacOSXでも使えるので、マルチプラットホームで
 手軽な開発ツールを作成するときに使えます。


C言語用スクリプトアシスト  アセンブリ言語のスクリプトアシストができたので  C言語プログラムでも、同様に入力ミスを低減できる  アプリケーションプログラムを作成します。  すべてのコードを入力することは不可能と判断して  制御構文と関数を入力しやすいようにしました。  Cの制御構文は、以下としました。  do...while、switch... case...は、使う頻度が  少ないので割愛しました。  他に関数を定義できるようにします。  大まかな仕様を考えたので、GUIをイメージしました。  ボタンにテキストを貼付け、クリックすると  対応した変数あるいは値を設定できるように  します。  各ボタンの処理を考えていきます。  Default   クリックすると、雛形となるデータタイプと   main関数を入力します。  Clear   クリックすると、入力内容を削除します。  Space   空行を入れたい行を左クリック後、クリックすると   空行が入ります。  IF...   if文を入れたい行を左クリック後、condition(条件)の   右の欄に式を入れ、クリックすると、if文が入ります。  IF...ELSE...   if else 文を入れたい行を左クリック後、condition(条件)の   右の欄に式を入れ、クリックすると、if else文が入ります。  WHILE   while文を入れたい行を左クリック後、condition(条件)の   右の欄に式を入れ、クリックすると、while文が入ります。  FOR   for文を入れたい行を左クリック後、condition(条件)の   右の欄に式を入れ、クリックすると、for文が入ります。  clear condition   condition(条件)の右の欄の内容を、削除します。  FUNCTION   関数の雛形を入れたい行を左クリック後、function name(関数名)   data type(選択)、パラメータ(文字列)を右の欄に入れてから   クリックすると、関数定義が入ります。  ファイルに保存したいときは、Saveボタンをクリックし  保存してあるファイルを編集したいときはLoadボタンを  クリックします。  終了は、Exitボタンをクリックします。  詳細なコード定義は、エディタを使うとして、この程度の  ソフトでも、タイプミス軽減、時間短縮に充分威力を発揮  します。  Tcl/Tkのソースコードは、以下。 #!/usr/local/bin/wish wm title . "Make script" set theFileName "" set fd_in "" set fd_out "" set xcondition "" set xfname "" set xparam "" ####################################### # define objects ####################################### #----- label ----- label .lblFileNameLabel -text "FileName" label .lblFileName -textvariable theFileName label .lblDummy -text "control" label .lblCondition -text "condition" label .lblFName -text "function name" label .lblFtype -text "data type" label .lblParam -text "parameters" #----- entry ----- entry .txtCond -textvariable xcondition entry .txtFName -textvariable xfname entry .txtParam -textvariable xparam #----- button (script) ----- button .btnIF -text "IF" -width 8 -command { # get active line number set ptr [.lstScript index active] # tk_messageBox -type ok -message "$ptr" # store strings .lstScript insert $ptr "if ( [.txtCond get]) {" .lstScript insert [expr $ptr+1] " " .lstScript insert [expr $ptr+2] "}" } button .btnIFE -text "IF...ELSE..." -width 8 -command { # get active line number set ptr [.lstScript index active] # store strings .lstScript insert $ptr "if ( [.txtCond get] ) {" .lstScript insert [expr $ptr+1] " " .lstScript insert [expr $ptr+2] "} else {" .lstScript insert [expr $ptr+3] " " .lstScript insert [expr $ptr+4] "}" } button .btnWHILE -text "WHILE" -width 8 -command { # get active line number set ptr [.lstScript index active] # store strings .lstScript insert $ptr "while ( [.txtCond get] ) {" .lstScript insert [expr $ptr+1] " " .lstScript insert [expr $ptr+2] "}" } button .btnFOR -text "FOR" -width 8 -command { # get active line number set ptr [.lstScript index active] # store strings .lstScript insert $ptr "for ( [.txtCond get] ) {" .lstScript insert [expr $ptr+1] " " .lstScript insert [expr $ptr+2] "}" } button .btnFUNCTION -text "FUNCTION" -width 8 -command { # get active line number set ptr [.lstScript index active] # store strings .lstScript insert $ptr "[.sbxDtype get] $xfname ($xparam)" .lstScript insert [expr $ptr+1] "{" .lstScript insert [expr $ptr+2] " " .lstScript insert [expr $ptr+3] "}" } #----- list box ----- listbox .lstScript -font "utf-8" -yscrollcommand ".scbLadderV set" -width 50 #----- scroll bar ----- scrollbar .scbLadderV -orient vertical -command ".lstScript yview" #----- spin box ----- spinbox .sbxDtype -value {"void" "UBYTE" "SBYTE" "UWORD" "SWORD" "ULONG" "SLONG"} -state readonly -width 8 #----- button (system) ----- button .btnExit -text "Exit" -command "exit" button .btnLoad -text "Load" -command { # clear list box .lstScript delete 0 end # get file name set theFileName [tk_getOpenFile -filetypes {{"assembly code file" {.asm}}}] # set file handle set fd_in [open $theFileName "r"] # read context while { [gets $fd_in sbuf] >= 0 } { # store lines to listbox .lstScript insert end $sbuf } # close close $fd_in } button .btnSave -text "Save" -command { # get file name set theFileName [tk_getSaveFile -filetypes {{"text code file" {.txt}}}] # add extention set theFileName "$theFileName.txt" # set file handle set fd_out [open $theFileName "w"] # write context set last [.lstScript index end] # tk_messageBox -type ok -message "$last" for {set i 0} {$i < $last} {incr i} { set tmp [.lstScript get $i] puts $fd_out $tmp } # close close $fd_out } button .btnDefault -text "Default" -command { # clear .lstScript delete 0 end # data types .lstScript insert end "typedef unsigned char UBYTE ;" .lstScript insert end "typedef unsigned short UWORD ;" .lstScript insert end "typedef unsigned long ULONG ;" .lstScript insert end "typedef signed char SBYTE ;" .lstScript insert end "typedef signed short SWORD ;" .lstScript insert end "typedef signed long SLONG ;" .lstScript insert end " " # main function .lstScript insert end "void main(void)" .lstScript insert end "{" .lstScript insert end " " .lstScript insert end "}" } button .btnClear -text "Clear" -command { .lstScript delete 0 end } button .btnClearCondition -text "clear condition" -command { set xcondition "" } button .btnClearParam -text "clear parameters" -command { set xparam "" } button .btnSpace -text "Space" -command { # get active line number set ptr [.lstScript index active] # store strings .lstScript insert $ptr " " } ####################################### # window area placing ####################################### grid .lblFileNameLabel -column 0 -row 0 grid .lblFileName -column 0 -row 1 grid .lstScript -column 0 -row 2 grid .lblDummy -column 0 -row 6 grid .btnIF -column 0 -row 7 grid .btnIFE -column 0 -row 8 grid .btnWHILE -column 0 -row 9 grid .btnFOR -column 0 -row 10 grid .btnFUNCTION -column 0 -row 11 grid .scbLadderV -column 1 -row 2 -sticky ns grid .btnLoad -column 1 -row 0 grid .btnSave -column 1 -row 1 grid .btnDefault -column 2 -row 0 grid .btnClear -column 2 -row 1 grid .btnSpace -column 3 -row 0 grid .lblCondition -column 1 -row 7 grid .txtCond -column 2 -row 7 grid .lblFName -column 1 -row 11 grid .txtFName -column 2 -row 11 grid .lblFtype -column 1 -row 12 grid .sbxDtype -column 2 -row 12 grid .lblParam -column 1 -row 13 grid .txtParam -column 2 -row 13 grid .btnClearCondition -column 5 -row 7 grid .btnClearParam -column 5 -row 13 grid .btnExit -column 5 -row 14  200行程度のプログラムですが、意外に役に立ちました。
目次

inserted by FC2 system