What is Native Code?

后端 未结 4 568
慢半拍i
慢半拍i 2021-02-04 04:23

The Project\'s Web section (under project properties in VS2008) has a list of debuggers: ASP.NET, Native Code, SQL Server. What is Native Code?

相关标签:
4条回答
  • 2021-02-04 04:40

    Native code doesn't run on the Common Language Runtime (CLR). An example would be a non-managed C++ application.

    0 讨论(0)
  • 2021-02-04 04:51

    Native code is essentially data in memory that the central processing chip in the computer can read and execute directly. Think of the CPU sucking in data, and that data flipping switches as it goes through, turning things off and on:

       [  CPU  ] ==================================== [  RAM  ]
         ^^^^^
         |   |
    
         LOAD _memoryAddress12, D1   ; tells the CPU to get data from slot 12
                                     ; in RAM, and put it in register D1 inside the CPU
    
         ^^^^^
         |   |
    
         ADD D1, 24                  ; tells the CPU to do an internal calculation
    
         ^^^^^
         |   |
    
         STORE R0, _memoryAddress23 ; tells the CPU to put the answer into slot 23 in ram
    

    You can think of the instructions like punch cards, or those musical piano roll things, that flip switches in the CPU as they go in. The important part is that this is in HARDWARE: it's literally happening on wires / circuitry, almost at the speed of light. But there are a lot of switches to flip. So, each of those "native instructions" going into the machine gets executed at the "clock speed" of the machine (around 2.5 billion times per second on a modern PC). In reality, it's a bit more complex, with some instructions taking a bit longer, some being done at the same time, etc.

    Now, in contrast, virtual machines run non-native code (often called bytecode), literally on a virtual, fake machine. When it comes to languages, a virtual machine is a program that runs ANOTHER program, instead of the program just running directly in hardware. So, where the above program loads data, adds to it, and stores a result in just three native instructions a virtual program might do something more like this (Disclaimer: this is rusty, pseudo-assembly code):

       load _firstInstruction, D1
       if_equal D1, 12
       jump _itsAnAddInstructionHandleIt
       if_equal D1, 13
       jump _itsASubstractInstructionHandleIt
       if_equal D1, 14
       jump _itsAMultiplyInstructionHandleIt
       if_equal D1, 15
       jump _itsADivideInstructionHandleIt
       if_equal D1, 16
       jump _itsALoadInstructionHandleIt
       ...
    
    _itsALoadInstructionHandleIt:
       load D1, D2
       add 4, D2
       load D2, D3
       return
    

    And so on. This is just an example of handling ONE of the above native instructions in a non-native way: about 10 instructions (depending on implementation) instead of the first, single, native instruction, and I left out important details, like unboxing data! The point is that, probably in an average of about 20-30 instructions later, you'll have accomplished the same thing as ONE line from the native code above.

    NOW. All that said, GOOD virtual machines have a JIT, which can convert SOME of this into native code as they're executed, or just before executing them. But there are a lot of things, like Boxed types, which can't be directly converted, because the whole point of a virtual machine is that it doesn't work in the a low level, circuitry-friendly way, that native code would. Virtual machines are easier to program, but much slower.

    Another huge disadvantage of virtual machines is that they often have big memory overheads, which makes them pretty useless if you want to code up millions of items of data all in memory at the same time. At times like that, a VM, though intended to make code higher-level and more readable, can force you to do LOWER-level, nastier things than native code, because the benefits start to become drawbacks.

    0 讨论(0)
  • 2021-02-04 04:51

    For starters, the native code is just an Intermediate Language tailored to run on a particular assembly. It resembles the object code as in other HLLs.

    0 讨论(0)
  • 2021-02-04 05:01

    Native code is machine code executed directly by the CPU. This is in contrast to .NET bytecode, which is interpreted by the .NET virtual machine.

    A nice MSDN hit: Debugging Native Code

    0 讨论(0)
提交回复
热议问题