Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. C and Machine Code horror

C and Machine Code horror

Scheduled Pinned Locked Moved The Weird and The Wonderful
performance
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timothy Baldwin
    wrote on last edited by
    #1
        /\*Generate mreadmem\*/
        blockpoint2=BLOCKS+1;
        codeblockpos=0;
        addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/
        addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/
        addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/
        addlong(vraddrl);
        addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/
        addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/
        addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/
        addbyte(0xC3); /\*RET\*/
        addbyte(0x57); /\*PUSH %edi\*/
        addbyte(0xE8); /\*CALL\*/
        addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\]));
        addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/
        addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/
        addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/
        addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/
        addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/
        addbyte(0xC3); /\*RET\*/
    

    There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.

    P P S L 4 Replies Last reply
    0
    • T Timothy Baldwin
          /\*Generate mreadmem\*/
          blockpoint2=BLOCKS+1;
          codeblockpos=0;
          addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/
          addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/
          addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/
          addlong(vraddrl);
          addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/
          addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/
          addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/
          addbyte(0xC3); /\*RET\*/
          addbyte(0x57); /\*PUSH %edi\*/
          addbyte(0xE8); /\*CALL\*/
          addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\]));
          addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/
          addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/
          addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/
          addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/
          addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/
          addbyte(0xC3); /\*RET\*/
      

      There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      Timothy Baldwin wrote:

      The only way I can think of making this worse is to remove the comments.

      I agree. It would serve as potential job security though.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

      1 Reply Last reply
      0
      • T Timothy Baldwin
            /\*Generate mreadmem\*/
            blockpoint2=BLOCKS+1;
            codeblockpos=0;
            addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/
            addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/
            addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/
            addlong(vraddrl);
            addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/
            addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/
            addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/
            addbyte(0xC3); /\*RET\*/
            addbyte(0x57); /\*PUSH %edi\*/
            addbyte(0xE8); /\*CALL\*/
            addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\]));
            addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/
            addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/
            addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/
            addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/
            addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/
            addbyte(0xC3); /\*RET\*/
        

        There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Ohhh... so that's how you do that...

        1 Reply Last reply
        0
        • T Timothy Baldwin
              /\*Generate mreadmem\*/
              blockpoint2=BLOCKS+1;
              codeblockpos=0;
              addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/
              addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/
              addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/
              addlong(vraddrl);
              addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/
              addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/
              addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/
              addbyte(0xC3); /\*RET\*/
              addbyte(0x57); /\*PUSH %edi\*/
              addbyte(0xE8); /\*CALL\*/
              addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\]));
              addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/
              addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/
              addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/
              addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/
              addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/
              addbyte(0xC3); /\*RET\*/
          

          There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.

          S Offline
          S Offline
          supercat9
          wrote on last edited by
          #4

          There are cases where I would consider such code appropriate. If the routine in question will represent 90% of the running time of the program in which it resides, writing the routine as indicated will reduce its own running time by 60%, the running time of the program will be significant, and there is no other practical way to achieve such a speedup, then such code could be reasonable. Such code may also be reasonable in cases where one wishes to thwart disassembly. In such cases, one may have to tolerate some messiness in the source to obfuscate the machine code. If obfuscation of the machine code is a bona fide and legitimate goal, the nasty source code may be an acceptable price to pay. The above code doesn't seem to contain any loops, so it wouldn't have much use as a speedup method. It might be designed to discourage reverse-engineering, though there would be better ways of accomplishing that.

          T 1 Reply Last reply
          0
          • T Timothy Baldwin
                /\*Generate mreadmem\*/
                blockpoint2=BLOCKS+1;
                codeblockpos=0;
                addbyte(0x89); addbyte(0xFA); /\*MOVL %edi,%edx\*/
                addbyte(0xC1); addbyte(0xEA); addbyte(12); /\*SHR $12,%edx\*/
                addbyte(0x8B); addbyte(0x0C); addbyte(0x95); /\*MOV vraddrl(,%edx,4),%ecx\*/
                addlong(vraddrl);
                addbyte(0xF6); addbyte(0xC1); addbyte(1); /\*TST %cl,1\*/
                addbyte(0x75); addbyte(4); /\*JNZ notinbuffer\*/
                addbyte(0x8B); addbyte(0x14); addbyte(0x39); /\*MOVL (%ecx,%edi),%edx\*/
                addbyte(0xC3); /\*RET\*/
                addbyte(0x57); /\*PUSH %edi\*/
                addbyte(0xE8); /\*CALL\*/
                addlong(readmemfl-(uint32\_t)(&rcodeblock\[blockpoint2\]\[codeblockpos+4\]));
                addbyte(0x89); addbyte(0xF9); /\*MOVL %edi,%ecx\*/
                addbyte(0xC1); addbyte(0xE9); addbyte(12); /\*SHR $12,%ecx\*/
                addbyte(0x83); addbyte(0xC4); addbyte(0x04); /\*ADDL $4,%esp\*/
                addbyte(0x89); addbyte(0xC2); /\*MOVL %eax,%edx\*/
                addbyte(0x8B); addbyte(0x0C); addbyte(0x8D); addlong(vraddrl); /\*MOV vraddrl(,%ecx,4),%ecx\*/
                addbyte(0xC3); /\*RET\*/
            

            There is written to memory and executed there. The only way I can think of making this worse is to remove the comments.

            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #5

            Too much code, too much writing. It could be done in a simple and write-only way like this:

            = {0x89,0xFA,0xC1,0xEA,0x0C,0x8B,...,0xC3 };

            addbytes(mreadmem);

            Sometimes if your code is crap it is better to asm it down to make others thinking "Uh, better not touch that".

            Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

            1 Reply Last reply
            0
            • S supercat9

              There are cases where I would consider such code appropriate. If the routine in question will represent 90% of the running time of the program in which it resides, writing the routine as indicated will reduce its own running time by 60%, the running time of the program will be significant, and there is no other practical way to achieve such a speedup, then such code could be reasonable. Such code may also be reasonable in cases where one wishes to thwart disassembly. In such cases, one may have to tolerate some messiness in the source to obfuscate the machine code. If obfuscation of the machine code is a bona fide and legitimate goal, the nasty source code may be an acceptable price to pay. The above code doesn't seem to contain any loops, so it wouldn't have much use as a speedup method. It might be designed to discourage reverse-engineering, though there would be better ways of accomplishing that.

              T Offline
              T Offline
              Timothy Baldwin
              wrote on last edited by
              #6

              The C code only executes once. It would have been better to use an assembler! However most of this file is a JIT complier written in a similar style - a complete absence of symbolic constants. And it is slow. As for discouraging reverse-engineering, this code is published under the GNU General Public Licence.

              S 1 Reply Last reply
              0
              • T Timothy Baldwin

                The C code only executes once. It would have been better to use an assembler! However most of this file is a JIT complier written in a similar style - a complete absence of symbolic constants. And it is slow. As for discouraging reverse-engineering, this code is published under the GNU General Public Licence.

                S Offline
                S Offline
                supercat9
                wrote on last edited by
                #7

                Timothy Baldwin wrote:

                As for discouraging reverse-engineering, this code is published under the GNU General Public Licence

                If it weren't for the comments, that might explain it. BTW, would the GPL require the release of source code for things like p-code which is interpreted by the rest of the program? What if the source code for the p-code never existed (because it was hand-generated)?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups