What is an API

what is an API

HR API acronyms you will have seen them more than once and twice when you read articles about software. But not everyone knows what an API really is. That is why we clarify it in this article. The first thing to say is that they are the acronym for Application Programming Interface, that is, in Spanish, Application Programming Interface. And many times it generates confusion even among those who know some programming.

For example, Arduino has its own programming API, offering a number of functions that you can use in the Arduino IDE or other environments to program the microcontroller to allow you to create your projects. But ... could you tell me the difference between a programming library and an API? Are there differences between a framework and an API? Is an API the same as an ABI? There are many doubts that perhaps continue to generate confusion and that we are going to make clear right now.

I don't know if you know that low-level languages, like the assembler or ASM, depend directly on the hardware architecture, while the high-level ones abstract from the hardware to facilitate the programmer's task, but depend on the operating system (syscalls) or on certain APIs, etc. So that all this does not sound like Chinese to you, let's see what it is about ...

What is an API?

A API is a tool with which developers are provided so that they have at their disposal a series of functions, subroutines and procedures or methods for OOP that can be used through the existing libraries. Among what an API offers are from functions to create simple apps, functions related to AI, with the generation of graphics, management of hardware resources, etc.

For example, I'm sure well-known APIs sound like the one offered by Linux through the glibc library, or graphics like OpenGL and Vulkan, or also others like OpenCL for heterogeneous computing, OpenXR for virtual and augmented reality, etc. Other operating systems and software also include their own APIs so that other programmers can create addons, plugins or modules for that system, etc.

Example with Arduino

If you have a badge Arduino and you frequently use Arduino IDE, or any other development environment for Arduino, you will know that when you create a code you make use of multiple options to order your microcontroller to do a series of actions. For example, pinMode () is a typical function to configure an Arduino pin, right?

When you write pinMode (9, INPUT)For example, you are indicating that pin 9 of the Arduino board should work as an input, that is, the microcontroller will be waiting for the information to enter through that pin to read it and perform an action. But have you ever wondered how he is able to understand that command?

Well, Arduino has a Development API that is made available to us. Third-party libraries can be added to this API as we have seen in many examples on this blog. Like SparkFun's for certain sensors, etc. With all this, the functions can be entered in the Arduino IDE and it will translate the code properly to load it into the memory of the microcontroller so that it can process it.

If you did not have this API, you could not create these programs for Arduino in such a simple way, since we should try to make them in assembly code for the ATMega328P microcontroller, that is, ASM for AVR architecture. And that would be much more complicated, since you would have to directly use the instructions available by this ISA. If so, you would not only have to adequately know the repertoire of that ISA, but also other aspects such as the number of registrations, etc. That is, you should have low-level knowledge of the hardware you are working for.

By example, ASM code What you should generate for an LED to blink in a loop would be:

<br data-mce-bogus="1">

.ORG 0x0000
; the next instruction has to be written to
; address 0x0000
rjmp START
; the reset vector: jump to "main"
START:
ldi r16, low(RAMEND) ; set up the stack
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16
ldi r16, 0xFF
; load register 16 with 0xFF (all bits 1)
out DDRB, r16
; write the value in r16 (0xFF) to Data
; Direction Register B
LOOP:
sbi PortB, 5
; switch off the LED
rcall delay_05
cbi PortB, 5
; wait for half a second
; switch it on
rcall delay_05 ; wait for half a secon
rjmp LOOP ; jump to loop
DELAY_05:
; the subroutine:
ldi r16, 31
; load r16 with 31
OUTER_LOOP:
; outer loop label
ldi r24, low(1021) ; load registers r24:r25 with 1021, our new
; init value
ldi r25, high(1021) ; the loop label
DELAY_LOOP:
; "add immediate to word": r24:r25 are
; incremented
adiw r24, 1
; if no overflow ("branch if not equal"), go
; back to "delay_loop"
brne DELAY_LOOP
dec r16
; decrement r16
brne OUTER_LOOP
ret
; and loop if outer loop not finished
; return from subroutine

While thanks to the API the facilities are total, writing at a high level the following equivalent code (much shorter and more intuitive):

<br data-mce-bogus="1">

int ledPin = 13; 		// LED que se encuentra en el pin 13
   void setup(){ 
   pinMode(ledPin, OUTPUT);	// El p1n 13 será una salida digital 
} 
void loop(){ 
   digitalWrite(ledPin, HIGH);	// Enciende el LED
   delay(1000); 				// Pausa de 1 segundo 
   digitalWrite(ledPin, LOW); 	// Apaga el LED 
   delay(1000);				// Pausa de 1 segundo 

Differences with an ABI

API vs Linux ABI

ABI is a lesser known term, it is a Application Binary Interface, or in English Application Binary Interface. In this case, it is an interface between modules of a program, generally between a library or a machine language operating system for the architecture in which you are: SPARC, AMD64, ARM, PPC, RISC-V, etc.

Thanks to the ABI, the way to call the functions is determined, binary format which can understand the machine you are compiling for or the system calls, how exceptions are handled, how data is passed, etc.

Differences with a Framework

Un framework or framework is more of a set of tools at your disposal to help the development of a given project. The famework usually sets some coding standards, provides useful components, etc. For example, JUnit is a framework for Java, or Symfony / Cake for PHP, etc.

Differences with an SDK and NDK

SDK is a Software Development Kit, that is, a software development kit. It goes beyond what is a framework or an API. An example could be Android Studio or iOS xCode, etc. For example, the first one, in addition to the Android API itself, also includes an IDE or integrated development environment for programming and compiling, libraries, tools, etc.

Furthermore, NDK (Native Development Kit) it is a complement. For example, Android NDK allows developers to reuse C / C ++ code by introducing it to applications through JNI (Java Native Interface) ...

Differences with a library

Glibc diagram Wikipedia

Finally, the library is a collection of reusable source code that makes life easier for programmers. For example, inside the C library stdio.h there is a printf function to print text on the screen. For this to be possible, a source code is needed that makes the operating system do this task. But since it is something very recurrent that is used frequently, simply by invoking that library you can make use of printf without writing all the code from scratch. In other words, in other words, they can be seen as precast blocks.

A library and an API can be easily confused, in fact, libraries are wrapped inside an API. Example glibc...

I hope that after this you have an idea clearer about what an API, ABI, a framework, an SDK and a library are, in addition to being able to differentiate between them from now on.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.