Machine code

From Wikipedia for FEVERv2
Jump to navigation Jump to search

For code that is completely internal to some CPUs and normally inaccessible to programmers, see Microcode. Machine code_sentence_0

In computer programming, machine code, consisting of machine language instructions, is a low-level programming language used to directly control a computer's central processing unit (CPU). Machine code_sentence_1

Each instruction causes the CPU to perform a very specific task, such as a load, a store, a jump, or an arithmetic logic unit (ALU) operation on one or more units of data in the CPU's registers or memory. Machine code_sentence_2

Machine code is a strictly numerical language which is intended to run as fast as possible, and may be regarded as the lowest-level representation of a compiled or assembled computer program or as a primitive and hardware-dependent programming language. Machine code_sentence_3

While it is possible to write programs directly in machine code, managing individual bits and calculating numerical addresses and constants manually is tedious and error-prone. Machine code_sentence_4

For this reason, programs are very rarely written directly in machine code in modern contexts, but may be done for low level debugging, program patching (especially when assembler source is not available) and assembly language disassembly. Machine code_sentence_5

The overwhelming majority of practical programs today are written in higher-level languages or assembly language. Machine code_sentence_6

The source code is then translated to executable machine code by utilities such as compilers, assemblers, and linkers, with the important exception of interpreted programs, which are not translated into machine code. Machine code_sentence_7

However, the interpreter itself, which may be seen as an executor or processor performing the instructions of the source code, typically consists of directly executable machine code (generated from assembly or high-level language source code). Machine code_sentence_8

Machine code is by definition the lowest level of programming detail visible to the programmer, but internally many processors use microcode or optimise and transform machine code instructions into sequences of micro-ops. Machine code_sentence_9

This is not generally considered to be a machine code. Machine code_sentence_10

Instruction set Machine code_section_0

Main article: Instruction set Machine code_sentence_11

Every processor or processor family has its own instruction set. Machine code_sentence_12

Instructions are patterns of bits, digits or characters that by physical design correspond to different commands to the machine. Machine code_sentence_13

Thus, the instruction set is specific to a class of processors using (mostly) the same architecture. Machine code_sentence_14

Successor or derivative processor designs often include all the instructions of a predecessor and may add additional instructions. Machine code_sentence_15

Occasionally, a successor design will discontinue or alter the meaning of some instruction code (typically because it is needed for new purposes), affecting code compatibility to some extent; even nearly completely compatible processors may show slightly different behavior for some instructions, but this is rarely a problem. Machine code_sentence_16

Systems may also differ in other details, such as memory arrangement, operating systems, or peripheral devices. Machine code_sentence_17

Because a program normally relies on such factors, different systems will typically not run the same machine code, even when the same type of processor is used. Machine code_sentence_18

A processor's instruction set may have all instructions of the same length, or it may have variable-length instructions. Machine code_sentence_19

How the patterns are organized varies strongly with the particular architecture and often also with the type of instruction. Machine code_sentence_20

Most instructions have one or more opcode fields which specifies the basic instruction type (such as arithmetic, logical, jump, etc.) and the actual operation (such as add or compare) and other fields that may give the type of the operand(s), the addressing mode(s), the addressing offset(s) or index, or the actual value itself (such constant operands contained in an instruction are called immediates). Machine code_sentence_21

Not all machines or individual instructions have explicit operands. Machine code_sentence_22

An accumulator machine has a combined left operand and result in an implicit accumulator for most arithmetic instructions. Machine code_sentence_23

Other architectures (such as 8086 and the x86-family) have accumulator versions of common instructions, with the accumulator regarded as one of the general registers by longer instructions. Machine code_sentence_24

A stack machine has most or all of its operands on an implicit stack. Machine code_sentence_25

Special purpose instructions also often lack explicit operands (CPUID in the x86 architecture writes values into four implicit destination registers, for instance). Machine code_sentence_26

This distinction between explicit and implicit operands is important in code generators, especially in the register allocation and live range tracking parts. Machine code_sentence_27

A good code optimizer can track implicit as well as explicit operands which may allow more frequent constant propagation, constant folding of registers (a register assigned the result of a constant expression freed up by replacing it by that constant) and other code enhancements. Machine code_sentence_28

Programs Machine code_section_1

A computer program is a list of instructions that can be executed by a central processing unit (CPU). Machine code_sentence_29

A program's execution is done in order for the CPU that is executing it to solve a specific problem and thus accomplish a specific result. Machine code_sentence_30

While simple processors are able to execute instructions one after another, superscalar processors are capable of executing a variety of different instructions at once. Machine code_sentence_31

Program flow may be influenced by special 'jump' instructions that transfer execution to an instruction other than the numerically following one. Machine code_sentence_32

Conditional jumps are taken (execution continues at another address) or not (execution continues at the next instruction) depending on some condition. Machine code_sentence_33

Assembly languages Machine code_section_2

Main article: Assembly language Machine code_sentence_34

A much more readable rendition of machine language, called assembly language, uses mnemonic codes to refer to machine code instructions, rather than using the instructions' numeric values directly, and uses symbolic names to refer to storage locations and sometimes registers. Machine code_sentence_35

For example, on the Zilog Z80 processor, the machine code 00000101, which causes the CPU to decrement the B processor register, would be represented in assembly language as DEC B. Machine code_sentence_36

Example Machine code_section_3

The MIPS architecture provides a specific example for a machine code whose instructions are always 32 bits long. Machine code_sentence_37

The general type of instruction is given by the op (operation) field, the highest 6 bits. Machine code_sentence_38

J-type (jump) and I-type (immediate) instructions are fully specified by op. R-type (register) instructions include an additional field funct to determine the exact operation. Machine code_sentence_39

The fields used in these types are: Machine code_sentence_40

rs, rt, and rd indicate register operands; shamt gives a shift amount; and the address or immediate fields contain an operand directly. Machine code_sentence_41

For example, adding the registers 1 and 2 and placing the result in register 6 is encoded: Machine code_sentence_42

Load a value into register 8, taken from the memory cell 68 cells after the location listed in register 3: Machine code_sentence_43

Jumping to the address 1024: Machine code_sentence_44

Relationship to microcode Machine code_section_4

In some computer architectures, the machine code is implemented by an even more fundamental underlying layer called microcode, providing a common machine language interface across a line or family of different models of computer with widely different underlying dataflows. Machine code_sentence_45

This is done to facilitate porting of machine language programs between different models. Machine code_sentence_46

An example of this use is the IBM System/360 family of computers and their successors. Machine code_sentence_47

With dataflow path widths of 8 bits to 64 bits and beyond, they nevertheless present a common architecture at the machine language level across the entire line. Machine code_sentence_48

Using microcode to implement an emulator enables the computer to present the architecture of an entirely different computer. Machine code_sentence_49

The System/360 line used this to allow porting programs from earlier IBM machines to the new family of computers, e.g. an IBM 1401/1440/1460 emulator on the IBM S/360 model 40. Machine code_sentence_50

Relationship to bytecode Machine code_section_5

Machine code is generally different from bytecode (also known as p-code), which is either executed by an interpreter or itself compiled into machine code for faster (direct) execution. Machine code_sentence_51

An exception is when a processor is designed to use a particular bytecode directly as its machine code, such as is the case with Java processors. Machine code_sentence_52

Machine code and assembly code are sometimes called native code when referring to platform-dependent parts of language features or libraries. Machine code_sentence_53

Storing in memory Machine code_section_6

The Harvard architecture is a computer architecture with physically separate storage and signal pathways for the code (instructions) and data. Machine code_sentence_54

Today, most processors implement such separate signal pathways for performance reasons but implement a Modified Harvard architecture, so they can support tasks like loading an executable program from disk storage as data and then executing it. Machine code_sentence_55

Harvard architecture is contrasted to the Von Neumann architecture, where data and code are stored in the same memory which is read by the processor allowing the computer to execute commands. Machine code_sentence_56

From the point of view of a process, the code space is the part of its address space where the code in execution is stored. Machine code_sentence_57

In multitasking systems this comprises the program's code segment and usually shared libraries. Machine code_sentence_58

In multi-threading environment, different threads of one process share code space along with data space, which reduces the overhead of context switching considerably as compared to process switching. Machine code_sentence_59

Readability by humans Machine code_section_7

Pamela Samuelson wrote that machine code is so unreadable that the United States Copyright Office cannot identify whether a particular encoded program is an original work of authorship; however, the US Copyright Office does allow for copyright registration of computer programs and a program's machine code can sometimes be decompiled in order to make its functioning more easily understandable to humans. Machine code_sentence_60

Cognitive science professor Douglas Hofstadter has compared machine code to genetic code, saying that "Looking at a program written in machine language is vaguely comparable to looking at a DNA molecule atom by atom." Machine code_sentence_61

See also Machine code_section_8

Machine code_unordered_list_0

Credits to the contents of this page go to the authors of the corresponding Wikipedia page: code.