Assembly Language Example
Okay, so this post is just an example of an elementary assembly language program. It will be useful to you if you haven’t learned how to program in assembly before, or simply want to learn some syntax. This article specifically talks about the HCS08 microcontroller, so if you want to compile this code, you should have one of those handy. Also, make sure you have the databook on hand.
We are now ready to write our first machine language program. This program is to initialize a 5-byte variable (an array that I will think of as being named n) by copying a 5-byte constant (an array I will think of as being named ni) into it. There are several steps that must be taken – it is very important to understand what each step does and how it fits into the programming picture.
Step 1. Decide where in memory the array variable (n) will be located. It must be in RAM so all five must be between addresses 0060-025F. All five must be in succession for it to be an array. Lets pick 0188-018C. If asked for the address of n, the correct answer would be 0188 (the lowest number in the set of addresses). There is nothing more to do for any variable other than to decide on its address.
Step 2. Decide where in memory the array constant (ni) will be located and what values it has. Constants must be in ROM so all five must be between addresses E000-FFFF (actually about FFAE is upper limit). I will select location E24E-E252. Many constants have self-defining values. Suppose we needed the constant five. Its value would be 5. The values I am about to choose for ni are not self-defining. I will select the ASCII (ASCII is a character code for alphanumeric information) value of the letters A to E. These decisions can be expressed as follows:
E24E: 41 42 43 44 45
Step 3. Decide where in memory to place the program, and write it. It also must be in ROM. I choose E425 to start. We will see where it ends. Here it is:
E425: C6 E24E load A from ni[0]
C7 0188 store A into n[0]
C6 E24F
C7 0189
C6 E250
C7 018A
C6 E251
C7 018B
C6 E252
C7 018C store A into n[4] – last byte of n
Question: What is the address of the last byte of this program segment?
Step 4. Tell the HCS08 to start at location E425 when power is first applied to it. We do this by defining a pointer constant with that value at location FFFE. We will discuss this reset location later in another article. For now all you need to know is that at power-on the value at location FFFE is loaded into the PC (because the PC is 16 bits, the value at FFFF contains part of the starting address as well.
FFFE: E425
Our first machine language program is finished. You can bring up the S08 simulator, type all of the values specified for the ROM into it – 5 bytes of data, 30 bytes of program, 2 bytes of reset address – press the reset icon and then single step repeatedly to watch it run. The above steps must be performed for every machine-language and every assembly-language program that you write with the HCS08.