Programmieren für Fortgeschrittene - Assembler - Teil 1 - AZ-Delivery

You have most likely written the software for your previous microelectronics projects in the Arduino IDE in the C programming language. The commands of the Arduino IDE are relatively intuitive and beginner-friendly.
If you have ever wondered how the microcontroller works internally, you will learn about the structure and the assembly language in this blog series. This is a low-level programming language and therefore much closer to hardware than C++, for example.

The assembly programming language is not only useful for understanding how the processors work.  but also offers the advantage that only minimal memory is required and the individual commands can be processed much faster.

This is why assembly language is still used today in time-critical systems or when only a small amount of memory is available, such as in compilers or embedded systems.

 

Introduction

The assembly language was developed in 1947 by the British Kathleen Booth. In the 1980s, it was largely replaced by high-level programming languages such as C due to more powerful computers.

In assembler it is possible to call direct processor instructions, this mainly includes writing and reading memory registers, program flow control and also logical and mathematical operations.

 

The problem with this programming language, however, is that programming requires in-depth knowledge of memory structure, for example, and the commands can seem complex at first.

ATmega328P structure

 

At the beginning of the address space are the 32 general registers (R0 to R31). These registers are directly accessible and are used for all arithmetic and logical operations and for the data transfer used.

This is immediately followed by the I/O register areawhich contains all specialized control and status registers with which peripheral units (such as ports, timers, UART etc.) are configured and controlled.

 

Then the SRAM area (Static RAM), which serves as data memory. In this memory area temporary variables and the stack are stored.

 

The flash memory is a separate memory area in which, among other things, the program itself is stored. The ATmega328P has 32 kilobytes of flash memory.

 

In addition, the ATmega328P also has 1 kilobyte of EEPROMa non-volatile memory that is addressed separately.

 

Figure 1: Block diagram of the ATmega328P structure

The registers R0 to R31, which are used for arithmetic operations in the examples, are located in the "AVR CPU" in the diagram.

Number systems

We use numbers such as 180 or 2380 in everyday life. These are so-called decimal numbers, which means that we use powers of the number 10.

Figure 2: Decimal system

 

One of the reasons for this is that we have 10 fingers.

 

Binary system

As the memory and all processes in the computer are binary, the common decimal system is not suitable for this, as only two states (voltage or no voltage) are possible in the memory. The binary system was developed for this purpose. This works with multiples of the number 2.

For example:

Figure 3: Binary system

 

The program notation of binary numbers is as follows:
0b10110100

 

Hexadecimal system

In order to understand more complex operations, it is important to know the different number systems. In addition to the binary and decimal system, there is also the hexadecimal and octal system.

As the name suggests, the octal system works with powers of 8 and the hexadecimal system with powers of 16.

The most relevant is the hexadecimal system, in which letters are used for all two-digit numbers. This means A=10; B=11; ... ; F=15

Figure 4: Hexadecimal system

 

The program notation for hexadecimal numbers is as follows:

0xB4

 

Nevertheless, in everyday life, tools such as a scientific calculator or a conversion website offer the advantage of faster conversion from the decimal system we know to the other systems.

 

16bit

As the ATmega 328P is an 8 bit processor, a memory register only has a length of 8 bits, which corresponds to a number range of 0-255.
This number range is not always sufficient. If, for example, you want to save a pointer to a memory address, this would already lead to problems, as the addresses correspond to a length of 16 bits.

 

In order to be able to save these nevertheless, it is possible to save the upper or lower 8 bits in two separate registers.

This means that a number

0000111111110000

can be divided between the registers as follows:

00001111 (high)

and

11110000 (low)

Programs

Software Tools Download:

Two software tools are required. Firstly, an assembler to convert the code into a compiled file and a programmer to transfer the compiled hex file to the board.

 

Assembler

The following software is recommended as an assembler avra which must be compiled manually on your computer.


Linux:

Under Linux, installation is simple, as most operating systems, such as Ubuntu; Fedora; Manjaro, use the make command.

First create an image of the files in the GitHub repository on your computer. You can use the Git-Clone command for this.

cd Downloads

git clone https://github.com/Ro5bert/avra.git

Alternatively, you can also download the files directly from the Github repository directly.

 

Once the files are on your computer, switch to the folder via the terminal as follows:

cd Downloads/avra

 

Then run the command

make

 

command.

Finally, the compiled file must be installed. To do this, simply use the

sudo make install

 

command can be executed.

 

The compiler software is now successfully installed on your computer. The further procedure can be found in the section Application

Windows:

Of course you can also install the program under Windows, but as with Linux, the program must first be compiled. For this, the MS Visual Studio 2019 IDE is recommended, which is available free of charge in the community version. available free of charge.

A detailed description of the steps can be found in a instructions available on the GitHub repository.

 

AVRDUDE

The AVRDUDE software is suitable for transferring the compiled files to the microcontroller.
This is normally already installed with the Arduino IDE, as this also uses the software for data transfer.

 

If you still want to install the software yourself, this is of course also possible.

Linux

Execute the following command in the terminal:
sudo apt-get install avrdude

 

Windows

In the GitHub repository you can download the latest software for your processor.

Application

After successful installation, you can create a file with the file extension .asm using a text editor or code editor. To compile, execute the following command in the terminal in the directory of the file:
avra programname.asm

 

Now a programname.hex file has been created. This binary file can now be loaded onto the board.

This can be done with the following command:

avrdude -c arduino -p m328p -P -b 115200 -U flash:w: "Path/File.hex":a

 

Adjust the serial port and the file path accordingly.
You can determine the serial port either in Windows in the device manager and under Linux with dmesg | grep tty or alternatively in the Arduino IDE.

 

Alternatively, you can also use the graphical user interface AVRDUDESS graphical user interface. This uses the same command as the command line version, but the configuration and selection of parameters is much more intuitive and therefore more convenient.

Figure 5: Graphical user interface AVRDUDESS

Conclusion

Now that the initial basics have been established in this blog post, the next part will focus on the application in a short example project.

 

Have fun recreating it :)

 

References of the blog series:

https://de.wikipedia.org/wiki/Assemblersprache

https://www.instructables.com/Command-Line-AVR-Tutorials/

Projekte für fortgeschrittene

2 commentaires

Leopold Gann

Leopold Gann

Bezugnehmend auf ihr Umrechnungsbeispiel binär/dezimal – 10110100=180 ist zwar richtig, aber die 2er-Potenzen sind jedoch verkehrt rum angegeben …

Roman

Roman

Vielen Dank für diesen anschaulichen Einstieg in die Assembler‑Programmierung mit dem ATmega328P.

Ich freue mich sehr auf die nächsten Teile des Kurses!

Eine kleine Rückmeldung zum Blockdiagramm:
Beim ersten Lesen war ich irritiert, weil ich die 32 allgemeinen Register (R0–R31) und den I/O‑Registerbereich nicht direkt im Bild verortet habe. Mir wurde erst später klar, dass beide Registerarten fest im AVR‑CPU‑Block stecken und deshalb nicht als eigene Kästchen auftauchen. Vielleicht hilft künftigen Leserinnen und Lesern ein kurzer Hinweis im Text (oder eine kleine Markierung im Diagramm), dass das Register‑File und der I/O‑Registerraum „im Inneren“ der CPU bzw. entlang des Datenbusses liegen.

Ansonsten gefällt mir der Kurs bislang sehr gut – weiter so!

Laisser un commentaire

Tous les commentaires sont modérés avant d'être publiés

Articles de blog recommandés

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery