The Full Wiki

Machine code: Wikis

Advertisements
  
  

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

Encyclopedia

From Wikipedia, the free encyclopedia

Machine code or machine language is a system of instructions and data executed directly by a computer's central processing unit. Machine code may be regarded as a primitive (and cumbersome) programming language or as the lowest-level representation of a compiled and/or assembled computer program. Programs in interpreted languages [1] are not represented by machine code however, although their interpreter (which may be seen as a processor executing the higher level program) often is. Machine code is sometimes called native code when referring to platform-dependent parts of language features or libraries.[2] Machine code should not be confused with so called "bytecode", which is executed by an interpreter.

Contents

Machine code instructions

Every processor or processor family has its own machine code instruction set. Instructions are patterns of bits that by physical design correspond to different commands to the machine. The instruction set is thus specific to a class of processors using (much) the same architecture. Successor or derivative processor designs often include all the instructions of a predecessor and may add additional instructions. 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 seldom a problem. Systems may also differ in other details, such as memory arrangement, operating systems, or peripheral devices; 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.

A machine code instruction set may have all instructions of the same length, or it may have variable-length instructions. How the patterns are organized varies strongly with the particular architecture and often also with the type of instruction. 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).

Programs

A computer program is a sequence of instructions that are executed by a CPU. While simple processors execute instructions one after the other, superscalar processors are capable of executing several instructions at once.

Program flow may be influenced by special 'jump' instructions that transfer execution to an instruction other than the following one. Conditional jumps are taken (execution continues at another address) or not (execution continues at the next instruction) depending on some condition.

Assembly languages

A much more readable rendition of machine language, called assembly language, uses mnemonic codes to refer to machine code instructions, rather than simply using the instructions' numeric values. 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.

Example

The MIPS architecture provides a specific example for a machine code whose instructions are always 32 bits long. The general type of instruction is given by the op (operation) field, the highest 6 bits. 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. The fields used in these types are:

   6      5     5     5     5      6 bits
[  op  |  rs |  rt |  rd |shamt| funct]  R-type
[  op  |  rs |  rt | address/immediate]  I-type
[  op  |        target address        ]  J-type

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

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

[  op  |  rs |  rt |  rd |shamt| funct]
    0     1     2     6     0     32     decimal
 000000 00001 00010 00110 00000 100000   binary

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

[  op  |  rs |  rt | address/immediate]
   35     3     8           68           decimal
 100011 00011 01000 00000 00001 000100   binary

Jumping to the address 1024:

[  op  |        target address        ]
    2                 1024               decimal
 000010 00000 00000 00000 10000 000000   binary

Relationship to microcode

In some computer architectures, the machine code is implemented by a more fundamental underlying layer of programs called microprograms, providing a common machine language interface across a line or family of different models of computer with widely different underlying dataflows. This is done to facilitate porting of machine language programs between different models. An example of this use is the IBM System/360 family of computers and their successors. 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.

Using a microcode layer to implement an emulator enables the computer to present the architecture of an entirely different computer. 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.

See also

Further reading

  • Hennessy, John L.; Patterson, David A.. Computer Organization and Design. The Hardware/Software Interface.. Morgan Kaufmann Publishers. ISBN 1-55860-281-X.  
  • Tanenbaum, Andrew S.. Structured Computer Organization. Prentice Hall. ISBN 0-13-020435-8.  
  • Brookshear, J. Glenn. Computer Science: An Overview. Addison Wesley. ISBN 0321387015.  

Notes and references

Advertisements

Simple English

Machine code or machine language is the name for commands. They can directly be executed by a processor. Usually, they are 1s and 0s. Their order tells the computer what to do.[1][2][3][4] This code is the lowest level of software. All other kinds of software need to be translated into machine code before they can be used.

Each processor has its own machine code.[5]

Each instruction is made up of an opcode (operation code) and operand(s). An instruction tells the computer to do one thing. The operands are usually memory addresses. An instruction set is a list of the opcodes used in a computer. Machine code is what assemble code. Other programming languages are compiled to or interpreted as.

