Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Some doubts related to ASM (PIC)

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
750
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,049
Today I just started to learn ASM for PIC.


I could build the ABSOLUTE CODE successfully and it is tested ok in both simulator and Hardware.


There after, i tried to download that code using a bootloader in PIC16F877A.
But that code(LED blinking) is not working when i use bootloader to download the code to PIC.
Then i guess, i should generate RELOCATABLE code....Is it?
IF YES:
But it is showing many errors...
Then what modifications are to be done in below LED blinking program, so that i could generate RELOCATABLE CODE?



Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
;*****Set up the Constants**** 
STATUS    equ       03h                 ;Address of the STATUS register
TRISD     equ       88h                 ;Address of the tristate register for port A
PORTD     equ       08h                 ;Address of Port A
COUNT1    equ       20h                 ;First counter for our delay loops
COUNT2    equ       21h                 ;Second counter for our delay loops 
COUNT3    equ       23h                 ;Second counter for our delay loops 
 
;****Set up the port**** 
 
bsf             STATUS,5                                ;Switch to Bank 1
movlw           00h                             ;Set the Port A pins
movwf           TRISD                                   ;to output.
bcf                     STATUS,5                                ;Switch back to Bank 0 
movlw           h'10'
movwf           COUNT3
;****Turn the LED off**** 
 
Start   call    Delay
                movlw               h'00'                  
                movwf               PORTD                                                         
 
;****Turn the LED on**** 
              call   Delay
              movlw              h'ff'             
              movwf              PORTD                                                   
              goto               Start                                                                              
 
;delay routine
Delay
Loop1               decfsz              COUNT1,1     
                        goto                Loop1        
                        decfsz              COUNT2,1     
                        goto                Loop1 
                        decfsz              COUNT3,1       
                        goto                Loop1               
                        movlw h'5'
                        movwf COUNT3
return 
end

 
Last edited:

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
47,440
Helped
14,034
Reputation
28,321
Reaction score
12,684
Trophy points
1,393
Location
Bochum, Germany
Activity points
275,931
I don't see, why the code won't be relocatable by the linker. It's a question of how to set up the linker, e.g. to set "Start" to the absolute address expected by the bootloader. But I must admit, I'm not particularly motivated to write pure assembly coded applications. It's important however to understand the assembly code for debugging purposes and to be able to code critical parts of a C based application in assembler. But I'm sure, that you can find all required details in the MPLAB ASM documentation.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,352
Helped
4,723
Reputation
9,459
Reaction score
4,491
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
122,485
Assuming you are using MPLAB:

1. Include the header file for the 16F877A. Then you have all the register and bit names defined for you and you don't have to do it yourself.
2. Relocatable code should not have absolute addresses within it. The decision as to where data and code is located should be made by the assembler and linker.

Baically, wrap the instructions inside a 'code' section and the data inside either an 'idata' (if initialized) or 'udata' (if not initialized) section.
In the data sections, follow the name of the variable with 'res x' where x is the number of addresses to reserve. For example:

MyLabel udata
COUNT1 res 1
COUNT2 res 1
COUNT3 res 1

You can omit 'MyLabel' if you like, it works like any other label in the program. You can also follow 'udata' with a fixed address if you want to, otherwise the assembler will allocate one for you.
The 'res' statement means 'reserve', in other words allocate that many bytes for the variable before it. You could for example use something like 'TransmitBuffer res 20' to reserve 20 bytes of space.
The working part of the program should be the same for absolute or relocatable code, it's only where it ends up in the memory space that is decided for you.

Brian.
 

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
750
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,049
Now i think it is really tough for me to understand:-|, could you give me a small example?
 

sankar.m8

Advanced Member level 4
Joined
Apr 15, 2011
Messages
106
Helped
14
Reputation
28
Reaction score
14
Trophy points
1,298
Location
Chennai, Tamil Nadu, India
Activity points
1,884
For MPLAB IDE

LIST P=16f877a
#include <p16f877a.inc>

__CONFIG H'3F39'

#define LED PORTB,0



CBLOCK H'20'
delay1
delay2
ENDC

ORG 0
goto main
ORG 4

init:
bsf STATUS,RP0
movlw B'00000000'
movwf TRISB
bcf STATUS,RP0
clrf PORTB
return

*/Delay subroutine

nsec_a:
movlw D'100'
movwf delay1
nsecloop_a:
nop
nop
nop
nop
nop
nop
nop
nop
nop
clrwdt
decfsz delay1
goto nsecloop_a
return

nsec1a:
call nsec_a
decfsz delay2
goto nsec1a
return

msec250a:
movlw D'250'
movwf delay2
goto nsec1a


main:

call init
loop:
bsf LED
call msec250a
bcf LED
call msec250a
goto loop

end


In the New project, Use this code...
Add the source file ...the same code..
Select the device PIC16f877A
Built it
you will get the HEX file in the saved location...(default in the c/prog files/Microchip/)
 

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
750
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,049
Showing error @ sankar

Code:
----------------------------------------------------------------------
Debug build of project `C:\Documents and Settings\vinod\My Documents\pic projects\MY ASMMMMMM.mcp' started.
Language tool versions: MPASMWIN.exe v5.36, mplink.exe v4.36, mplib.exe v4.36
Preprocessor symbol `__DEBUG' is defined.
Sat Jun 25 12:38:17 2011
----------------------------------------------------------------------
Make: The target "E:\microcontroller\new 2842011 c files\asm3.o" is out of date.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F877A "asm3.asm" /l"asm3.lst" /e"asm3.err" /d__DEBUG=1
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 1 : Found directive in column 1. (LIST)
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 4 : Found directive in column 1. (__CONFIG)
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 10 : Found directive in column 1. (CBLOCK)
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 13 : Found directive in column 1. (ENDC)
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 15 : Found directive in column 1. (ORG)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 16 : Found opcode in column 1. (goto)
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 17 : Found directive in column 1. (ORG)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 20 : Found opcode in column 1. (bsf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 21 : Found opcode in column 1. (movlw)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 22 : Found opcode in column 1. (movwf)
Message[302] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 22 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 23 : Found opcode in column 1. (bcf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 24 : Found opcode in column 1. (clrf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 25 : Found opcode in column 1. (return)
Error[108]   E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 27 : Illegal character (*)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 30 : Found opcode in column 1. (movlw)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 31 : Found opcode in column 1. (movwf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 33 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 34 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 35 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 36 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 37 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 38 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 39 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 40 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 41 : Found opcode in column 1. (nop)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 42 : Found opcode in column 1. (clrwdt)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 43 : Found opcode in column 1. (decfsz)
Message[305] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 43 : Using default destination of 1 (file).
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 44 : Found opcode in column 1. (goto)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 45 : Found opcode in column 1. (return)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 48 : Found opcode in column 1. (call)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 49 : Found opcode in column 1. (decfsz)
Message[305] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 49 : Using default destination of 1 (file).
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 50 : Found opcode in column 1. (goto)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 51 : Found opcode in column 1. (return)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 54 : Found opcode in column 1. (movlw)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 55 : Found opcode in column 1. (movwf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 56 : Found opcode in column 1. (goto)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 61 : Found opcode in column 1. (call)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 63 : Found opcode in column 1. (bsf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 64 : Found opcode in column 1. (call)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 65 : Found opcode in column 1. (bcf)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 66 : Found opcode in column 1. (call)
Warning[203] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 67 : Found opcode in column 1. (goto)
Warning[205] E:\MICROCONTROLLER\NEW 2842011 C FILES\ASM3.ASM 69 : Found directive in column 1. (end)
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `C:\Documents and Settings\vinod\My Documents\pic projects\MY ASMMMMMM.mcp' failed.
Language tool versions: MPASMWIN.exe v5.36, mplink.exe v4.36, mplib.exe v4.36
Preprocessor symbol `__DEBUG' is defined.
Sat Jun 25 12:38:20 2011
----------------------------------------------------------------------
BUILD FAILED
 

sankar.m8

Advanced Member level 4
Joined
Apr 15, 2011
Messages
106
Helped
14
Reputation
28
Reaction score
14
Trophy points
1,298
Location
Chennai, Tamil Nadu, India
Activity points
1,884
Goto the microchip folder in program files, and search for the p16f877a.inc file in the library.....if it is there, then give the path in the #include <>
and......dnt forget to select the device from the options......
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,485
Which bootloader are you using? Microchip's AN851, AN1310, etc?

Most bootloaders include at least a sample linker script with their installation.

Have a look in the installation directory on your system for a file with a .lkr extension.

When you identify the bootloader version and the existence of the linker script, I can step you through the process.

Ciao

---------- Post added at 09:12 ---------- Previous post was at 09:01 ----------

By the way, your looking for the linker script in the bootloader's installation directory, not MPLAB's. Bootloaders occupy various sections of flash, therefore the application code must be relocated to areas specified by the bootloader. Otherwise there is a high probability you could overwrite portions of the bootloader itself.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,352
Helped
4,723
Reputation
9,459
Reaction score
4,491
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
122,485
Don't give up Vinodstanur all those errors are just because Sankar.m8 didn't post the code between tags and it was reformatted by the forum software. The only thng that should be in the first position of a line of code is a directive or a label, the errors are because the leading spaces were removed and the opcodes were moved to the start of the line.

The 'LIST' and '#include' lines are correct and they save you having to define the PIC registers in your code. When they are there, you can use register names and bit names without using any 'equ' statements. For example you can use 'bsf INTCON,RBIE' and the assembler will know it means bit 3 of register 0x0B.

Sankar.m8 posted code which is absolute though and you want relocatable code. The working part of the code is exactly the same but you remove all references to memory addresses unless you explicity have to position them in a fixed place. If you are only using one module (.asm) file it doesn't really matter whether you use absolute or relocatable code as it will work at whatever address to give it. The reason for using relocatable code is it allows you to use more than one source file and importantly, the source might be a useful routine you or someone else developed in another project.

For example:
You write a serial terminal program and it starts at address 0x0000, ends at 0x0100 with varaibles using addresses at 0x20 to 0x30
You have an LED flashing program and it starts at address 0x0000, ends at address 0x0020 and variables use addresses 0x20 to 0x25
You have a program to make a beep sound, it starts at address 0x000, ends at 0x0040 and variables use addresses 0x20 to 0x50.

Each program is working perfectly in absolute code.
Now you try to make a program that receives serial data, flashes an LED as each byte arrives and beeps after 100 characters, using the code of all three of your programs. The trouble is, they all occupy the same addresses so the result will be lots of warnings about memory being overwritten and if you are lucky, only the last one compiled will actually work.

Now, if they were written in relocatable code, each program would simply be proceeded by the keyword 'code' and all the variables would be in 'udata' or 'idata' blocks with 'res' to say how many bytes each needed. All three programs would be added to the project. When the assembler was run, it would convert the assembly language to binary opcodes and whenever if found a variable it would give it a temporary name and make a note of where in the code it was. The resulting files are called 'object code'. It would do this for all three files. Then the linker program runs, it would go through the object files and change the temporary names to real addresses and also find the best way to fit the the code sections into the available PIC program memory. Each program will be intact but it might put the 'beep' program between the terminal code and the LED code if it fits better that way. It will also arrange the variables so they do not overlap. For example it might place the variables at 0x20 - 0x30, 0x31 - 0x36 and 0x37 - 0x87 so they don't overlap.

You will see why relocatable is more efficient, especially in high level languages where many library functions may be used. It gives freedom to mix them in source code without having to worry about where they end up in the final executable code.

Brian.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,485
Now i think it is really tough for me to understand:-|, could you give me a small example?
As Betwixt indicated, to successfully assemble relocated code you will need to replace some of the assembler directives. I sure one of us could step you through the process, however you may find step by step examples would ease the learning curve.

So have a look at the Gooligum Tutorials 1 - 3 for the Midrange PICs in Assembler:

Gooligum Midrange PIC Assembler Tutorials

The tutorials do an excellent job of guiding you through the process of writing relocated code in assembler. The tutorials use a 12F629 which has only one port refer to as GPIO instead of PORTA, PORTB, etc that you are normally accustom and make use of an internal oscillator. However, you should be able to adapt the lessons to the 16F877A without too much effort.

The first few midrange tutorials do refer occasionally to the baseline tutorials particularly on how to setup a new project and include a linker script, which is a necessity when assembling relocatable code.

Gooligum Baseline PIC Assembler Tutorials

Study the first three tutorials and you should have no trouble writing relocatable code and with the proper linker script, be able to successfully download the code using the bootloader.

Ciao
 

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
750
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,049
Which bootloader are you using? Microchip's AN851, AN1310, etc?

Most bootloaders include at least a sample linker script with their installation.

Have a look in the installation directory on your system for a file with a .lkr extension.

When you identify the bootloader version and the existence of the linker script, I can step you through the process.

Ciao



I am using 'Tiny bootloader' but i couldn't see any .lkr extension in the Tiny bootloader installation folder.

---------- Post added at 10:38 ---------- Previous post was at 10:34 ----------

I downloaded the bootloader from Tiny PIC bootloader
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,485
I'm not familiar with that particular bootloader. I have found it on the NET and will take a look at it. Mean while have a look at:

AN851 - A FLASH Bootloader for PIC16 and PIC18 Devices

It has a sample linker script for the 16F877A and a nice download utility. Download the PDF manual and have a look, it explains why your application code must be relocated to predetermined sections of flash.

And I will have a look at Tiny Bootloader.

---------- Post added at 11:27 ---------- Previous post was at 10:43 ----------

You are correct, there is not a linker script provided. They seem to assume you are writing only absolute code. I have not proofread your code for errors, however if you assemble the following code you should be able to download it successfully using the Tiny Bootloader and run it if properly written:


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
LIST      P=16F877A;, F=INHX8M  
            #include <p16f877a.inc>     
 
__CONFIG  _HS_OSC & _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _LVP_OFF & _DEBUG_OFF & _CPD_OFF
 
 
;*****Set up the Constants**** 
STATUS    equ       03h                 ;Address of the STATUS register
TRISD       equ       88h                 ;Address of the tristate register for port A
PORTD      equ       08h                 ;Address of Port A
COUNT1    equ       20h                 ;First counter for our delay loops
COUNT2    equ       21h                 ;Second counter for our delay loops 
COUNT3    equ       23h                 ;Second counter for our delay loops 
 
[COLOR="#0000FF"];******************* Necessary Code For Tiny Bootloader ******************************
 
ORG       0x0000
            clrf STATUS
            movlw 0x00
            movwf PCLATH
            goto Init
 
;*****************************************************************************[/COLOR]
 
;****Set up the port**** 
 
[COLOR="#0000FF"]Init[/COLOR]       bsf                 STATUS,5                                ;Switch to Bank 1
           movlw                00h                             ;Set the Port A pins
           movwf                TRISD                                   ;to output.
           bcf                  STATUS,5                                ;Switch back to Bank 0 
           movlw                h'10'
           movwf                COUNT3
;****Turn the LED off**** 
 
Start   call    Delay
                movlw               h'00'                  
                movwf               PORTD                                                         
 
;****Turn the LED on**** 
              call   Delay
              movlw              h'ff'             
              movwf              PORTD                                                   
              goto               Start                                                                              
 
;delay routine
Delay
Loop1               decfsz              COUNT1,1     
                        goto                Loop1        
                        decfsz              COUNT2,1     
                        goto                Loop1 
                        decfsz              COUNT3,1       
                        goto                Loop1               
                        movlw h'5'
                        movwf COUNT3
return 
end



This is Absolute not Relocatable Code, I should be able to modify a linker script which would enable you to properly link relocatable code and download it using the Tiny Bootloader. However, 4:30AM here and I going to call it a night. I'll work on the script tomorrow.

Ciao
 
Last edited:

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
750
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,049
Special thanks @ bigdogguru....
Code which you modified is working in Hardware...

Now, i am just reading the links which u provided, and trying to understand the basics...
:-D Ohhh....4:30AM!!! Then Good Night.(or Good Morning)...hehe confused:smile:
 
Last edited:

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,352
Helped
4,723
Reputation
9,459
Reaction score
4,491
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
122,485
One point to take note of:

Bootloader code itself probably should be absolute. The reason being that if it was relocatable it would probably be placed where it would be more useful to put the program code.

Brian.
 
Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top