Using an Assembler and Simulator

I would suggest that you create a directory somewhere on your hard drive called COURSE and put the following files in this directory. They are all zipped and you will need PKUNZIP from PKWARE or a similar unzipper to extract the files. All these programs are DOS based and should be ran under DOS. If you are using WIN3.1 or WIN95 you'll need to get to DOS. If you're a power user you could setup icons to run these programs, but my explanations and examples will assume that you are at the DOS command line C:\COURSE prompt.

I've spent several hours searching the net to find all these files. All are either shareware or freeware and there should be no ethical problems using them for this course. You should, however, purchase all of them in the event you continue using them after the course. I can't give you license to use them, but for educational purposes, I don't think that there will be any problems. Most of these programs have manuals that explain how they work. Print these out for future reference. I've lost the docs for QEDIT and BR but if you've ever used a text editor or browser, you shouldn't have any problem using them.

The assembler we are going to use is made by Pseudo Corp and can be downloaded here. The simulator is made by HiTech Corp and is here. The examples from lesson 6 are here. The text editor QEDIT , by SemWare, can be downloaded here. A text browser can be downloaded here. Any text editor can be used to create the assembler source files. You could even use notepad in Windows. I am giving you the ones in lesson 6 already typed up. Also, a handy 8051 reference can be downloaded here. It is a TSR that can be loaded once before starting the assembler or simulator, and then invoked by pressing a couple of keys together. It has some good info and might be useful in the process.

For those of you who just can't wait, here is a rough draft of the system we will use later on in this course for training purposes. It's isn't finished (all the parts are not marked) but all the pin numbers and the general connections are complete, and could be built, but don't. I'll give you the real thing later on. There is a text file describing how to print it out, read it before trying to print it!

The first step in writing a program is to define the problem as completely as possible. You will always think of things as you go along that you left out. After thinking out the problem, you start by typing up a source file. I have already typed up all the sample files in lesson 6 and assembled them. The source files all end with the .asm extension. The extensions created by the assembler are .obj which is the object file and .lst which is the listing file. The listing file shows all the addresses resolved by the assembler, all the code generated and all the comments. This file is very important in using the simulator. In the simulator, there are no labels shown because the simulator really dis-assembles the object file as it is loaded into the simulator. No names are listed in the simulator so the listing file is used to keep up with where the simulator really is. You'll see all this as we go along. The listing file also shows any errors in the assembly process so that you can correct them. It is crucial that you look at the listing file to be sure that there are no errors listed.

One note, the assembler creates object files with the extension .obj but the simulator looks for object files with the extension .hex. So you have to rename them after assembly before loading them into the simulator. Sorry about that, but that's the way it works. I have already renamed the problem object files for you, but any you create will have to be renamed each time you assemble a file before loading them into the simulator.

The simulator is a way to test your programs without downloading them to the DS5000. This means, for our purposes, you don't have to have a DS5000 to learn to use a DS5000. The way you would use it in real life is to write each program, assemble it, and then load it into the simulator. After all the different pieces of the whole program have been tested, you would combine them into one file, possibly test it again, and then download it to the DS5000. This particular simulator isn't fancy, and there are better ones, especially the one from Pseudo Corp, the one I use when I'm making something ($199). But this one is free and will do for our purposes. The only thing that it can't do is simulate an interrupt, but this can be worked around and I'll show you how later.

We are going to look at some of the examples from the last lesson and watch them in action in the simulator. We're not going to look at all of them, but you can look at the others on your own. The first we will look at is PROB01.ASM. I am assuming that you followed my directions and created a directory to hold all the files listed on this page. These should include the assembler program, the editor program, the simulator program, and all the example files from lesson 6. Also you are in that directory at the DOS prompt.

The first thing to do is start the simulator. Do this by typing SIM51EM and press ENTER. You are now looking at the simulator screen with all the various sections of the 8051. The screen is divided into six sections. Each section is separated by dashed lines. The top section is the Registers section.On the left side starting on the 8th line are the names Addr, Opcode and Instructions. This is the Code section. On the Right starting on line 4 is the Internal RAM section. Below that is the External RAM section. Below that is the Program memory section. Below that is the Function Key assignments and what each does. To the left of that is two boxes, one that says File and the other says Select. Below that is the command line with the flashing cursor.

Of these sections, we will be concerned with the Register, Code, and Internal RAM sections, the function keys, and the command line. I will describe each as this discourse continues. Notice that in the function key section, the F7 File IO is highlighted. Also on the far left of that, a box that says FILE is shown. Inside that box are Name file, Read file, and Write file. These are the commands that can be entered on the command line. Only the capital letter of each is used. So to name a file you press N, to read a file you press R and to write a file you press W.

First we must name the file we want to load into the simulator. Press N and enter PROB01.HEX and press enter. Now PROB01.HEX should appear on the line. Press enter to accept it. Now press R(READ) and Y to accept PROB01.HEX. PROB01.HEX is now loaded into the simulator.

Looking at the Code section we see under Addr a column of 4 digit numbers. On the first line we see a 0000 followed by 7802, MOV R0,#02. The 0000 says that this is location 0000 in Program memory ( the first location). The 7802 is the actual two byte Opcode that is assembled from the MOV R0,#02 instruction. The 78 says that this is a MOV immediate into R0 and the 02 is the value moved into R0. Also notice that this line is highlighted. This is the instruction that will be executed next. Notice above the Code section is R0 and the value below it is 00. This is what is currently in R0. Also notice that at the top of the screen in the Register section is ACC and below that a 00. This is what is currently in the accumulator. And lastly notice the PC and the 0000 below it. This is the current value in the Program Counter.

The next thing we must do is to start executing the program. To do this first press F5. Notice the left two boxes have now changed to Breakpoint and Misc. These are the commands that can now be entered on the command line. Notice the Single step in the right box. To single step the program one instruction, press S. Do that now, but only once. Notice that now in the Code window the highlight is on the next line. Also notice that R0 now has a 2 in it. We have just executed our first instruction, to put a 2 in R0. Notice that the ACC is still 0. Also notice that the PC is now 0002. Remember that the PC always points to the instruction that will be executed next. Now press S again. Now the ACC has a 2 in it as does R0. Also the PC is now 0004. We have just executed the second instruction, to move a 2 into the accumulator. We are now ready to add 2 plus 2.Now press S again. Now the accumulator has a 4 in it. This is the result of adding 2 plus 2. This also finishes this program.

Notice that in the Code window below the three instructions that were in this program that the rest of the lines have NOP in them for the Instruction. Also notice that the Opcode is 00. A NOP is an instruction that does nothing and happens to have an Opcode value of 00. These are here because all the rest of Program memory has 0's in them, which we didn't load with anything, but are interpreted as NOP's. This is normally what you will see following the last instruction in a program.

In review, the Opcode is the actual value that the assembler assembles for an instruction and is what the micro understands and can execute. The Instruction is the english form of the Opcode and is for the benefit of the programmer.

To exit the simulator, first press F1 and then E and Y. This completes the first session with the simulator. You can simulate this program again by repeating the previous steps.

Next we will simulate PROB04.ASM. Start the simulator, name PROB04.HEX, and read it. Also you will need the list file for this program (PROB04.LST) printed out for reference. You need to set your printer for condensed because the lines are longer than 80 characters.

About the listing file PROB04.LST, you'll notice that this resembles the source file PROB04.ASM but with three extra columns of numbers to the left. The left most column is line numbers and is just for reference. The next column is addresses or memory locations. They mean different things depending on what part of the listing your looking at. Notice that on line 3 there is an org 8. This is telling the assembler to start reserving internal ram locations with location 8. The next line has an 8 in the second column. This is the location reserved for the variable first. Line 5 has a 9 in this column. Location 9 is reserved for the variable second. I mention these because in the simulator there are no labels shown but instead you'll see the numbers 8 and 9 used. This is some of what the assembler takes care of for you. You get to reference the variables by name in the source file, while the micro uses numbers to reference them. Now look at line 7. Here is an org 0. This tells the assembler to start assembling code at location 0000h. Notice that on line 8 there is a third column of numbers. This is the actual opcode generated by the assembler for a given instruction. You will see these numbers in the simulator in the code window along with the numbers in the second column.

I tell you this to show you the relationship between the listing file and the code window in the simulator. Since there are no labels in the simulator, the listing file can be used to interpret the code window in the simulator and keep up with where you are in the simulator.

With the simulator started and PROB04.HEX loaded, notice that all the registers have a 0 in them to start with. Also notice the first 4 lines in the code window. These are the 4 instructions in the listing file. See the simularity between the 2nd and 3rd colums in the listing file and the code window. Also notice that none of the lines in the listing file above the instructions are listed in the code window. All these lines are for the assembler only and don't generate any code.

Now single step once. Notice that in the Int Ram window, on the second line there is an 0008 and beside that an 02. This is the 02 that was loaded into first which had location 8 reserved for it by the assembler. So the result of MOV first,#2 was to put a 2 in location 8 of the internal ram. The layout of the Int Ram window is the location followed by 8 bytes of data. The 8th byte on that line is location 07h. The next line starts with location 08h and goes through location 0fh. The next line starts with location 10h and ends with location 17h, and so on.

Now single step once. Notice that location 09 in the internal ram now has a 2 in it. Single step again and notice that there is now a 02 in the accumulator. We have just moved the 2 from location 8(first) and put it in the accumulator. Single step again and notice that the accumulator has a 04 in it. We have just added location 9(secnd) to the accumulator with the result in the accumulator. We are now done with the program, and the second session of using the simulator. Exit the simulator to get ready for the next and last example.

Now start the simulator and load the PROB03.HEX file. In this example we will modify port lines to simulate a switch being closed. Remembering back to PROB03.ASM, p1 bit 0 was where we connected the switch and p0 bit 0 is the line connected to the LED. If the switch is closed we will have a 1 on p1 bit 0. If we have a 1 on p0 bit 0, the LED will be lit.

Notice that in the Register window in the simulator, on the left side line 3 is P0 followed by FF and 11111111. The FF is the hex representation of 11111111b. The 1's are the individual bits or lines of p0, with the left most being bit 7 and the right most bit 0. The same holds true for p1. So currently there is all 1's on p0 and all 1's on p1. So the switch is closed and the LED is lit. Also notice that across the top of the register window is the CY or carry flag and it is currently a 0. Also the first line in the code window has the instruction MOV C,P1.0. This says to move port 1 bit 0 into the carry flag. Single step once. Now see that the CY has a 1 in it. We have just moved the 1 on bit 0 p1 into the carry flag. Single step again. This moved the 1 in the carry flag to p0 bit 0. But since we already had a 1 on that bit, no difference was noticed. Notice that the highlighted line in the code window is a SJMP 0000. Notice that in the listing file PROB03.LST there is a label on line 8 called start. Also notice in the address column is a 0000h. So the label start is at location 0000h. On line 10 of the listing file is a sjmp start. This says to jump to the label start. Since start is at 0000h, that is the address shown in the code window. This says to jump to location 0000h. This again illustrates the relationship of the listing file to the code window Single step once more. Now we are back to the first instruction.

Now we are going to set bit 0 of p1 to a 0. Do this by first pressing F6. Now press I for internal ram. Now F for fill. Now enter 90 for the start, 90 for the end, and 00 for the byte. Answer Y to the following prompt. Now see that p1 has a 00h or 00000000b in it. We have not only put a 0 in bit 0 but a 0 in all the bits. We put a 90h for the start and stop address for the fill because p1 is at address 90h in the 8051 and can't be changed. If you notice in the listing file all the names and numbers that follow the program listing. These are all the reserved names in the assembler and the corresponding addresses of each inside the 8051. Find P1 and you will see the address 0090 to the right of it. This is the address of p1 in the 8051. So we entered a start for the fill of 90 and a 90 for the end of the fill. This selected the one location for the fill. You can specify a range of addresses and fill all of them at once. That is why it asks for a start and an ending adress for the fill. But since we only wanted to alter p1, we entered the same address for the start and end. Now the switch would be open. Press F5 to switch back to Execute. Now single step once. Notice that the carry flag now has a 0 in it. We have just moved the 0 in p1 bit 0 into the carry flag. Single step once. Notice that bit 0 of port 0 now has a 0 in it and the rest of the bits are still 1's. The LED would now be off. Single step once. Now we're back to the first instruction.

We can now put all 1's back into p1 to simulate the switch being closed again. Do this by repeating the above steps only when it asks for the byte of data, enter FF (make sure CAPS is set on your keyboard or it won't accept the entry) instead of 00. Now single step through the instructions again and you will see a 1 in p0 bit 0. This would again lite the LED. You can now exit the simulator. We have finished this program example.

Well this has been somewhat sticky, having to look at the listing file and the code window, navigating the simulator, loading programs and altering register contents. But I hope this has given you some insight into the simulator, the relationship of the listing file to the code window, and some differences between the assembly source file and the hex object file that is generated by the assembler. The fact that labels are dropped in the assembly process and replaced by absolute addresses for the micro to use. How the assembler lets you write a program using english names and labels in a form much easier for the programmer to keep up with. Without an assembler, the task of writing a program would be extremely difficult, since what ends up inside the micro is just numbers.

In my early days of micro building I hand assembled each instruction into the hex opcodes and programmed an EPROM with them. The time consummed this way was enormous. It was an exercise in converting assembly language into opcodes that, in and of itself, was a total waste of time. I had to keep an intense concentration level while doing this, and one mistake would cause the program not to work. I would then have to go through my hand written program and not only check the logic of my instructions, but whether I had properly assembled them into opcodes. I never want to do that again. The tools I have furnished you with, I would have given practically anything to have had back then. There were such tools but they cost several thousand dollars, way beyond my price range. I considered my time to be cheap and went the route I did for that reason. But time isn't cheap in reality, and anything you can use to better utilize it is a must. I probably should have came up with the money to buy these tools back then but I didn't. One project I worked on for Levi Strauss, took me 18 months to finish. With these tools I could have done it in a few weeks, I'm sure. Well enough of that.

In the next lesson we will look at the second phase of writing a complete system. The first phase is to define the system and what you want to accomplish with it. The next is to write all the subroutines and functions that will be repeated throughout the system. These are called the BIOS (Basic Input/Output System) routines and OS (Operating System) routines.

My home page is http://www.hkrmicrop.com/personal/index.html .

On to lesson 8.