10
\$\begingroup\$

So I often see books/tutorials and references when programming in assembly to a microprocessor.....then I see some refer to it as a microcontroller.

For instance the Atmel ATtiny2313....i saw some tutorials on, some call it a microprocessor, some call it a microcontroller?

Which is it? and is programming them (basically) the same? (in assembly)

\$\endgroup\$
3
  • 5
    \$\begingroup\$ possible duplicate of What's the difference between a microcontroller and a microprocessor? \$\endgroup\$
    – Kellenjb
    Mar 10, 2011 at 18:33
  • 1
    \$\begingroup\$ @Kellenjb: It would be if it wasn't asking about the programming differences. I think this is different enough not to close. \$\endgroup\$
    – BG100
    Mar 10, 2011 at 18:49
  • 1
    \$\begingroup\$ @BG100 the accepted answer only really explained the difference, not a programming difference. \$\endgroup\$
    – Kortuk
    Mar 13, 2011 at 6:53

6 Answers 6

16
\$\begingroup\$

This is really two questions in one...

Firstly, what is the difference between a microcontroller and a microprocessor?

Microprocessor is a purely a CPU that follows a set of instructions read from an external memory bus. It controls external peripherals (such as screen, keyboard, mouse, hard drive, etc) via an external communications bus. When you program a microprocessor, your program is external to the device. In a computer, this memory is initially the boot up BIOS ROM which initially reads the operating system from the hard drive into RAM memory, then continues to execute it from there.

Microcontroller is kinda like an all-in-one CPU + Memory, with some external ports to communicate with the outside world. It's self contained and doesn't use external memory to hold it's program (although if needed it can read and write working data to external memory).

Secondly, is programming a microcontroller and microprocessor the same?

In some ways yes, and in some ways no.

Assembly language is a broad term that describes a set of instructions that the CPU can directly understand. When you 'compile' assembly language, it doesn't really compile anything, all it does it convert it to a sequence of bytes that represent the commands and plugs in some relative memory locations. This is common to both microprocessors and microcontrollers.

However, different types of CPU understand a different set of CPU instructions. For example, if you write an assembly language program that works with a pic 16F877 microcontroller, it will be complete nonsense to a microprocessor or any other microcontroller outside of the 16Fxxx family of pic microcontrollers.

So, although assembly works in a similar way across all microprocessors and microcontrollers, the actual list of instructions that you write are very different. To write in assembly language, you need to have an in depth knowledge of the device's architecture, which you can normally get from the datasheet in the case of a microcontroller.

\$\endgroup\$
5
  • \$\begingroup\$ Well what I meant was yea ASM will be different for each one.....but are the commands/etc generally the same (or done the same way) between a MC and a MP...I mean a MC has an MP so i would assume so.. (minus the memory) \$\endgroup\$
    – user3073
    Mar 10, 2011 at 18:53
  • \$\begingroup\$ @Sauron: No, not really.. while some commands may be similar between devices, like add, mov, sub, etc, they are probably implemented differently, and will not port between devices. \$\endgroup\$
    – BG100
    Mar 10, 2011 at 18:57
  • 1
    \$\begingroup\$ Excellent reply, and one that probably could have helped me when I was starting my microprocessors class. \$\endgroup\$
    – pfyon
    Mar 10, 2011 at 19:19
  • \$\begingroup\$ well what I meant was......Like since a Microcontroller has a CPU inside of it...the Assembly instructions are more aimed at the actual CPU rather than the components around it. \$\endgroup\$
    – user3073
    Mar 10, 2011 at 19:42
  • \$\begingroup\$ @Sauron: Yes, that's correct. \$\endgroup\$
    – BG100
    Mar 10, 2011 at 20:03
10
\$\begingroup\$

The difference is that the microcontroller includes on-chip memory like Flash EEPROM and RAM, and peripherals like parallel and serial I/O. With the first microprocessors those were all external devices. Instead of the I/O's microprocessors had an the address and data bus brought to their pins.
The way you write code for either is the same.

To illustrate that point: there are architectures (ARM for instance) where the very same CPU is availble as microcontroller (with all code and data memory on the chip), as microprocessor (all code and data memory external), or as hybrid (some memory on the chip, but for most applications you will add external memory). The CPU is the same, so the programming (in the sense of CPU instructions) is the same.

