\(4.2.\)The Elements of Machine Language

1.Machine language

  • The machine language is a most important interface between hardware and software. It's exactly the way software can control hardware.

    • What are the supported operations?
    • What do they operate on?
    • How is the program controlled?
  • This kind of interface is done in a almost one-to-one correspondence with the actual hardware implementation. The idea is that the hardware is built in a way that directly corresponds to the type of functionality that is provided by the software layer above it.

  • When we actually go to design a machine language, the basic element is a cost-performance tradeoff. The more sophisticated operations that we want to give our machine language, the more large or sophisticated data types it operates on, the more costly it will be to actually build this.

  • Each machine language defines a set of operations which fall into several categories:

    • Arithmetic Operations: add, subtract, ...
    • Logical Operations: And, Or, ...
    • Flow Control: goto instructionX, if C then goto instruction Y

2.Memory

  • accessing memory is an expensive operation in at least two related points of view :

    1. If you have a large memory, specifying what part of memory you want to work with requires a large amount of bits.
    2. It's going to be wasteful in terms of instruction, for accessing a value from a large memory takes relatively long time.

  To solve this, we introduce memory hierarchy*:

  • The basic idea is instead of having just one large block of memory, we're going to have a whole sequence of memories that are getting bigger and bigger.

    • The smallest memories are going to be very easy to access. Because:

      1. We don't have to specify large address space because there are only going to be a very few of them.
      2. The amount of them is small, so we can get information from them very quickly.
    • Then, there is going to be slight larger memories called cache.

    • And even big memory, called main memory.

    • And maybe even even larger memory called disk.

  • At each time we get farther away from the arithmetic unit itself, our memory gets bigger, accessing it becomes harder both in terms of giving a larger, wider address, and in terms of the time we need to wait until we get the value. But we have more information there.

3.Registers

  • Almost every CPU has a few easy-accessed registers that are located inside the CPU. Since there are so few of them and everything requires very few bits, getting the information of them is extremely quickly.

  • There are two kinds of register:

    1. Data Registers: We can put numbers, etc, in the register. And when we are saying something like add the register 1 to register 2, what will happen is the contents of register 1 will be added to the contents of register 2.

    2. Address Register: We can put into the register an address into main memory. It allow us to specify at which part of the bigger memory we want to access for operations.

      • For example, say we have a operation like storing the contents of register 1 into memory address that is specified by that register called A. Once we actually perform this operation in the hardware, the number 77 will be written into the main memory.

4.Addressing modes

  • Register: \(e.g.\)Add R1 R2 means adding R2 to R1.
  • Direct: We can have direct access to memory.\(e.g.\) Add R1 M[200] means adding R1 to memory location 200.
  • Indirect: This is a example we had previously for using the A register where the @A memory address that we access is not specified as part of instruction, but rather is already written inside the address register that was previously loaded inside the CPU with some correct value.\(e.g.\) Add R1 @A
  • Immediate: \(e.g.\) Add R1 73

5.Dealing with input and output

  • One way to actually access these input or output devices(keyboard, for example) is to actually connect the registers which control these output devices as part of your memory.

    • For example, we may have a mouse that is connected in a way that whenever the user moves the mouse, the last movement is written into some kind of a register. And that register is accessible by the computer in a certain address as part of the memory. This gives us access to input and output as though we are accessing the memory itself.
  • Software are usually part of the drivers in an operating system. It must know exactly not only what are the addresses to which this input or output device is connected,but also how to speak with it.

6.Flow control

  • Sometimes we need to "jump" unconditionally to another location, for example:

  • Sometimes we need to jump only if some condition is met, for example: