We Recommend

Object-Oriented Programming in Pascal: A Graphical Approach Object-Oriented Programming in Pascal: A Graphical Approach
Covers all of the essential concepts of objectoriented programming, including object re-use, inheritance, virtual methods, and polymorphisms.


Ballyhoo


Posted By

kodeape on 02/08/07


Tagged


Versions (?)


Jump size optimization...


Published in: Assembler 


  1. // Author: Orlando Llanes
  2. //
  3. // This code is hereby donated to the public domain
  4. //
  5. // asm.cpp : Defines the entry point for the console application.
  6. //
  7.  
  8. #include "stdafx.h"
  9.  
  10. #include <ctype.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. typedef struct
  16. {
  17. int Source;
  18. int Target;
  19. int ActualSize;
  20. int Size;
  21. } TBranchOp;
  22.  
  23. int main(int argc, char* argv[])
  24. {
  25. char Src[] = "j4 :2 n n j5 :3 n j2 :4 j3 :5 r";
  26. int SrcPos = 0;
  27.  
  28. unsigned __int8 Code[1024];
  29. int CodeOfs = 0;
  30.  
  31. int Target[10];
  32.  
  33. TBranchOp Branch[1024];
  34. int nBranch = 0;
  35.  
  36. int Tmp;
  37. int i;
  38. int j;
  39. int k;
  40. int l;
  41.  
  42. int SizeDiff;
  43.  
  44. char Ch;
  45.  
  46. FILE* Out;
  47.  
  48. memset( Code, 0, sizeof Code );
  49. memset( Target, 0xFF, sizeof Target );
  50. memset( Branch, 0, sizeof Branch );
  51.  
  52. while( Src[SrcPos] )
  53. {
  54. do
  55. {
  56. Ch = Src[SrcPos++];
  57. } while( isspace(Ch) );
  58.  
  59. switch( Ch )
  60. {
  61. case 'j':
  62. Ch = Src[SrcPos++];
  63. printf( " jmp %c\n", Ch );
  64.  
  65. Branch[nBranch].Source = CodeOfs;
  66. Branch[nBranch].Target = Ch;
  67. Branch[nBranch].ActualSize = 2;
  68. Branch[nBranch].Size = 2;
  69. nBranch++;
  70.  
  71. Code[CodeOfs++] = 0xEB;
  72. Code[CodeOfs++] = 0;
  73. break;
  74.  
  75. case ':':
  76. Ch = Src[SrcPos++];
  77. printf( "%c:\n", Ch );
  78. if( Target[Ch - '0'] != -1 )
  79. {
  80. printf( "Error: Label %c has already been declared\n", Ch );
  81. return 0;
  82. }
  83. Target[Ch - '0'] = CodeOfs;
  84. break;
  85.  
  86. case 'n':
  87. printf( " nop\n" );
  88. Code[CodeOfs++] = 0x90;
  89. break;
  90.  
  91. case 'r':
  92. printf( " ret\n" );
  93. Code[CodeOfs++] = 0xC3;
  94. break;
  95. }
  96. }
  97.  
  98. // Grow jump targets in place
  99. do
  100. {
  101. SizeDiff = 0;
  102.  
  103. for( i = 0; i < nBranch; i++ )
  104. {
  105. j = Branch[i].Source;
  106. k = Branch[i].Target;
  107. l = Target[k - '0'];
  108.  
  109. if( l == -1 )
  110. {
  111. printf( "Error: Label '%c' undeclared.\n", k );
  112. return 0;
  113. }
  114.  
  115. Tmp = l - j - Branch[i].Size;
  116.  
  117. if( (abs(Tmp) > 128) && (Branch[i].Size <= 2) )
  118. {
  119. for( k = 0; k < 10; k++ )
  120. {
  121. if( Target[k] > Branch[i].Source )
  122. {
  123. if( Target[k] )
  124. Target[k]++;
  125. }
  126. }
  127.  
  128. for( k = 0; k < nBranch; k++ )
  129. {
  130. if( Branch[k].Source > Branch[i].Source )
  131. Branch[k].Source++;
  132. }
  133.  
  134. memmove( &Code[j + 3], &Code[j + 2], CodeOfs - j - 1 );
  135. CodeOfs++;
  136.  
  137. Code[j] = 0xE9;
  138. SizeDiff++;
  139. Branch[i].Size++;
  140. }
  141. }
  142. } while( SizeDiff );
  143.  
  144. // Fill in jump targets
  145. for( i = 0; i < nBranch; i++ )
  146. {
  147. j = Branch[i].Source;
  148. k = Branch[i].Target;
  149. l = Target[k - '0'];
  150.  
  151. Tmp = l - j - Branch[i].Size;
  152.  
  153. Code[j + 1] = Tmp;
  154. if( Branch[i].Size >= 3 )
  155. Code[j + 2] = Tmp >> 8;
  156. }
  157.  
  158. Out = fopen("a.com", "w+b");
  159. if( Out )
  160. {
  161. fwrite( Code, 1, CodeOfs, Out );
  162.  
  163. fclose( Out );
  164. }
  165.  
  166. for( i = 0; i < CodeOfs; i++ )
  167. printf( "0x%.2X\n", Code[i] );
  168.  
  169. return 0;
  170. }

Report this snippet 

You need to login to post a comment.