\$\endgroup\$
3
  • \$\begingroup\$ Oh ok that makes more sense, but the ASM for each one is basically the same? \$\endgroup\$
    – user3073
    Mar 10, 2011 at 18:39
  • \$\begingroup\$ You mean if I learn to code for Cortex M series, i can then code for Cortex A series too?? \$\endgroup\$
    – 0xakhil
    Jun 29, 2011 at 16:43
  • \$\begingroup\$ Basically yes to both questions, the asm instructions are the same (although there might be minor variations, just like various ARM versions can add specific instructions). But whenever you are using things outside the CPU (cache, interrupt controller, peripherals, etc) there will be big differences. \$\endgroup\$ Aug 31, 2011 at 16:09
8
\$\begingroup\$

Although this tends to be a grey area, another common difference between microcontrollers and microprocessors is that microcontrollers often use Harvard architecture (separate address space for code and data), while micrprocessors almost all use Von Neumann architecture (combined address space for code and data).

Examples of microntroller families using Harvard architecture include: AVRs, Intel 8051, PICs (except PIC32, see below), and ARM Cortex-M3. A notable exception are Freescale processors, like the HCS08, which use Von Neumann architecture, as does the Parallax Propeller.

This affects programming in several ways (examples shown below use C):

There may be several types of RAM, each with their own address space. For example, the 8051 has external data (xdata) which is addressed separate from the first 256 bytes of RAM, even though both are implemented on the same chip. So one has to use qualifiers on variables declarations like unsigned int xdata foo;

If constants are declared in code memory, they may need to be copied to RAM before they can be accessed. Or, there needs to be a way to access code memory as if it were data -- e.g. the code qualifier for 8051s, or PIC's Program Space Visiblity (PSV) feature.

These non-standard ways of accessing code and RAM tend to be the major difference (besides peripherals) when porting C code from one chip family to another.

You can't execute code from RAM in a strict Harvard architecture, so there can't be any self-modifying code (unless you count re-flashing program memory on the fly). However the PIC32 has a modified Harvard architecture that allows code to be executed in RAM. The Parallax Propeller actually makes use of its ability to modify code to perform subroutine returns, since it has no hardware stack.

\$\endgroup\$
1
\$\begingroup\$

A microcontroller is generally a single-chip solution to provide computation and peripheral functions.

A microprocessor provides the computation functions but not the peripheral functions.

Peripheral functions can be as simple as having a few bits of simple I/O; or might include sophisticated counters and timers, video display, ethernet, motor control, audio and video codec, et cetra.

For a given architecture (say the x86-based CPU's and MCU's), the "computational" coding will be identical. But how you access peripheral functions will vary, and so you will have very different hardware-specific coding to do, based on how the peripheral functionality is implemented on your target hardware.

\$\endgroup\$
1
\$\begingroup\$

Microprocessors are typically used in computers which are constructed to run programs of arbitrary yet-to-be-determined purpose. Such computers will generally have some vendor-supplied code in them with which the user-supplied code will be expected to interact. Microcontrollers, by contrast, are typically used in machines which are built for the sole purpose of running a single program. Often, someone who writes code for a microcontroller will supply every single instruction that machine will execute.

Some microcontrollers use the same instruction sets as popular microprocessors. The 68000 instruction set used in the original Macintosh, Amiga, and Atari ST lines of personal computers has also been used in some microcontrollers. Even though the instruction set used by a Macintosh and a 68HC340-based control board is the same, however, programming for the two platforms is apt to be very different. On the Macintosh, by the time a user-supplied program starts running, much of the system will already be "set up". Code which wants a block of memory can load a registers with the amount needed and execute an "A-trap" instruction. The Macintosh OS will then return a pointer to some memory that has not previously been allocated for some other purpose, and mark that area of memory so it won't be allocated again until instructed that the original recipient no longer needs it. By contrast, on a board with a 68HC340 and 128K of RAM, there's no need or ability to "ask" for RAM. When the program starts, will "gets" 128K it can use however it wants; nothing else will use it, but on the flip side the user's program has to keep track of what areas it's using for what purposes since nothing else will track that.

While the distinction here is really between a microcomputer versus microcontroller, and the question is about microprocessors, most discussions of microprocessor programming discuss it in the context of a general-purpose computer.

\$\endgroup\$
0
\$\begingroup\$

Microprocessor: A digital hardware module that executes instructions. The module can be a complete integrated circuit.

Microcontroller: A complete module that contains a microprocessor with internal memory in addition to other modules.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to EE.SE, Mike. Use <enter> x 2 for paragraph break. I've fixed it for you. \$\endgroup\$
    – Transistor
    Oct 9, 2017 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy