Assembly Language Examples

This article gives another step-by-step lesson about assembly language programming.  In order to follow along with this article, you should have this previous article on hand:  An Assembly Language Example

Refer to that program and do the following:

Rewrite step 3 above using 32 and 96 instead of C6 and C7.

This sounds like a good idea at first. Only about half the number of instructions will be needed as we move 2 bytes at a time instead of one. But wait! How do we move an odd number of bytes? We can’t move only the 5th byte using these two instructions. For this exercise it is not fair to revert back to the previous instructions for the 5th byte (although in an actual program that would be a very good idea). Read the code below to find out!

E425: 32 E24E                              load H:X from ni[0-1]
96 0188                                           store H:X into n[0-1]
32 E250
96 018A
32 E251
96 018B

This is expressed more cryptically in a simulator/debugger memory display, but is the same info:

E420: .. .. .. .. .. 32 E2 4E
E428: 96 01 88 32 E2 50 96 01

E430: 8A 32 E2 51 96 01 8B ..

Another Assembly Language Task:

Perform I = 3*J – K + 21, where I, J, and K are variables. We will need some new instructions. They are:

CB hh ll Add value at location hhll into A
C0 hh ll Subtract value at location hhll from A

Step1. Decide where to place I, J, and K in memory. Remember they must be in RAM.

019E: ?? Location for I showing its value is unknown
019F: ?? Location for J
01A0: ?? Location for K

Usually we just place all our variables in consecutive RAM locations.

Step2. Decide where in memory to place the constant 21. It must be in ROM.

E0E0: 15                        Note the value looks different in hex!

Step3. Decide where to place the program and write it.

E10C: C6 019F                    load A with value from J
CB 019F                                add in another J to form 2*J in A
CB 019F                                add in another J for 3*J
C0 01A0                              subtract K from 3*J in A
CB E0E0                              add the constant 21 into A
C7 019E                              store the result into I

Step4. Define the starting address in the reset address in ROM.

FFFE: E10C

You should enter these values in the S08 simulator memory, and run the program a couple of times. REMEMBER to enter test values into the variables J and K before running the program. This section of code assumes that those values get placed there by sections of code not shown. To test this section you must provide some values to test. Verify that the value stored into I by the last instruction is correct for the values of J and K that you provide.

An Assembly Language Example

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.