逆アセンブルのリファクタリング
authorj8takagi <j8takagi@nifty.com>
Fri, 6 Jul 2018 19:26:34 +0000 (04:26 +0900)
committerj8takagi <j8takagi@nifty.com>
Fri, 6 Jul 2018 19:26:34 +0000 (04:26 +0900)
src/disassemble.c
test/system/casl2rev/io/0.txt [new file with mode: 0644]
test/system/casl2rev/io/Makefile [new file with mode: 0644]
test/system/casl2rev/io/cmd [new file with mode: 0755]
test/system/casl2rev/io/desc.txt [new file with mode: 0644]
test/system/casl2rev/sum_10/0.txt

index f2df812..ff24e3c 100644 (file)
@@ -3,7 +3,34 @@
 /**
  * @brief 機械コードの出力列
  */
-int codecol = 38;
+int codecol = 32;
+
+/**
+ * @brief ファイルストリームから1ワードを取得する
+ *
+ * @return 取得した1ワード
+ *
+ * @param stream ファイルストリーム
+ */
+WORD fgetword(FILE *stream);
+
+/**
+ * @brief ファイルストリームを1ワード戻す
+ *
+ * @return なし
+ *
+ * @param stream ファイルストリーム
+ */
+void fungetword(FILE *stream);
+
+/**
+ * @brief ファイルストリームから、値が0の連続するWORD数を返す
+ *
+ * @return 値が0の連続するWORD数
+ *
+ * @param stream ファイルストリーム
+ */
+WORD zero_data_cnt(FILE *stream);
 
 /**
  * @brief 機械コードをコメントとして標準出力へ出力する
@@ -124,11 +151,36 @@ void disassemble_ds(WORD wcnt, WORD pradr)
     }
 }
 
+WORD fgetword(FILE *stream)
+{
+    WORD aword;
+    fread(&aword, sizeof(WORD), 1, stream);
+    return aword;
+}
+
+void fungetword(FILE *stream)
+{
+    fseek(stream, -sizeof(WORD), SEEK_CUR);
+}
+
+WORD zero_data_cnt(FILE *stream)
+{
+    WORD cnt = 0, word = 0;
+    while(!feof(stream) && word == 0) {
+        word = fgetword(stream);
+        cnt++;
+    }
+    if(!feof(stream)) {
+        fungetword(stream);
+    }
+    return cnt;
+}
+
 bool disassemble_file(const char *file)
 {
     bool stat = true;
     FILE *fp;
-    WORD i = 0, w, cmd, adr, dsarea;
+    WORD i = 0, word, cmd, zcnt;
     CMDTYPE cmdtype = 0;
     char *cmdname;
     bool inst = true;
@@ -142,37 +194,27 @@ bool disassemble_file(const char *file)
     create_code_cmdtype();                          /* 命令のコードとタイプがキーのハッシュ表を作成 */
 
     fprintf(stdout, "MAIN    START\n");
