Programming in Assembly Language

 

Back in the 80s I wrote many PC assembly routines such as designing a key pad input program, mathematical manipulation, graphical presentation, hardware interface, user interface and so on... Today's computers and development tools are much more sophisticated, available, reliable, cheaper, and more efficient than at that time. This tutorial will only introduce you to the basics of turning ON and OFF any device under the control of the Microcontroller. Later we will explore how to further automate the Microcontroller control over the real world applications.

Software programming needs practice and lots of exposure. Once you master the basics and familiarize your self with programming you start creating your own programs. Here I would like to enumerate various languages that are used in today's embedded systems: Basic, C, and Assembly. There is no such a thing called the best. Each language has pros and cons:

Basic: The easiest to learn and speedup development time but may lack the required high speed and hardware manipulation. Usually it takes more code space in the DSP or Microcontrolers. Caution must be practiced when dealing with hardware and code limitation.

Assembly: Compared to Basic language it is much faster when executing commands. Assembly also can directly talk to hard and various peripherals, such as the serial port devices, printers, display systems, audio equipment, and so on. It is very agile language that put you in total control over any device connected to the computer or embedded system. Unlike the Basic language, Assembly takes less code space than Basic and is more complex.

 

C: It is somewhere between Basic and Assembly. Today's programmers favor C language over Basic and Assembly. Even though they may need to include some assembly code due to the limitations of C. Different embedded manufacturers have their own assembly commands. Once you learn assembly for any manufacturer you can leap frog in any other embedded or PC assembly.

The choice is yours when it comes to which language you should use given a solution to any problem. You should weight advantages and disadvantages when dealing with certain task. I found that assembly is really not hard to learn but it takes more time to provide the needed solution, not forgetting the power and agility it carries with it. If the solution to a given problem doesn't require very high speed, you are very tight in time, and you are new to the world of embedded then the choice would be Basic language. If you are give plenty of time to develop software that deals with very high speed applications and consumes plenty of code space then the choice could be C or Assembly.

Having said so, today's technology offers DSP and Microcontrollers that are highly capable of handling high-speed applications via the built in high-speed hardware and are very generous in code space and are much cheaper. Here are few words about the Assembly language:

  1. Many people find that learning new things is hard and if you are new to assembly then you may find it hard. Once you learn it then you will love it and appreciate its authority.
  2. Troubleshooting and maintenance of assembly is not as easy as troubleshooting and maintenance of other languages.
  3.  Today's technology eliminated the need for assembly language, but allowed inclusion of Assembly code within Basic or C code.
  4. It is important how you design your code. If the application requires high-speed processing then proper and well written C code may suffice it, thus eliminating the need for Assembly.

Let me now introduce you to the Assembly language. Assembly language enables you to control hardware, process data, make decision, handle requests, accepts and provides data at different time intervals. In the case of PC the hardware kernels of the operating system (OS) are responsible for handling the hardware activities when called upon from the software commands. In the world of embedded systems the registers of the DSP or the Microcontroller handle the world around them. Registers are special memory locations inside the chip that perform operations and manipulate the hardware connected to them. These memory locations can be manipulated directly or indirectly to process data and communicate with the world around them. For example a register (memory location) can enable you to turn ON or OFF the light in your house. If you fill this register with the value 1 the light turns ON and if you fill this same register with the value 0 the light turns OFF. Registers are also used to control flow of data such as data can move from inside the Microcontroller to the outside world (OUTPUT) or from outside to inside (INPUT). MCU and DSP can have more than one register such as registers A, B, C, or D. It is as simple as that. Registers are identified by 1) Their address 2) Value to fill them with. This is identical to identifying a person by his address and his name. For example a person that lives in building 3 has the name Joe. His address is building # 3 and his name is Joe. The question is how do I change the value of these different registers? We need to identify the address of that register (building # 3) and fill the address with 1 or 0 (Joe or Mike). Here is a simple Assembly command for the Sxtech Microcontroller that allows you to change the register value:

mov !ra,#%11111110

mov ra, #%00000000

Registers can have up to 8 bits (or word) and each bit is identified by its address within the register. For example register A bits are identified as (from left to right) bit# 7, bit#6, bit#5, bit#4, bit#3, bit#2, bit#1, bit#0 a total of 8 bits. The first assembly line above says I am dealing with Register A (ra). The symbol ! in (!ra) says that I am intending to fill this bit (or pin) with the value 1 or 0. The command mov is saying that I will MOVE (mov) the binary value (11111110) to register A, meaning that bit# 0 will become 0 and bits 1 to 7 will become 1. When the value moving to !ra is 0 the corresponding bit (pin) will become an output pin and when the value moving to !ra is 1 the corresponding bit becomes input pin. The result will be that bit# 0 of register A will become an output and bits 1 to 7 become inputs. The #% in the [mov !ra,#%11111110] means that this is a binary (%) number (#).

Up till now we identified which register we are dealing with (A) and which bit (pin) in this register will be input and which becomes output. The next step is to tell the Microcontroller to produce ON (1) signal or OFF (0) signal from the bits (pins) we assigned as inputs or outputs of that register (A). Simply give them the values you desire, 1 or 0. The second line above says MOVE (mov) the binary value (00000000) to register A (ra). This makes all bits (pins) of register A to go low (0) (OFF).

The following command says make pins 0 to 3 HI (1 or ON) and pins 4 to 7 LOW (0 or OFF):

mov ra, #%00001111

This operation can be used to control your motors, light, dishwasher, security system, and anything you wish to control. Since the Microcontroller operates from a small battery (typically about 1 to 5 volts) then we can't directly connect the bulb to this pin because the bulb and motor operate from much higher voltages (12 VDC, 20 VDC, 110 or 220 VAC, or other). We can use a driver to enable us to control high voltage devices (motor, bulbs, and so on) from the low voltage Microcontroller (1 to 5 volts). See my web site on how to build your own driver to accomplish this task. When you come back make sure that the Microcontroller output (pin under control) is connected to the resistor connecting to the base of the transistor. The transistor is capable of handling much higher voltages and power than the Microcontroller Input or Output pins. Depending on your application you may need to use relays or transistors that have the power requirement of your application.

In the next paper we will explore how to automate the control of Microcontrollers to the world around it. 

Having fun yet?

 

  

to Index

  

By Engineer: Firas Faham

1