Advertisement
dllbridge

Untitled

Mar 22nd, 2024
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #include    <stdio.h>
  5.  
  6.  
  7.  
  8.  
  9. void                  monitor(char*);
  10. char* asm_strcpy(char*, const char*);
  11. char* asm_strcat(char*, const char*);
  12. char*    _strcpy(char*, const char*);
  13. char*    _strcat(char*, const char*);
  14.  
  15.  
  16. char sz1[99] =    "SONY ",
  17.      sz2[99] = "Pictures",
  18.     *psz     =         0 ;  
  19.  
  20. /////////////////////////////////////////////////////////
  21. int main(int nParam, char **ppsz)                      //  
  22. {
  23.    
  24.     if(nParam == 2)
  25.     {
  26.        
  27.        printf("From the command line, you passed the word: %s\n", ppsz[1] );   
  28.        asm_strcpy(sz2, ppsz[1] );
  29.     }
  30.    
  31. //  psz    = asm_strcpy(sz1, sz2);
  32.     psz    = asm_strcat(sz1, sz2);    
  33.     sz2[0] =  48 ;
  34.     sz2[1] =  '0';
  35.     sz2[2] =   0 ;
  36.     printf("sz1 = %s \n", sz1);    
  37.     printf("sz2 = %s \n", sz2);
  38.     printf("psz = %s \n", psz);
  39.     monitor(sz1);
  40.     scanf("%s", sz1);
  41.    
  42.     printf("_RGB = %d \n", _RGB(11, 22, 33));
  43.    
  44. return 0;
  45. }
  46.  
  47.  
  48.  
  49. #ifdef   _MSC_VER
  50. /////////////////////////////////////////////////////////
  51. char* asm_strcpy(char *psz1, const char *psz2)         //
  52. {    
  53.      __asm  // - - - - - - - - - - - - -
  54.        {                 mov  esi , psz2               //  Передача адресов строк в индексные регистры: esi
  55.                          mov  edi , psz1               //  (Source Index) и edi (Extended Destination Index)
  56.                    
  57.             L_01:        mov   al , [esi]
  58.                          mov [edi],   al
  59.                          or    al ,    0
  60.                          jz   L_02
  61.                          inc  esi
  62.                          inc  edi
  63.                          jmp  L_01
  64.             L_02:        mov  eax , psz1               //  Передача, через аккумулятор (eax) возвращаемого параметра.
  65.        };   // - - - - - - - - - - - - -
  66. }
  67. #endif
  68.  
  69.  
  70.  
  71. #ifdef   __GNUC__
  72. /////////////////////////////////////////////////////////  -masm=intel
  73. char* asm_strcpy(char *psz1, const char *psz2)         //                
  74. {
  75.    
  76.     __asm("           mov  esi ,   %1         ;"
  77.           "           mov  edi ,   %0         ;"
  78.           " L_01:     mov   al , [esi]        ;"       
  79.           "           mov [edi],   al         ;"
  80.           "           or    al ,    0         ;"
  81.           "           jz   L_02            \n\t"       //  Так тоже можно завершить строку.
  82.           "           inc  esi                ;"
  83.           "           inc  edi                ;"   
  84.           "           jmp  L_01               ;"
  85.           " L_02:                             ;"         
  86.           //    - - - - - - - - - - - - - - - -
  87.           // "m" = memory
  88.           :: "m"(psz1), "m"(psz2)
  89.            : "%eax", "%esi", "%edi"                    //  Регистры, которые нужно восстановить после использования.    
  90.           );
  91.    
  92. return psz1;   
  93. }  
  94.  
  95.  
  96. /////////////////////////////////////////////////////////  -masm=intel
  97. char* asm_strcat(char *psz1, const char *psz2)         //                
  98. {
  99.    
  100.     __asm("           mov  edi ,   %0         ;"
  101.           "           dec  edi                ;"   
  102.           " L_03:     inc  edi                ;"   
  103.           "           mov   al , [edi]        ;"   
  104.           "           or    al ,    0         ;"                   
  105.           "           jnz  L_03               ;"    
  106.           "           mov  esi ,   %1         ;"
  107.           " L_04:     mov   al , [esi]        ;"       
  108.           "           mov [edi],   al         ;"
  109.           "           or    al ,    0         ;"
  110.           "           jz   L_05               ;"       //  Так тоже можно завершить строку.
  111.           "           inc  esi                ;"
  112.           "           inc  edi                ;"   
  113.           "           jmp  L_04               ;"
  114.           " L_05:                             ;"         
  115.           //    - - - - - - - - - - - - - - - -
  116.           :: "m"(psz1), "m"(psz2)
  117.            : "%eax", "%esi", "%edi"                    //  Регистры, которые нужно восстановить после использования.    
  118.           );
  119.    
  120. return psz1;   
  121. }  
  122.  
  123. #endif
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. /////////////////////////////////////////////////////////
  131. char* _strcpy(char *psz1, const char *psz2)            //
  132. {  
  133.     int   i =    0;
  134.     char *p = psz1;
  135.    
  136.     while(psz2[i] != 0) psz1[i] = psz2[i++];
  137.        
  138.     psz1[i] = 0;  
  139.      
  140. return psz1;     
  141. }
  142.  
  143. /////////////////////////////////////////////////////////
  144. char* _strcat(char *psz1, const char *psz2)            //
  145. {  
  146.     int   i =    0,
  147.           k =    0;
  148.     char *p = psz1;
  149.    
  150.     while(psz1[++k]);      
  151.     while(psz2[i] != 0) psz1[i+k] = psz2[i++];
  152.        
  153.     psz1[i+k] = 0;  
  154.      
  155. return psz1;     
  156. }
  157.  
  158.  
  159. /////////////////////////////////////////////////////////
  160. void monitor(char *psz)                                //
  161. {
  162.     int i = 0;       printf("\n We print from the \"monitor\" function:\n");  
  163.                      printf("Codes of all characters in the psz string:\n");
  164.      while(psz[i]) { printf("psz[%2d] = %c = %3d\n", i, psz[i], psz[i]); i++; }
  165.                      printf("psz[%2d] = %c = %3d\n", i, psz[i], psz[i]);  
  166. }
  167.  
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement