Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help understanding assembly code

Status
Not open for further replies.

KMD

New Member
Hi all, I'm just getting started with micro controllers, my first programmer and chips should be arriving later this week :p.

I'm teaching myself assembly language from various tutorials on the web. The one I'm currently using is from this site.
**broken link removed**

If you follow the link, it shows how to set up a delay loop to flash a LED. I think I understand everything there like: how the ports are set up, how the pins are alternately set high and low and why the delay loop is needed and how it works. The part that is confusing me, why is it not necessary to reset the values COUNT1 and COUNT2 after each loop.

According to the tutorial, the 2 register locations 08h and 09h start off with 255 in them and we are calling 08h COUNT1, and 09h COUNT2.
If decfsz COUNT1,1 keeps running over and over again until COUNT1 = 0, how can COUNT1 be used again in another loop without being reset to 255? What am I missing here?

Thanks for any help.
 
When the loop is executed again then Count1 & 2 will both equal zero and when decremented will wrap around to 255 (-1) .So the second time the delay is executed it will actually be 256 * 256 instead of 255 * 256 (not 255*255 as stated on the web page because Count1 will get set to zero each time).

HTH

Mike.
 
Ahhh, thanks, that explains it.
 
Dear KMD,

I'm concerned that this particular tutorial may be teaching you some bad habits...

May I make a couple suggestions please?

Specify and use the 'include' file for the processor you're using in your source file... The 'include' file contains the register names and bit names and you won't have to declare them in your program... I know this may seem like extra work as you're getting started but you'll find that using the register and bit names already defined in the 'include' file will help make your program easier to read, debug, and support...

Use the bit names in your program... Most of us know that a bsf STATUS,RP0 statement will select Bank 1 but bsf STATUS,5 is not as meaningful (yes I know, it's spelled out in the comment, but still, it's a good programming habit to learn early on)...

If you're not doing so already, be sure to include 'config' lines in your program... Setting the config' bits correctly can be a stumbling block for many newcomers...

Code:
;*
;*  Filename: 12F683 Serial.asm	
;*    Author: Mike McLaren, K8LH   (k8lh@arrl.net)
;*      Date: 27-Nov-04  (rev 22-Dec-04)
;*
;*   Test 'bit-banged' 9600 baud serial I/O Routines
;*   Uses 12F683 INTOSC at 8-MHz
;*
;*     MPLab: 7.00    (tabs=8)
;*     MPAsm: 3.90
;*
        #include	<p12f683.inc>

        __config	_FCMEN_OFF&_IESO_OFF&_BOD_OFF&_CPD_OFF&_CP_OFF&_MCLRE_OFF& _PWRTE_ON&_WDT_OFF&_INTOSCIO


        org	0x0000

RESET   clrf    STATUS          ;                              |B0
;
; Initialization
;
MAIN    movlw   b'00000111'     ;                              |B0
        movwf   CMCON0          ;digital I/O                   |B0
        bsf     STATUS,RP0      ;bank 1                        |B1
        clrf    ANSEL           ;digital I/O                   |B1
        movlw   b'01110001'     ;                              |B1
        movwf   OSCCON          ;8-mhz INTOSC system clock     |B1
        movlw   b'00001100'     ;GP3 & GP2 are inputs          |B1
        movwf   TRISIO          ;                              |B1
;

I hope you don't mind the suggestions...

Best wishes... Have fun... Regards, Mike

<added>

By the way, if you haven't already done so, please check out the fine tutorials on Nigel Goodwin's web site...
 
Thank you very much Mike, I'm happy to get all the advice I can, and don't worry at all about offending me. I really hate trying to unlearn bad habits so I would like to learn this stuff the right way, first. I'm still disappointed in the 2 java courses I've taken, all they focused on was syntax and a little on OO programming methods, nothing on correct style at all. I'm currently an EE student and expect to take a micro controller course next fall, so I'm looking to get a head start on this now, might as well learn it right the first time.

If you don't mind, I'd like to ask you a few questions about the code you posted.

First off, can you tell me how to post it in a neat little box like that? It makes it much easier to read :) . I also like the column at the end of each comment that shows what bank your in at all times, I can see that being a useful thing to add.

So the first few lines are comments detailing the file info.
How is software usually dated? As in, do you date it from the day you started on it? Compiled it? Finished it? Also, I assume the December date represents a revision of some kind, how it that usually handled?
I'm not sure what info this line is trying to convey:
line 6 ;* Test 'bit-banged' 9600 baud serial I/O Routines

MPLab and MPAsm I assume are the programs used to write and compile the program. Speaking of which, I've downloaded MPLAB IDE 7.11 and have it installed. Is this an all in one program? will I need anything else (other then the programmer, chip, and cable) to write a program, compile the program, and then install it on the chip?

I didn't know anything about the #include statement until you mentioned it, so when I use it, it will automatically create a constant called TRISA and PORTA (or whatever the official names are) with the correct addresses of the registers? Where can I get the information that's contained in the p12f683.inc file?

What are the config commands for? I have not actually written and compiled a program yet so I don't know what they would be used for.

I did take a look at Nigel Goodwin's tutorial, but it looked like it assumed you already knew assembly and concepts, I'll take another look at it to see if I was mistaken on that point.

Thanks a lot for any more help you can give Mike.

Keith
 
KMD said:
First off, can you tell me how to post it in a neat little box like that? It makes it much easier to read :) .

You simply press the 'Code' button before and after the section of code, it prevents your browser destroying the formatting.

I didn't know anything about the #include statement until you mentioned it, so when I use it, it will automatically create a constant called TRISA and PORTA (or whatever the official names are) with the correct addresses of the registers? Where can I get the information that's contained in the p12f683.inc file?

From the p12f683.inc file, it's only a simple text file, it's installed (along with all the other .inc files) when you install MPLAB.

What are the config commands for? I have not actually written and compiled a program yet so I don't know what they would be used for.

The __CONFIG line sets the configuration fuses, it's very important to set them in the source code, then they are assembled in to the .HEX file along with the program, and then set correctly when you program the chip.

I did take a look at Nigel Goodwin's tutorial, but it looked like it assumed you already knew assembly and concepts, I'll take another look at it to see if I was mistaken on that point.

They are not intended as a complete beginners guide, they assume you have the datasheet from MicroChip, which tells you all the instructions. Use then in conjunction with other tutorials, like the one you are already looking at! - you might like to look at the online book at **broken link removed**.

I consider the way to learn is to experiment, look at working code, then modify it to make it do what you want - you learn even more when it doesn't do what you want :lol:
 
Hi Keith,

First off, can you tell me how to post it in a neat little box like that?

Use the <Code> button (on the tool bar when in the "Post a reply" window) before and after your pasted code...



So the first few lines are comments detailing the file info.
How is software usually dated? As in, do you date it from the day you started on it? Compiled it? Finished it? Also, I assume the December date represents a revision of some kind, how it that usually handled?

Everyone seems to use different dating conventions... Some will date stamp each revision with comments on bugs fixed and features added for that revision... You'll find plenty of sample programs if you search around (PICList is one possible source)...

I'm not sure what info this line is trying to convey:
line 6 ;* Test 'bit-banged' 9600 baud serial I/O Routines

That's my brief description of the 12F683 Serial.asm program... The little 8-pin 12F683 does not have a built-in USART peripheral like some of the other PICs... This program (available on PICList) demonstrates one method of software (bit-banged) 9600 baud Serial I/O using TMR2 generated 104-usec interrupts...

MPLab and MPAsm I assume are the programs used to write and compile the program. Speaking of which, I've downloaded MPLAB IDE 7.11 and have it installed. Is this an all in one program? will I need anything else (other then the programmer, chip, and cable) to write a program, compile the program, and then install it on the chip?

If you program in Assembler, the MPLab package is pretty much all you need (besides the programmer)...

I didn't know anything about the #include statement until you mentioned it, so when I use it, it will automatically create a constant called TRISA and PORTA (or whatever the official names are) with the correct addresses of the registers? Where can I get the information that's contained in the p12f683.inc file?

The "include" file is an assembly source file that contains labels and equates for each register and bit... There's an "include" file for each PIC mcu in the C:\Program Files\Microchip\MPASM Suite folder after you install MPLab... Open one up and browse through it in MPLab...

What are the config commands for? I have not actually written and compiled a program yet so I don't know what they would be used for.

You should definately go through the Microchip MPLab sample project tutorial... I'm not sure which document it is but I'm pretty sure you'll find it on the MPLab web page...

Hmmm, how to explain configuration fuses and the 'config' line in your assembly language program file? Let me brain-storm it and provide a follow-up post, if someone else doesn't jump in with an elegant description... I'm not very good at explaining things...

Regards, Mike
 
Keith,

Excuse me for duplicating Nigel's post... I didn't refresh before posting and I guess he beat me to the punch (grin)...

As Nigel mentioned, please study the vast amount of source code out there... I'm sure it'll stimulate you and force you back to that Data Sheet many many times... Also, search the Electro-Tech forum and the Microchip forum... Most of the 'newbie' questions you'll ask in the coming months have already been answered, probably in great detail...

Best wishes... Regards, Mike
 
Thanks for all the help guys, it's so tough to dive into a completely new subject like this without a little help. Online tutorials are great, but you never know if your learning anything useful. It's also easy to be overwhelmed by the shear volume of material out there.
 
Some thoughts on code style:

This is an amusing article about how to write really bad code. If you make sure to avoid the things mentioned your code will be a lot better.

https://thc.org/root/phun/unmaintain.html

You can never comment too much. The more info you put into your comments the easier time you will have remembering what your were doing. When I have to write assembly I comment it with the equivalent C code which tends to be much easier to understand. It's easy to assume that you don't need to comment because you will remember what you did later but you'll be suprised how quickly you forget; a little typing now will save a lot of debugging later.

Another thing I like to do is write the comments before writing code. If you have a comment for each function defining its inputs, outputs, and effects you can catch a lot of logic problems before you write any code.

People develop a lot of bad coding habits in school. I remember the crappy code I used to write :D .
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top