Program builders turn code into another language or machine code. Machine code is sometimes called native code. This is used when talking about things that work on only some computers.[6]

Contents

Writing machine code

Machine code can be written in different forms:

  • Using a number of switches. This generates a sequence of 1 and 0. This was used in the early days of computing. Since the 1970s, it is no longer used.
  • Using a Hex editor. This allows to use opcodes instead of the number of the command. This is still very cryptic.
  • Using Assembler. Assembler languages are simpler than opcodes. Their syntax is easy to read. The assembler will translate the source code into machine code on its own.
  • Using a High-level programming language. This allows programs that use abstraction. These programs are translated into machine code. The translation can happen in many steps. Java programs are first translated into bytecode. Then it is turned into machine language when it is used.

Typical instructions of machine code

Machine code usually offers many kinds of instructions:

  • Arithmetical operations: Addition, subtraction, multiplication, division.
  • Logical operations: Conjunction, disjunction, negation.
  • Operations acting on single bits: Shift left, shift right.
  • Operations acting on memory: copy a value from one register to another.
  • Operations that compare two values: bigger than, smaller than, equal.
  • Operations that combine other operations: add, compare, and copy if equal to some value(as one operation), jump to some point in the program if a register is zero.
  • Operations that act on program flow: jump to some address.
  • Operations that convert data types: eg. convert a 32 bit integer to a 64 bit integer, convert a floating point value to an integer (by truncating).

Many modern processors use microcode for some of the commands. More complex commands tend to use it. This is often done with CISC architectures.

Instructions

Every processor or processor family has its own machine code instruction set. Instructions are patterns of bits. They correspond to the different commands that can be given. An instruction set is specific to a processor or a family of processors that have a similar set up. Newer processors often copy all of its instructions. Processors that try to be similar to the original processor can also copy all of its instructions. Often, newer processors add more instructions not present in the original design.

Sometimes a newer processor will change the meaning of an instruction. Sometimes it will no longer support it. This can change the compatibility of the code. Some old code will no longer work on the newer processor. Processors that are similar will sometime act different. This is rarely a problem. Many systems may also be different in other ways. Their access to the memory or its arrangement may be different. Computer hardware may be connected in a different way. Also, the access to other hardware could be changed. A program normally relies on such factors. For this reason, different systems will typically not run the same machine code. This is true even when the same type of processor is used.

Most instructions have one or more opcode fields. They specify the basic instruction type. Other fields may give the type of the operands, the addressing mode, and so on. There may also be special instructions. They are contained in the opcode itself. These instructions are called immediates.

Processor designs can be different in other ways. Different instructions can have different lengths. Also, they can have the same length. Having all instructions have the same length can simplify the design.

Example

The MIPS architecture has instructions which are 32 bits long. This section has examples of code. The general type of instruction is in the op (operation) field. It is the highest 6 bits. J-type (jump) and I-type (immediate) instructions are fully given by op. R-type (register) instructions include the field funct. It determines the exact operation of the code. The fields used in these types are:

   6      5     5     5     5      6 bits
[  op  |  rs |  rt |  rd |shamt| funct]  R-type
[  op  |  rs |  rt | address/immediate]  I-type
[  op  |        target address        ]  J-type

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

Example: add the registers 1 and 2. Place the result in register 6. It is encoded:

[  op  |  rs |  rt |  rd |shamt| funct]
    0     1     2     6     0     32     decimal
 000000 00001 00010 00110 00000 100000   binary

Load a value into register 8. Take it from the memory cell 68 cells after the location listed in register 3:

[  op  |  rs |  rt | address/immediate]
   35     3     8           68           decimal
 100011 00011 01000 00000 00001 000100   binary

Jump to the address 1024:

[  op  |        target address        ]
    2                 1024               decimal
 000010 00000 00000 00000 10000 000000   binary

Other pages

Notes and References


Advertisements






Got something to say? Make a comment.
Your name
Your email address
Message