MgDisasm.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // MgDisasm.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "MgAsmCom.h"
  5. #include <cstdlib>
  6. CMgDisasmBase g_Disasm;
  7. CMgAsmBase g_Asm;
  8. int main(int argc, char *argv[])
  9. {
  10. int i, j, n;
  11. ulong l;
  12. char s[TEXTLEN], errtext[TEXTLEN];
  13. char *pasm;
  14. CMgDisasmBase::t_disasm da;
  15. CMgAsmBase::t_asmmodel am;
  16. // Demonstration of Disassembler.
  17. printf("Disassembler:\n");
  18. //// Quickly determine size of command.
  19. //l=g_Disasm.Disasm("\xB8\x50\x11\x40\x00\xFF\xD0",
  20. // 10,0x400000,&da,DISASM_SIZE);
  21. //printf("Size of command = %i bytes\n",l);
  22. //ADD [475AE0],1 MASM mode, lowercase, don't show default segment
  23. g_Disasm.m_nIDEAL=0; g_Disasm.m_nLowercase=1; g_Disasm.m_nPutDefSeg=0;
  24. l=g_Disasm.Disasm("\xE8\x4B\x11\x00\x00",
  25. 10,0x400000,&da,DISASM_CODE);
  26. printf("%3i %-24s %-24s (MASM)\n",l,da.dump,da.result);
  27. // ADD [475AE0],1 IDEAL mode, uppercase, show default segment
  28. //g_Disasm.m_nIDEAL=1; g_Disasm.m_nLowercase=0; g_Disasm.m_nPutDefSeg=1;
  29. //l=g_Disasm.Disasm("\xE8\x4B\x11\x00\x00",
  30. // 10,0x400000,&da,DISASM_CODE);
  31. //printf("%3i %-24s %-24s (IDEAL)\n",l,da.dump,da.result);
  32. //// CALL 45187C
  33. //l=g_Disasm.Disasm("\xE8\x1F\x14\x00\x00",
  34. // 5,0x450458,&da,DISASM_CODE);
  35. //printf("%3i %-24s %-24s jmpconst=%08X\n",l,da.dump,da.result,da.jmpconst);
  36. //
  37. //// JNZ 450517
  38. //l=g_Disasm.Disasm("\xE8\x1F\x14\x00\x00",
  39. // 2,0x4504A3,&da,DISASM_CODE);
  40. //printf("%3i %-24s %-24s jmpconst=%08X\n",l,da.dump,da.result,da.jmpconst);
  41. //g_Disasm.m_nIDEAL=0; g_Disasm.m_nLowercase=1; g_Disasm.m_nPutDefSeg=0;
  42. //l=g_Disasm.Disasm("\x0F\x9E\xC0",
  43. // 10,0x400000,&da,DISASM_CODE);
  44. //printf("%3i %-24s %-24s (MASM)\n",l,da.dump,da.result);
  45. // Demonstration of Assembler.
  46. printf("\nAssembler:\n");
  47. // Assemble one of the commands above. First try form with 32-bit immediate.//32λ
  48. pasm = "mov eax,dword ptr [40E000]";
  49. printf("%s:\n", pasm);
  50. j = g_Asm.Assemble(pasm, 0x400000, &am, 0, 0, errtext);
  51. n = sprintf(s, "%3i ", j);
  52. for (i = 0; i < j; i++)
  53. n += sprintf(s + n, "%02X ", am.code[i]);
  54. if (j <= 0)
  55. sprintf(s + n, " error=\"%s\"", errtext);
  56. printf("%s\n", s);
  57. // Then variant with 8-bit immediate constant.//8λ
  58. //j=g_Asm.Assemble(pasm,0x400000,&am,0,2,errtext);
  59. //n=sprintf(s,"%3i ",j);
  60. //for (i=0; i<j; i++) n+=sprintf(s+n,"%02X ",am.code[i]);
  61. //if (j<=0) sprintf(s+n," error=\"%s\"",errtext);
  62. //printf("%s\n",s);
  63. //// Error, unable to determine size of operands.
  64. //pasm="MOV [475AE0],1";
  65. //printf("%s:\n",pasm);
  66. //j=g_Asm.Assemble(pasm,0x400000,&am,0,0,errtext);
  67. //n=sprintf(s,"%3i ",j);
  68. //for (i=0; i<j; i++) n+=sprintf(s+n,"%02X ",am.code[i]);
  69. //if (j<=0) sprintf(s+n," error=\"%s\"",errtext);
  70. //printf("%s\n",s);
  71. // //
  72. //pasm="setle al";
  73. //printf("%s:\n",pasm);
  74. //j=g_Asm.Assemble(pasm,0x400000,&am,0,0,errtext);
  75. //n=sprintf(s,"%3i ",j);
  76. //for (i=0; i<j; i++) n+=sprintf(s+n,"%02X ",am.code[i]);
  77. //if (j<=0) sprintf(s+n," error=\"%s\"",errtext);
  78. //printf("%s\n",s);
  79. // Show results.
  80. system("pause");
  81. return 0;
  82. }