-    for(fread(&w, sizeof(WORD), 1, fp); !feof(fp); i++, fread(&w, sizeof(WORD), 1, fp)) {
-        cmd = w & 0xFF00;
-        cmdname = getcmdname(cmd);
+    for(word = fgetword(fp); !feof(fp); i++, word = fgetword(fp)) {
+        cmdname = getcmdname(cmd = word & 0xFF00);
         cmdtype = getcmdtype(cmd);
-        if(w == 0){
-            if(inst == true) {
+        if(word == 0){
+            if(inst == true) {  /* プログラム領域の場合  */
                 disassemble_cmd_r(NONE, "nop", 0, i);
-            } else {
-                dsarea = 1;
-                do {
-                    fread(&w, sizeof(WORD), 1, fp);
-                    dsarea++;
-                } while(w == 0 && !feof(fp));
-                if(!feof(fp)) {
-                    fseek(fp, -1 * sizeof(WORD) , SEEK_CUR);
-                }
-                if(dsarea == 1) {
+            } else {            /* データ領域の場合 */
+                if((zcnt = zero_data_cnt(fp)) == 1) { /* 1つだけの0はDCとみなす */
                     disassemble_dc(0, i);
-                } else {
-                    disassemble_ds(dsarea, i);
-                    i += dsarea - 1;
+                } else {        /* 連続する0はDSとみなす */
+                    disassemble_ds(zcnt, i);
+                    i += zcnt - 1;
                 }
             }
         } else if(cmd == 0) {
-            disassemble_dc(w, i);
+            disassemble_dc(word, i);
         } else {
             if(cmdtype == R_ADR_X || cmdtype == ADR_X) {
-                fread(&adr, sizeof(WORD), 1, fp);
-                disassemble_cmd_adr_x(cmdtype, cmdname, w, adr, i++);
+                disassemble_cmd_adr_x(cmdtype, cmdname, word, fgetword(fp), i++);
             } else {
-                disassemble_cmd_r(cmdtype, cmdname, w, i);
+                disassemble_cmd_r(cmdtype, cmdname, word, i);
             }
             inst = (cmd != 0x8100) ? true : false;
         }
@@ -186,30 +228,29 @@ bool disassemble_file(const char *file)
 
 void disassemble_memory(WORD start, WORD end)
 {
-    WORD i, w, cmd, adr;
+    WORD i, word, cmd;
     CMDTYPE cmdtype = 0;
     char *cmdname;
     bool inst = true;
 
     for(i = start; i <= end; i++) {
-        w = sys->memory[i];
-        cmd = w & 0xFF00;
-        cmdname = getcmdname(cmd);
+        word = sys->memory[i];
+        cmdname = getcmdname(cmd = word & 0xFF00);
         cmdtype = getcmdtype(cmd);
-        if(w == 0) {
-            if(inst == true) {
+        if(word == 0) {
+            if(inst == true) {  /* プログラム領域の場合  */
                 disassemble_cmd_r(NONE, "nop", 0, i);
-            } else {
+            } else {            /* データ領域の場合。メモリーでは、DC 0とみなす */
                 disassemble_dc(0, i);
             }
         } else if(cmd == 0) {
-            disassemble_dc(w, i);
+            disassemble_dc(word, i);
         } else {
             if(cmdtype == R_ADR_X || cmdtype == ADR_X) {
-                adr = sys->memory[i+1];
-                disassemble_cmd_adr_x(cmdtype, cmdname, w, adr, i++);
+                disassemble_cmd_adr_x(cmdtype, cmdname, word, sys->memory[i+1], i);
+                i++;
             } else {
-                disassemble_cmd_r(cmdtype, cmdname, w, i);
+                disassemble_cmd_r(cmdtype, cmdname, word, i);
             }
             inst = (cmd != 0x8100) ? true : false;
         }
diff --git a/test/system/casl2rev/io/0.txt b/test/system/casl2rev/io/0.txt
new file mode 100644 (file)
index 0000000..2b6dd98
--- /dev/null
@@ -0,0 +1,170 @@
+MAIN    START
+        PUSH    #0000,GR1       ; #0000: #7001 #0000
+        PUSH    #0000,GR2       ; #0002: #7002 #0000
+        LAD     GR1,#005B       ; #0004: #1210 #005B
+        LAD     GR2,#006F       ; #0006: #1220 #006F
+        SVC     #0002           ; #0008: #F000 #0002
+        LAD     GR1,#00C8       ; #000A: #1210 #00C8
+        LAD     GR2,#00C9       ; #000C: #1220 #00C9
+        SVC     #0002           ; #000E: #F000 #0002
+        POP     GR2             ; #0010: #7120
+        POP     GR1             ; #0011: #7110
+        PUSH    #0000,GR1       ; #0012: #7001 #0000
+        PUSH    #0000,GR2       ; #0014: #7002 #0000
+        LAD     GR1,#0087       ; #0016: #1210 #0087
+        LAD     GR2,#00C7       ; #0018: #1220 #00C7
+        SVC     #0001           ; #001A: #F000 #0001
+        POP     GR2             ; #001C: #7120
+        POP     GR1             ; #001D: #7110
+        LD      GR1,#00C7       ; #001E: #1010 #00C7
+        JZE     #005A           ; #0020: #6300 #005A
+        PUSH    #0000,GR1       ; #0022: #7001 #0000
+        PUSH    #0000,GR2       ; #0024: #7002 #0000
+        LAD     GR1,#0070       ; #0026: #1210 #0070
+        LAD     GR2,#0079       ; #0028: #1220 #0079
+        SVC     #0002           ; #002A: #F000 #0002
+        LAD     GR1,#00CA       ; #002C: #1210 #00CA
+        LAD     GR2,#00CB       ; #002E: #1220 #00CB
+        SVC     #0002           ; #0030: #F000 #0002
+        POP     GR2             ; #0032: #7120
+        POP     GR1             ; #0033: #7110
+        PUSH    #0000,GR1       ; #0034: #7001 #0000
+        PUSH    #0000,GR2       ; #0036: #7002 #0000
+        LAD     GR1,#0087       ; #0038: #1210 #0087
+        LAD     GR2,#00C7       ; #003A: #1220 #00C7
+        SVC     #0002           ; #003C: #F000 #0002
+        LAD     GR1,#00CC       ; #003E: #1210 #00CC
+        LAD     GR2,#00CD       ; #0040: #1220 #00CD
+        SVC     #0002           ; #0042: #F000 #0002
+        POP     GR2             ; #0044: #7120
+        POP     GR1             ; #0045: #7110
+        PUSH    #0000,GR1       ; #0046: #7001 #0000
+        PUSH    #0000,GR2       ; #0048: #7002 #0000
+        LAD     GR1,#007A       ; #004A: #1210 #007A
+        LAD     GR2,#0086       ; #004C: #1220 #0086
+        SVC     #0002           ; #004E: #F000 #0002
+        LAD     GR1,#00CE       ; #0050: #1210 #00CE
+        LAD     GR2,#00CF       ; #0052: #1220 #00CF
+        SVC     #0002           ; #0054: #F000 #0002
+        POP     GR2             ; #0056: #7120
+        POP     GR1             ; #0057: #7110
+        JUMP    #0012           ; #0058: #6400 #0012
+        RET                     ; #005A: #8100
+        DC      76              ; #005B: #004C ::    76 = #004C = 0000000001001100 = 'L'
+        DC      69              ; #005C: #0045 ::    69 = #0045 = 0000000001000101 = 'E'
+        DC      84              ; #005D: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      39              ; #005E: #0027 ::    39 = #0027 = 0000000000100111 = '''
+        DC      83              ; #005F: #0053 ::    83 = #0053 = 0000000001010011 = 'S'
+        DC      32              ; #0060: #0020 ::    32 = #0020 = 0000000000100000 = ' '
+        DC      83              ; #0061: #0053 ::    83 = #0053 = 0000000001010011 = 'S'
+        DC      84              ; #0062: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      65              ; #0063: #0041 ::    65 = #0041 = 0000000001000001 = 'A'
+        DC      82              ; #0064: #0052 ::    82 = #0052 = 0000000001010010 = 'R'
+        DC      84              ; #0065: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      46              ; #0066: #002E ::    46 = #002E = 0000000000101110 = '.'
+        DC      32              ; #0067: #0020 ::    32 = #0020 = 0000000000100000 = ' '
+        DC      84              ; #0068: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      89              ; #0069: #0059 ::    89 = #0059 = 0000000001011001 = 'Y'
+        DC      80              ; #006A: #0050 ::    80 = #0050 = 0000000001010000 = 'P'
+        DC      69              ; #006B: #0045 ::    69 = #0045 = 0000000001000101 = 'E'
+        DC      32              ; #006C: #0020 ::    32 = #0020 = 0000000000100000 = ' '
+        DC      73              ; #006D: #0049 ::    73 = #0049 = 0000000001001001 = 'I'
+        DC      78              ; #006E: #004E ::    78 = #004E = 0000000001001110 = 'N'
+        DC      20              ; #006F: #0014 ::    20 = #0014 = 0000000000010100
+        DC      89              ; #0070: #0059 ::    89 = #0059 = 0000000001011001 = 'Y'
+        DC      79              ; #0071: #004F ::    79 = #004F = 0000000001001111 = 'O'
+        DC      85              ; #0072: #0055 ::    85 = #0055 = 0000000001010101 = 'U'
+        DC      32              ; #0073: #0020 ::    32 = #0020 = 0000000000100000 = ' '
+        DC      84              ; #0074: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      89              ; #0075: #0059 ::    89 = #0059 = 0000000001011001 = 'Y'
+        DC      80              ; #0076: #0050 ::    80 = #0050 = 0000000001010000 = 'P'
+        DC      69              ; #0077: #0045 ::    69 = #0045 = 0000000001000101 = 'E'
+        DC      68              ; #0078: #0044 ::    68 = #0044 = 0000000001000100 = 'D'
+        DC      9               ; #0079: #0009 ::     9 = #0009 = 0000000000001001 = '\t'
+        DC      87              ; #007A: #0057 ::    87 = #0057 = 0000000001010111 = 'W'
+        DC      72              ; #007B: #0048 ::    72 = #0048 = 0000000001001000 = 'H'
+        DC      65              ; #007C: #0041 ::    65 = #0041 = 0000000001000001 = 'A'
+        DC      84              ; #007D: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      39              ; #007E: #0027 ::    39 = #0027 = 0000000000100111 = '''
+        DC      83              ; #007F: #0053 ::    83 = #0053 = 0000000001010011 = 'S'
+        DC      32              ; #0080: #0020 ::    32 = #0020 = 0000000000100000 = ' '
+        DC      78              ; #0081: #004E ::    78 = #004E = 0000000001001110 = 'N'
+        DC      69              ; #0082: #0045 ::    69 = #0045 = 0000000001000101 = 'E'
+        DC      88              ; #0083: #0058 ::    88 = #0058 = 0000000001011000 = 'X'
+        DC      84              ; #0084: #0054 ::    84 = #0054 = 0000000001010100 = 'T'
+        DC      63              ; #0085: #003F ::    63 = #003F = 0000000000111111 = '?'
+        DC      12              ; #0086: #000C ::    12 = #000C = 0000000000001100
+        DS      65              ; #0087: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+                                ; #0088: #0000
+        DC      10              ; #00C8: #000A ::    10 = #000A = 0000000000001010 = '\n'
+        DC      1               ; #00C9: #0001 ::     1 = #0001 = 0000000000000001
+        DC      10              ; #00CA: #000A ::    10 = #000A = 0000000000001010 = '\n'
+        DC      1               ; #00CB: #0001 ::     1 = #0001 = 0000000000000001
+        DC      10              ; #00CC: #000A ::    10 = #000A = 0000000000001010 = '\n'
+        DC      1               ; #00CD: #0001 ::     1 = #0001 = 0000000000000001
+        DC      10              ; #00CE: #000A ::    10 = #000A = 0000000000001010 = '\n'
+        DC      1               ; #00CF: #0001 ::     1 = #0001 = 0000000000000001
+        END
diff --git a/test/system/casl2rev/io/Makefile b/test/system/casl2rev/io/Makefile
new file mode 100644 (file)
index 0000000..b6dac59
--- /dev/null
@@ -0,0 +1,2 @@
+include ../Define.mk
+include ../Test.mk
diff --git a/test/system/casl2rev/io/cmd b/test/system/casl2rev/io/cmd
new file mode 100755 (executable)
index 0000000..4c7ef32
--- /dev/null
@@ -0,0 +1 @@
+../../../../casl2rev ../../../../as/misc/io.o
diff --git a/test/system/casl2rev/io/desc.txt b/test/system/casl2rev/io/desc.txt
new file mode 100644 (file)
index 0000000..88b8b44
--- /dev/null
@@ -0,0 +1 @@
+DC、DSを用いたプログラムを逆アセンブル
index af34c97..75f6056 100644 (file)
@@ -1,19 +1,19 @@
 MAIN    START
-        PUSH    #0000,GR1             ; #0000: #7001 #0000
-        PUSH    #0000,GR2             ; #0002: #7002 #0000
-        XOR     GR2,GR2               ; #0004: #3622
-        LD      GR1,#0015             ; #0005: #1010 #0015
-        ADDL    GR2,GR1               ; #0007: #2621
-        ADDL    GR1,#0017             ; #0008: #2210 #0017
-        CPL     GR1,#0016             ; #000A: #4110 #0016
-        JPL     #0010                 ; #000C: #6500 #0010
-        JUMP    #0007                 ; #000E: #6400 #0007
-        ST      GR2,#0018             ; #0010: #1120 #0018
-        POP     GR2                   ; #0012: #7120
-        POP     GR1                   ; #0013: #7110
-        RET                           ; #0014: #8100
-        DC      1                     ; #0015: #0001 ::     1 = #0001 = 0000000000000001
-        DC      10                    ; #0016: #000A ::    10 = #000A = 0000000000001010 = '\n'
-        DC      1                     ; #0017: #0001 ::     1 = #0001 = 0000000000000001
-        DC      0                     ; #0018: #0000 ::     0 = #0000 = 0000000000000000
+        PUSH    #0000,GR1       ; #0000: #7001 #0000
+        PUSH    #0000,GR2       ; #0002: #7002 #0000
+        XOR     GR2,GR2         ; #0004: #3622
+        LD      GR1,#0015       ; #0005: #1010 #0015
+        ADDL    GR2,GR1         ; #0007: #2621
+        ADDL    GR1,#0017       ; #0008: #2210 #0017
+        CPL     GR1,#0016       ; #000A: #4110 #0016
+        JPL     #0010           ; #000C: #6500 #0010
+        JUMP    #0007           ; #000E: #6400 #0007
+        ST      GR2,#0018       ; #0010: #1120 #0018
+        POP     GR2             ; #0012: #7120
+        POP     GR1             ; #0013: #7110
+        RET                     ; #0014: #8100
+        DC      1               ; #0015: #0001 ::     1 = #0001 = 0000000000000001
+        DC      10              ; #0016: #000A ::    10 = #000A = 0000000000001010 = '\n'
+        DC      1               ; #0017: #0001 ::     1 = #0001 = 0000000000000001
+        DC      0               ; #0018: #0000 ::     0 = #0000 = 0000000000000000
         END