home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / dsasmsrc.zip / decoder.c next >
Text File  |  1998-10-09  |  31KB  |  804 lines

  1. //
  2. //
  3. // This program was written by Sang Cho, associate professor at 
  4. //                                       the department of 
  5. //                                       computer science and engineering
  6. //                                       chongju university
  7. // language used: gcc 
  8. //
  9. // date of second release: August 30, 1998 (alpha version)
  10. //
  11. //
  12. //      you can contact me: e-mail address: sangcho@alpha94.chongju.ac.kr
  13. //                            hitel id: chokhas
  14. //                        phone number: (0431) 229-8491    +82-431-229-8491
  15. //
  16. //            real address: Sang Cho
  17. //                      Computer and Information Engineering
  18. //                      ChongJu University
  19. //                      NaeDok-Dong 36 
  20. //                      ChongJu 360-764
  21. //                      South Korea
  22. //
  23. //   Copyright (C) 1997,1998                                 by Sang Cho.
  24. //
  25. // Permission is granted to make and distribute verbatim copies of this
  26. // program provided the copyright notice and this permission notice are
  27. // preserved on all copies.
  28. //
  29. // File: decoder.c 
  30.  
  31.  
  32. # define PREFIX 99
  33. # define max_col 12
  34.  
  35. # include "disasm.h"
  36.  
  37. /* *********************************************************************** */
  38. /* grammar control data                                   */
  39. /* *********************************************************************** */
  40.  
  41. int opcodeTable[] = {
  42. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  43. /* -----------------------------------------------------------------------*/
  44. /*00*/    6,  6,  6,  6,  1,  4,  0,  0,  6,  6,  6,  6,  1,  4,  0, -1,    
  45. /*10*/    6,  6,  6,  6,  1,  4,  0,  0,  6,  6,  6,  6,  1,  4,  0,  0,    
  46. /*20*/    6,  6,  6,  6,  1,  4, 99,  0,  6,  6,  6,  6,  1,  4, 99,  0,    
  47. /*30*/    6,  6,  6,  6,  1,  4, 99,  0,  6,  6,  6,  6,  1,  4, 99,  0,    
  48. /*40*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  49. /*50*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  50. /*60*/    0,  0,  6,  6, 99, 99, 99, 99,  4,  8,  1,  7,  0,  0,  0,  0,    
  51. /*70*/    1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,    
  52. /*80*/   10, 11, -1, 10,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  9,    
  53. /*90*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  5, 15,  0,  0,  0,  0,    
  54. /*A0*/   44, 44, 44, 44,  0,  0,  0,  0,  1,  4,  0,  0,  0,  0,  0,  0,    
  55. /*B0*/    1,  1,  1,  1,  1,  1,  1,  1,  4,  4,  4,  4,  4,  4,  4,  4,    
  56. /*C0*/   10, 10,  2,  0,  6,  6, 10, 11,  3,  0,  2,  0,  0,  1,  0,  0,    
  57. /*D0*/    9,  9,  9,  9,  1,  1, -1,  0, 12, 12, 12, 12, 12, 12, 12, 12,    
  58. /*E0*/    1,  1,  1,  1,  1,  1,  1,  1,  4,  4,  5,  1,  0,  0,  0,  0,    
  59. /*F0*/    0,  0, 16, 16,  0,  0, 14, 14,  0,  0,  0,  0,  0,  0,  9, 13};    
  60. /* -----------------------------------------------------------------------*/
  61.  
  62. int opcode2Table[] = {
  63. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  64. /* -----------------------------------------------------------------------*/
  65. /*00*/    4,  4,  2,  2, -1, -1,  0, -1,  0,  0, -1,  0, -1, -1, -1, -1,    
  66. /*10*/   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    
  67. /*20*/    2,  2,  2,  2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    
  68. /*30*/    0,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    
  69. /*40*/    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,    
  70. /*50*/   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    
  71. /*60*/    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, -1, -1,  2,  2,    
  72. /*70*/   -1,  5,  5,  5,  2,  2,  2,  0, -1, -1, -1, -1, -1, -1,  2,  2,    
  73. /*80*/    1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,    
  74. /*90*/    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,    
  75. /*A0*/    0,  0,  0,  2,  3,  2, -1, -1,  0,  0,  0,  2,  3,  2,  4,  2,    
  76. /*B0*/    2,  2,  2,  2,  2,  2,  2,  2, -1, -1,  5,  2,  2,  2,  2,  2,    
  77. /*C0*/    2,  2, -1, -1, -1, -1, -1,  4,  0,  0,  0,  0,  0,  0,  0,  0,    
  78. /*D0*/   -1,  2,  2,  2, -1,  2, -1, -1,  2,  2, -1,  2,  2,  2, -1,  2,    
  79. /*E0*/   -1,  2,  2, -1, -1,  2, -1, -1,  2,  2, -1,  2,  2,  2, -1,  2,    
  80. /*F0*/   -1,  2,  2,  2, -1,  2, -1, -1,  2,  2,  2, -1,  2,  2,  2, -1};
  81. /* -----------------------------------------------------------------------*/
  82.  
  83. int repeatgroupTable[] = {
  84. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  85. /* -----------------------------------------------------------------------*/
  86. /*00*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  87. /*10*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  88. /*20*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  89. /*30*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  90. /*40*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  91. /*50*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  92. /*60*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  2,  2,    
  93. /*70*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  94. /*80*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  95. /*90*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  96. /*A0*/    0,  0,  0,  0,  2,  2,  1,  1,  0,  0,  2,  2,  2,  2,  1,  1,    
  97. /*B0*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  98. /*C0*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  99. /*D0*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  100. /*E0*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,    
  101. /*F0*/    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0};    
  102. /* -----------------------------------------------------------------------*/
  103.  
  104. int modTable[] = {
  105. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  106. /* -----------------------------------------------------------------------*/
  107. /*00*/    1,  1,  1,  1,  2,  3,  1,  1,  1,  1,  1,  1,  2,  3,  1,  1,    
  108. /*10*/    1,  1,  1,  1,  2,  3,  1,  1,  1,  1,  1,  1,  2,  3,  1,  1,    
  109. /*20*/    1,  1,  1,  1,  2,  3,  1,  1,  1,  1,  1,  1,  2,  3,  1,  1,    
  110. /*30*/    1,  1,  1,  1,  2,  3,  1,  1,  1,  1,  1,  1,  2,  3,  1,  1,    
  111. /*40*/    4,  4,  4,  4,  5,  4,  4,  4,  4,  4,  4,  4,  5,  4,  4,  4,    
  112. /*50*/    4,  4,  4,  4,  5,  4,  4,  4,  4,  4,  4,  4,  5,  4,  4,  4,    
  113. /*60*/    4,  4,  4,  4,  5,  4,  4,  4,  4,  4,  4,  4,  5,  4,  4,  4,    
  114. /*70*/    4,  4,  4,  4,  5,  4,  4,  4,  4,  4,  4,  4,  5,  4,  4,  4,    
  115. /*80*/    6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  7,  6,  6,  6,    
  116. /*90*/    6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  7,  6,  6,  6,    
  117. /*A0*/    6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  7,  6,  6,  6,    
  118. /*B0*/    6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  7,  6,  6,  6,    
  119. /*C0*/    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,    
  120. /*D0*/    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,    
  121. /*E0*/    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,    
  122. /*F0*/    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8};    
  123. /* -----------------------------------------------------------------------*/
  124.  
  125. int mod16Table[] = {
  126. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  127. /* -----------------------------------------------------------------------*/
  128. /*00*/    1,  1,  1,  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  2,  1,    
  129. /*10*/    1,  1,  1,  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  2,  1,    
  130. /*20*/    1,  1,  1,  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  2,  1,    
  131. /*30*/    1,  1,  1,  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  2,  1,    
  132. /*40*/    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,    
  133. /*50*/    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,    
  134. /*60*/    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,    
  135. /*70*/    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,    
  136. /*80*/    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,    
  137. /*90*/    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,    
  138. /*A0*/    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,    
  139. /*B0*/    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,    
  140. /*C0*/    5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,    
  141. /*D0*/    5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,    
  142. /*E0*/    5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,    
  143. /*F0*/    5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5};    
  144. /* -----------------------------------------------------------------------*/
  145.  
  146.  
  147. int sibTable[] = {
  148. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  149. /* -----------------------------------------------------------------------*/
  150. /*00*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  151. /*10*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  152. /*20*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  153. /*30*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  154. /*40*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  155. /*50*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  156. /*60*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  157. /*70*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  158. /*80*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  159. /*90*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  160. /*A0*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  161. /*B0*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  162. /*C0*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  163. /*D0*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  164. /*E0*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2,    
  165. /*F0*/    2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  2,  1,  2,  2};    
  166. /* -----------------------------------------------------------------------*/
  167.  
  168. int regTable[] = {
  169. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  170. /* -----------------------------------------------------------------------*/
  171. /*00*/    0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,    
  172. /*10*/    2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,    
  173. /*20*/    4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,    
  174. /*30*/    6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,    
  175. /*40*/    0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,    
  176. /*50*/    2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,    
  177. /*60*/    4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,    
  178. /*70*/    6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,    
  179. /*80*/    0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,    
  180. /*90*/    2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,    
  181. /*A0*/    4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,    
  182. /*B0*/    6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,    
  183. /*C0*/    0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,    
  184. /*D0*/    2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,    
  185. /*E0*/    4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,    
  186. /*F0*/    6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7};    
  187. /* -----------------------------------------------------------------------*/
  188.  
  189. int rmTable[] = {
  190. /*        0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   */
  191. /* -----------------------------------------------------------------------*/
  192. /*00*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  193. /*10*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  194. /*20*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  195. /*30*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  196. /*40*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  197. /*50*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  198. /*60*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  199. /*70*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  200. /*80*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  201. /*90*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  202. /*A0*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  203. /*B0*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  204. /*C0*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  205. /*D0*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  206. /*E0*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7,    
  207. /*F0*/    0,  1,  2,  3,  4,  5,  6,  7,  0,  1,  2,  3,  4,  5,  6,  7};    
  208. /* -----------------------------------------------------------------------*/
  209.  
  210. /* *********************************************************************** */
  211. /* hooks between disassembler and printing                                 */
  212. /* *********************************************************************** */
  213. int a_loc=0;
  214. int a_loc_save=0;
  215. int i_col=0;
  216. int i_col_save=0;
  217. int i_psp=0;
  218. int prefixStack[8];
  219. int opclass;
  220. int modclass;
  221. int i_opclass;        
  222. int i_opcode;
  223. int i_mod;
  224. int opclassSave=-1;
  225. int opsave=-1;
  226. int modsave=-1;
  227. int i_sib;
  228. int i_byte;
  229. int i_word;
  230. int i_dword;
  231. int m_byte;
  232. int m_dword;
  233. int needspacing=0;
  234. int byteaddress=0;
  235. int stringBuf[4096];
  236. int imb=0;
  237. int mbytes[64];
  238. int NumberOfBytesProcessed=-1;  // we need to keep track of bytes processed.
  239.                               // I don't know why I need to start from -1.... but anyway
  240.                              // this is needed to process
  241. int addressOveride=0;       // prefix 0x64
  242. int operandOveride=0;      // this is needed to process
  243.                           // prefix 0x66: operand size overide
  244.  
  245. DWORD label_start_pos;     /* labe start position of case jump block */
  246. DWORD min_label; 
  247. DWORD cur_position;
  248. int   labelClass;
  249. int   finished;
  250. DWORD lastAnchor=0;
  251. int   leaveFlag=0;
  252. int   delta=0;
  253.  
  254. int   result=0;
  255.  
  256. /* *********************************************************************** */
  257. /* Original Part of grammar generated sources                              */
  258. /* *********************************************************************** */
  259.  
  260. int instruction(int c)
  261. {
  262. int  r;
  263.  
  264.    if (c==0)
  265.    {    
  266.       while(prefixes());
  267.       r=instructionbody();
  268.    }
  269.    else r=databody(c);
  270.    return r;
  271. }
  272. int databody(int c)
  273. {
  274.          if(c==1) return labeldata();
  275.    else if (c==2) return bytex();
  276.    else if (c==3) return byte();
  277.    else if (c==4) return pascalstring();
  278.    else if (c==5) return nullstring();
  279.    else if (c==6) return worddata();
  280.    else return 0;
  281. }
  282. int labeldata()
  283. {
  284. int r;
  285.    r = dword();
  286.    m_dword=result;
  287.    return r;
  288. }
  289. int worddata()
  290. {
  291. int r;
  292.    r = word();
  293.    m_dword=result;
  294.    return r;
  295. }
  296. int instructionbody()
  297. {
  298. int r;
  299.    if (PeekOneByte()!=0x0F) 
  300.                { r=onebyteinstr(); opclass=1; return r;} 
  301.    else {byte(); r=twobyteinstr(); opclass=2; return r;}
  302. }
  303. int byte()
  304. {
  305. int x;
  306.    x=ReadOneByte();
  307.    if(printMode==1)printf("%02X", x);else;
  308.    a_loc++;  i_col++;  result=x;
  309.    return 1;
  310. }
  311. int bytex()
  312. {
  313. int x;
  314.    x=ReadOneByte();
  315.    m_byte=x;                                                                             
  316.    a_loc++;  i_col++;  result=x;
  317.    return 1;
  318. }
  319. int op()
  320. {
  321. int x;
  322.    x=ReadOneByte();
  323.    if(printMode==1)printf("%02X", x);else;
  324.    a_loc++;  i_col++;  result=x;
  325.    return 1;
  326. }
  327. int pascalstring()
  328. {
  329. int i, n, x;
  330.     x=ReadOneByte();
  331.     //if(printMode==1)printf("%02X ", x);else;
  332.     a_loc++; i_col++; result=x;
  333.     stringBuf[0]=x;      
  334.     n=x;
  335.     for(i=1;i<n+1;i++)
  336.     { x=ReadOneByte(); //if(printMode==1)printf("%02X ", x);else; 
  337.     a_loc++; i_col++; result=x; stringBuf[i]=x;}
  338.     return 1;
  339. }
  340. int nullstring()
  341. {
  342. int i, x;
  343.     i=0;
  344.     while(1)
  345.     { x=ReadOneByte(); //if(printMode==1)printf("%02X ", x);else; 
  346.     a_loc++; i_col++; result=x; stringBuf[i++]=x; if(!isprint(PeekOneByte()))break;}
  347.     while(getMap(cur_position+i_col)==0x08)
  348.     {
  349.         x=ReadOneByte(); //if(printMode==1)printf("%02X ", x);else; 
  350.         stringBuf[i++]=x; a_loc++; i_col++; 
  351.     }
  352.     stringBuf[i]=-1;
  353.     return 1;
  354. }
  355. int word()
  356. {
  357. int x,y;
  358.    byte(); x=result;
  359.    byte(); y=result;
  360.    result=y*256+x;
  361.    return 1;
  362. }
  363. int dword()
  364. {
  365. int x1,x2,x3,x4;
  366.    byte(); x1=result;
  367.    byte(); x2=result;
  368.    byte(); x3=result;
  369.    byte(); x4=result;
  370.    result=((x4*256+x3)*256+x2)*256+x1;
  371.    return 1;
  372. }
  373. int adword()
  374. {
  375.    if (addressOveride) return word(); else return dword();
  376. }
  377. int wdword()
  378. {
  379.    if (operandOveride) return word(); else return dword();
  380. }
  381. int pword()
  382. {
  383. int r;
  384.    r=word();  if (!r) return 0; i_word=result;
  385.    r=dword(); if (!r) return 0; i_dword=result;
  386.    return 1;
  387. }   
  388. int prefixes()
  389. {
  390. int r;
  391.    if (opcodeTable[PeekOneByte()]==PREFIX)
  392.    {
  393.       r=op(); if (!r) return 0; 
  394.       prefixStack[i_psp++]=result;
  395.       if (result==102) operandOveride=1;
  396.       if (result==103) addressOveride=1;
  397.       return 1;
  398.    }
  399.    else return 0;
  400. }
  401. int onebyteinstr()
  402. {
  403. int r, b, x, y, y1, y2;
  404.    b=PeekOneByte();
  405.    //fprintf(stderr, "b=%02X",b),getch();
  406.    switch(opcodeTable[b])
  407.    {
  408.       case  0: r=op();     if(!r) return 0; x=result; 
  409.                //fprintf(stderr, "x=%02X",x),getch();
  410.                i_opclass=0; i_opcode=x;                
  411.                break;
  412.       case  1: r=op();     if(!r) return 0; x=result; 
  413.                r=byte();   if(!r) return 0; y=result;
  414.                i_opclass=1; i_opcode=x; i_byte=y;      
  415.                break;
  416.       case  2: r=op();     if(!r) return 0; x=result;
  417.                r=word();   if(!r) return 0; y=result;
  418.                i_opclass=2; i_opcode=x; i_word=y;      
  419.                break;
  420.       case  3: r=op();     if(!r) return 0; x=result;
  421.                r=word();   if(!r) return 0; y1=result;
  422.                r=byte();   if(!r) return 0; y2=result;
  423.                i_opclass=3; i_opcode=x; i_word=y1; i_byte=y2; 
  424.                break;
  425.       case     4: r=op();     if(!r) return 0; x=result;
  426.                r=wdword(); if(!r) return 0; y=result;
  427.                i_opclass=4; i_opcode=x; i_dword=y;
  428.                break;
  429.       case 44: r=op();     if(!r) return 0; x=result;
  430.                r=adword(); if(!r) return 0; y=result;
  431.                i_opclass=4; i_opcode=x; i_dword=y;
  432.                break;
  433.       case  5: r=op();     if(!r) return 0; x=result;
  434.                r=pword();  if(!r) return 0; 
  435.                i_opclass=5; i_opcode=x; 
  436.                break;
  437.       case  6: r=op();     if(!r) return 0; x=result;
  438.                r=modrm();  if(!r) return 0; 
  439.                i_opclass=6; i_opcode=x; 
  440.                break;
  441.       case  7: r=op();     if(!r) return 0; x=result;
  442.                r=modrm();  if(!r) return 0;
  443.                r=byte();   if(!r) return 0; y=result;
  444.                i_opclass=7; i_opcode=x; i_byte=y; 
  445.                break;
  446.       case  8: r=op();     if(!r) return 0; x=result;
  447.                r=modrm();  if(!r) return 0;
  448.                r=wdword(); if(!r) return 0; y=result;
  449.                i_opclass=8; i_opcode=x; i_dword=y; 
  450.                break;
  451.       case  9: r=op();     if(!r) return 0; x=result;
  452.                r=opext();  if(!r) return 0;
  453.                i_opclass=9; i_opcode=x;  
  454.                break;
  455.       case 10: r=op();     if(!r) return 0; x=result;
  456.                r=opext();  if(!r) return 0;
  457.                r=byte();   if(!r) return 0; y=result;
  458.                i_opclass=10; i_opcode=x; i_byte=y; 
  459.                break;
  460.       case 11: r=op();     if(!r) return 0; x=result;
  461.                r=opext();  if(!r) return 0;
  462.                r=wdword(); if(!r) return 0; y=result;
  463.                i_opclass=11; i_opcode=x; i_dword=y; 
  464.                break;
  465.       case 12: r=op();     if(!r) return 0; x=result;
  466.                r=opextg(); if(!r) return 0;
  467.                i_opclass=12; i_opcode=x;  
  468.                break;
  469.       case 13: r=op();     if(!r) return 0; x=result; // case jump block
  470.                b=PeekOneByte();
  471.                if (b==36) 
  472.                { 
  473.                   b=PeekSecondByte();
  474.                   if (rmTable[b]==5)
  475.                   {
  476.                      r=op(); if(!r) return 0; y1=result;
  477.                      r=op(); if(!r) return 0; y2=result;
  478.                      i_opclass=13;
  479.                      i_opcode=x;
  480.                      i_mod=y1;
  481.                      i_sib=y2;
  482.                      r=labelstartposition(); if(!r) return 0;
  483.    // ..................................................................            
  484.                      if (nextMode) 
  485.                      {
  486.                         r=label1(); 
  487.                         finished=1;
  488.                         if(!r) return 1;   // need to be careful ...
  489.                      }
  490.                      return 1;
  491.                   }
  492.                }
  493.                //else
  494.                {
  495.                   b=PeekOneByte();
  496.                   if (regTable[b]<7)
  497.                   {
  498.                      r=opext(); if(!r) return 0;
  499.                      i_opclass=13; i_opcode=x;
  500.                   }
  501.                   else return 0;
  502.                }
  503.                break;
  504.       case 14: r=op();     if(!r) return 0; x=result; // test group
  505.                if (x==246)
  506.                {
  507.                   b=PeekOneByte();
  508.                   if (regTable[b]==0)
  509.                   {
  510.                      r=opext();   if(!r) return 0;
  511.                      r=byte();    if(!r) return 0; y=result;
  512.                      i_opclass=14; i_opcode=x; i_byte=y;
  513.                   }
  514.                   else if (regTable[b]>1)
  515.                   {
  516.                      r=opext();   if(!r) return 0;
  517.                      i_opclass=14; i_opcode=x;
  518.                   }
  519.                   else return 0;
  520.                }
  521.                else
  522.                {
  523.                   b=PeekOneByte();
  524.                   if (regTable[b]==0)
  525.                   {
  526.                      r=opext();   if(!r) return 0;
  527.                      r=wdword();    if(!r) return 0; y=result;
  528.                      i_opclass=14; i_opcode=x; i_dword=y;
  529.                   }
  530.                   else 
  531.                   {
  532.                      r=opext();   if(!r) return 0;
  533.                      i_opclass=14; i_opcode=x;
  534.                   }
  535.                }
  536.                break;
  537.       case 15: r=op();     if(!r) return 0; x=result; // wait group
  538.                i_opclass=15; i_opcode=x;
  539.                b=PeekOneByte();
  540.                if (b==217)
  541.                {
  542.                   b=PeekSecondByte();
  543.                   if (regTable[b]==6||regTable[b]==7)
  544.                   {
  545.                      r=op();    if(!r) return 0; y=result;
  546.                      r=opext(); if(!r) return 0;
  547.                      i_opcode=y; prefixStack[i_psp++]=x;
  548.                   }
  549.                }
  550.                else if (b==219)
  551.                {
  552.                   b=PeekSecondByte();
  553.                   if (b==226||b==227)
  554.                   {
  555.                      r=op();    if(!r) return 0; y1=result;
  556.                      r=op();    if(!r) return 0; y2=result;
  557.                      i_opcode=y1; i_mod=y2; prefixStack[i_psp++]=x;   
  558.                   }
  559.                }
  560.                else if (b==221)
  561.                {
  562.                   b=PeekSecondByte();
  563.                   if (regTable[b]==6||regTable[b]==7)
  564.                   {
  565.                      r=op();    if(!r) return 0; y=result;
  566.                      r=opext(); if(!r) return 0;
  567.                      i_opcode=y; prefixStack[i_psp++]=x;
  568.                   }
  569.                }
  570.                else if (b==223)
  571.                {
  572.                   b=PeekSecondByte();
  573.                   if (b==224)
  574.                   {
  575.                      r=op();    if(!r) return 0; y1=result;
  576.                      r=op();    if(!r) return 0; y2=result;
  577.                      i_opcode=y1; i_mod=y2; prefixStack[i_psp++]=x;
  578.                   }
  579.                }
  580.                break;
  581.       case 16: r=op();     if(!r) return 0; x=result; // repeat group
  582.                if (x==242)
  583.                {
  584.                   while(prefixes());
  585.                   b=PeekOneByte();
  586.                   if (repeatgroupTable[b]==1)
  587.                   {
  588.                      r=op();      if(!r) return 0; y=result;
  589.                      i_opclass=16; i_opcode=y; prefixStack[i_psp++]=x;
  590.                   }
  591.                   else return 0;
  592.                }
  593.                else
  594.                {
  595.                   while(prefixes());
  596.                   b=PeekOneByte();
  597.                   if (repeatgroupTable[b]>0)
  598.                   {
  599.                      r=op();      if(!r) return 0; y=result;
  600.                      i_opclass=16; i_opcode=y; prefixStack[i_psp++]=x;  
  601.                   }
  602.                   else return 0;
  603.                }
  604.                break;
  605.       default: return 0;
  606.    }
  607.    return 1;
  608. }
  609. int twobyteinstr()
  610. {
  611. int r, b, x, y;
  612.    b=PeekOneByte();
  613.    switch(opcode2Table[b])
  614.    {
  615.       case 0: r=op();     if(!r) return 0; x=result;
  616.               i_opclass=0; i_opcode=x;
  617.               break;
  618.       case 1: r=op();     if(!r) return 0; x=result;
  619.               r=adword(); if(!r) return 0; y=result;
  620.               i_opclass=1; i_opcode=x; i_dword=y;
  621.               break;
  622.       case 2: r=op();     if(!r) return 0; x=result;
  623.               r=modrm();  if(!r) return 0;
  624.               i_opclass=2; i_opcode=x;
  625.               break;
  626.       case 3: r=op();     if(!r) return 0; x=result;
  627.               r=modrm();  if(!r) return 0;
  628.               r=byte();   if(!r) return 0; y=result;
  629.               i_opclass=3; i_opcode=x; i_byte=y;
  630.               break;
  631.       case 4: r=op();     if(!r) return 0; x=result;
  632.               r=opext();  if(!r) return 0;
  633.               i_opclass=4; i_opcode=x;
  634.               break;
  635.       case 5: r=op();     if(!r) return 0; x=result;
  636.               r=opext();  if(!r) return 0;
  637.               r=byte();   if(!r) return 0; y=result;
  638.               i_opclass=5; i_opcode=x; i_byte=y;
  639.               break;
  640.       default:
  641.               return 0;
  642.    }
  643.    return 1;
  644. }
  645. int modrm()
  646. {
  647.         if (addressOveride==0) return modrm1();
  648.    else if (addressOveride==1) return modrm2();
  649.    else return 0;
  650. }
  651. int modrm1()
  652. {
  653. int  r, b, x, y, y1, y2;
  654.  
  655.    b=PeekOneByte();
  656.    switch(modTable[b])
  657.    {
  658.       case 1: r=op();    if(!r) return 0; x=result;
  659.               i_mod=x;
  660.               break;
  661.       case 2: r=op();    if(!r) return 0; x=result;
  662.               r=op();    if(!r) return 0; y=result;
  663.               i_mod=x;   i_sib=y;
  664.               if (sibTable[y]==1)
  665.               {
  666.                  r=dword(); if(!r) return 0; y1=result;
  667.                  m_dword=y1;
  668.               }
  669.               break;
  670.       case 3: r=op();    if(!r) return 0; x=result;
  671.               r=dword(); if(!r) return 0; y=result;
  672.               i_mod=x;      m_dword=y;
  673.               break;
  674.       case 4: r=op();    if(!r) return 0; x=result;
  675.               r=byte();  if(!r) return 0; y=result;
  676.               i_mod=x;     m_byte=y;
  677.               break;
  678.       case 5: r=op();    if(!r) return 0; x=result;
  679.               r=sib();   if(!r) return 0; y1=result;
  680.               r=byte();  if(!r) return 0; y2=result;
  681.               i_mod=x;     i_sib=y1; m_byte=y2;
  682.               break;
  683.       case 6: r=op();    if(!r) return 0; x=result;
  684.               r=dword(); if(!r) return 0; y=result;
  685.               i_mod=x;     m_dword=y;
  686.               break;
  687.       case 7: r=op();    if(!r) return 0; x=result;
  688.               r=sib();   if(!r) return 0; y1=result;
  689.               r=dword(); if(!r) return 0; y2=result;
  690.               i_mod=x;     i_sib=y1; m_dword=y2;
  691.               break;
  692.       case 8: r=op();    if(!r) return 0; x=result;
  693.               i_mod=x;
  694.               break;
  695.       default: return 0;
  696.    }
  697.    return 1;
  698. }
  699. int modrm2()
  700. {
  701. int  r, b, x, y;
  702.    b=PeekOneByte();
  703.    switch(mod16Table[b])
  704.    {
  705.       case 1: r=op();   if(!r) return 0; x=result;
  706.               i_mod=x;
  707.               break;
  708.       case 2: r=op();   if(!r) return 0; x=result;
  709.               r=word(); if(!r) return 0; y=result;
  710.               i_mod=x;  m_dword=y;
  711.               break;
  712.       case 3: r=op();   if(!r) return 0; x=result;
  713.               r=byte(); if(!r) return 0; y=result;
  714.               i_mod=x;  m_byte=y;
  715.               break;
  716.       case 4: r=op();   if(!r) return 0; x=result;
  717.               r=word(); if(!r) return 0; y=result;
  718.               i_mod=x;  m_dword=y;
  719.               break;
  720.       case 5: r=op();   if(!r) return 0; x=result;
  721.               i_mod=x;
  722.               break;
  723.       default: return 0;
  724.    }
  725.    return 1;
  726. }
  727. int sib()
  728. {
  729.    return byte();
  730. }
  731. int labelstartposition()
  732. {
  733. int r;
  734.    r=dword(); if(!r) return 0;
  735.    m_dword=result;
  736.    label_start_pos=(DWORD)m_dword;
  737.    opclass=1;
  738.    if(nextMode>0)bodyprint0();
  739.    return 1;
  740. }
  741. int label1()
  742. {
  743. DWORD r, rr, s;
  744.    // I like to give this guy some more priority or power to overide some constraints
  745.    // namely It is worth try to find label blocks.... I guess
  746.    r=label_start_pos+4;s=r;
  747.    while (isLabelCheckable(r))
  748.    {
  749.        rr=(DWORD)getIntFile(r);
  750.        if (!isGoodAddress(rr)) return 1;
  751.        i_col=4;
  752.        pushTrace(305);
  753.        if (nextMode>0) EnterLabel(166, rr, r); 
  754.        popTrace();
  755.        r+=4;
  756.    }
  757.    return 1;
  758. }
  759. int opext()
  760. {
  761.    return modrm();
  762. }
  763. int opextg()
  764. {
  765.    return opext();
  766. }
  767. //...................................................................
  768.  
  769. /* globals */
  770.  
  771. int             fatalError = 0;         // flow control
  772. int             errorcount = 0;         // to use counting errors
  773. int             GotEof=0;
  774. int             yyfirsttime=1;
  775. unsigned char   c;
  776. PBYTE           yyfp, yypmax;
  777.  
  778. int ReadOneByte()
  779. {
  780.     if (yyfirsttime)
  781.     {
  782.         yyfirsttime=0; GotEof=0; 
  783.         yyfp   = (PBYTE)((int)lpFile + vCodeOffset);
  784.         yypmax = (PBYTE)((int)lpFile + CodeOffset + CodeSize);
  785.     }                                                       
  786.     if (GotEof) return EOF;
  787.     c = *yyfp++;
  788.     if (yyfp >= yypmax ) {GotEof = 1;}
  789.     return (int)c;
  790. }
  791. int PeekOneByte()
  792. {
  793.     if (yyfirsttime)
  794.         return  (int) c = *(PBYTE)((int)lpFile + vCodeOffset);
  795.     if (GotEof) return EOF;
  796.     if (yyfp >= yypmax ) return EOF; 
  797.     else return (int) c = *(yyfp);
  798. }
  799. int PeekSecondByte()
  800. {
  801.     if (GotEof) return EOF;
  802.     if (yyfp+1 >= yypmax ) return EOF; 
  803.     else return (int) c = *(yyfp+1);
  804. }