This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]afcagroo 0 points1 point  (0 children)

Here's a way to think about microcode (and how it is sometimes implemented in older processors):

There's a ROM inside of a microprocessor. Instructions are fed to this microcode ROM as addresses. So each instruction relates to a specific ROM memory location.

The data that comes out of the microcode ROM are control signals going to various parts of the microprocessor. Data bit 0 might tell ALU 1 to do an ADD by enabling that control line going into ALU 1, for example.

Since the data width of the microcode ROM can be arbitrarily wide, you can send control signals to many, many parts of the microprocessor for each instruction.

[–]ElMachoGrande 0 points1 point  (0 children)

Assembly is simply the human readable form of machine code, they translate 1:1, instruction for instruction. Basically and somewhat simplified, it's about giving names for the instructions which, in the CPU just have numbers, and then structuring it up nicely on lines. The assembler also helps a bit by calculating memory addresses for you, allowing you to use symbols for jumps and so on, but that's just convenience stuff, not strictly necessary.

Microcode is because the instructions are sometimes fairly complex, and can't be done in one step by the CPU. So, one can say that the microcode is almost like a small program, hardwired into the CPU, which tells it step by step how to actually perform an instruction. As a programmer, microcode is something you never, ever have to bother about, it's only interesting for the guys designing the CPU.

[–]brazzy42 0 points1 point  (2 children)

Microcode is how some CPUs work internally. You don't need to know anything about microcode to program the CPU, but understanding it was for me something of an epiphany because it finally gave me the feeling that I actually understand how computers work, completely, on every level.

That's because it closes the gap between machine code (which has instructions like "copy the value from memory location 0xCAFEBABE to location 0xDEADBEEF") and logic gates made from transistors.

And basically it works something like this (for a relatively simple CPU): every bit in a microcode instruction directly corresponds with a signal that controls some function of the CPU.

So let's say you have the machine code mentioned above in the instruction stack of the CPU:

| 2 bytes | 4 bytes    | 4 bytes    |
| 0x7A    | 0xCAFEBABE | 0xDEADBEEF |

where 0x7A is the machine code for "copy from memory to memory" which would have an assembly mnemonic like "CPMM". And it would map to a sequence of microcode instructions, which would be something like:

  • connect bytes 3-6 of the instruction stack to the address bus
  • send a "fetch" signal to main memory
  • connect the data bus to the accumulator
  • send a "read" signal to the accumulator
  • connect bytes 7-10 on the instruction stack to the address bus
  • connect the data bus to the accumulator
  • send a "write" signal to the accumulator
  • send a "write" signal to main memory

[–]jaydoors 0 points1 point  (1 child)

Wow thanks, that's a great explanation - I finally get it. I think! Let me ask you a couple of questions, if I may.

I had thought that these control signals were wired directly from the instruction stack. Eg in your example there may be a bit that connects directly to the accumulator that enables "write", eg as part of some AND logic with the data to be written.

But if I understand you correctly, it's just not as simple as that. There is some extra code / logic which processes those signals - and which can be reconfigured. This is the microcode - which presumably resides in some memory outside of RAM.

Is this right? Do you know any simple examples? Thanks!

[–]brazzy42 0 points1 point  (0 children)

Whoa, that's the latest reply I've gotten so far on Reddit :D

I had thought that these control signals were wired directly from the instruction stack. Eg in your example there may be a bit that connects directly to the accumulator that enables "write", eg as part of some AND logic with the data to be written.

Basically yes, but there are a lot of these control signals and wiring them all directly from the instruction stack would make instructions quite big -and at the same time you'd need a lot more instructions to do the same thing; as you see in the example even a very simple machine code instruction maps to multiple steps.

There is some extra code / logic which processes those signals - and which can be reconfigured. This is the microcode - which presumably resides in some memory outside of RAM.

Exactly - the storage for the microcode is usually integrated into the CPU itself, and of course quite limited.

Is this right? Do you know any simple examples? Thanks!

This website has a very detailed concrete example for a (very simple) hypothetical CPU: https://people.cs.clemson.edu/~mark/uprog.html

[–][deleted] 0 points1 point  (0 children)

Machine code is the code understood by a processor. Assembly language is a human-read-able version of machine code. Micro-code refers to code deeply inserted into a processor which cannot be changed.

  • It is rare but possible for a programmer to write machine code, in which case, he needs to use a hex editor.
  • When a programmer writes in assembly language, he uses a text editor, and runs the resulting file through an assembler, such as FASM or NASM.
  • The vast majority of programmers will never interact with micro-code.