Basic understanding of assembly command MPLABx IDE

Intro to dsPIC register:

In this post i will cover some basic commands that you can use with your dsPIC.  First of all you need to understand the dspic RAM.  In the dispic we have some of the registry entry that are fix and some registry that are usable.  The reserved range of register is chosen by the manufacturer.  Those include w0 to w15, the status, the portx and the TRISx.  The w0-w15 are used to quickly write some information to the memory to use within your main program.  The status registry is used automatically, when you make a mathematic operation or a logical test some of the bits in this registry will turned on or off.  For example if you use the command btst that i will cover next in this post, this will make a test to see if a bit of a certain registry is one or  zero.  (useful to see if a button as been pushed)  When you pass the test if the bit is at zero the Z bit in the status registry will stay at zero, if the bit is 1 the Z bit will be automatically set to 1.  There is also a C bit(carry bit) that is set when you do some mathematical operation, we will cover that later.  The other part of the registry is open for you to use freely.  I will show you some command that you can do to use those.

Direct addressing:

I will write some commands and explain those to you after.

mov 0x27FE, w0
The command mov will move the content of the register 0x27FE(whatever the content is) and we overwrite the content of w0 with it.  The first part of my example show the two register label before the = and after this is their content.
ie: Before the command
wo=0x55550x27FE=0x1234
After
wo=0x1234
0x27FE=0x1234

DEC 0x1000
The command dec will decrement the value inside the register 0x1000
ie: Before
0x1000=0x5555
After
ox1000=0x5554

AND 0x1000
this command will multiply the value at the level of the bites.  It will use w0 as default.
Before
0x1000=0x5555w0=0x332c
After
0x1000=1104
w0=0x332c

AND 0x1000, w0
this will multiply the wo
Before
0x1000=0x5555
w0=0x332c
After
0x1000=0x5555
w0=0x1104

mov #b0000000000000010, w0
this will overwrite the registry w0 with our chosen open bit.
ie : w0=0x0002

Immediat Addressing :

mov #0x1000, w0
This command is the same as the last one but we put the binay number in hexa.  If you use # this will be considered as a number by MPLABx.
Before
0x1000=0x5555
w0=0x5248
After
0x1000=0x5555
wo=0x1000

mov.b #0x0030, w0
This command is the normal mov command but we added a .b, this .b mean that we will move only the less significant ‘octet’ of the number 0x0030 to the w0 registery
Before
w0=0x5555
After
w0=0x5530

Indirect addressing :

mov [w0], [w1]
this will search w0, go inside and see the value of the content.  After it will search the registry that match with the content and copy is value.  It will after go in w1, see what is the content and go to the register named as the content to finally paste the value there.  It will be better with an example.
Before
wo=0x1000
w1=0x1002
0x1000=0x8BFA
0x1002=0x25AC
After
w0=0x1000
w1=0x1002
0x1000=0x8BFA
0x1002=0x8BFA

mov [w0+w1], [w2]

Before
wo=0x1000
w1=0x1002
w2=0x5555
0x1000=0x8BFA
0x1002=0x25AC

Pres-Incrementation :

mov [++w0], w2
This is almost the same as the last piece of code but we go in w0 to se the value, we add 2(the number of +) and after we go hunt the value in the register.
Before
wo=0x0802
w2=0xFBC4
0x0802=0x4E81
0x0804=0xC7A6
After
wo=0x0804
w2=0xC7A6
0x0802=0x4E81
0x0804=0xC7A6

Post-incrementation :

mov [w0++], w2
This is the same but you add the +2 value after you go hunting for the value of w0
Before
wo=0x0802
w2=0xFBC4
0x0802=0x4E81
0x0804=0xC7A6
After
wo=0x0804
w2=0x4E81
0x0802=0x4E81
0x0804=0xC7A6

OTHER USEFULL COMMENDS

bclr PORTx, #RC13
This command make the bit number #RC13 go from 1 to 0 in the register PORTx.

bclr w0, #7
This command make the bit number #7 go from 0 to 1 in the register w0.

 btst PORTD, #RD9
It is used to make a test to see if the bit number #RD9 is one or zero. (it can be used with a button or a switch).  After the test it modify the status registery ‘Z’ero to set it to one if the bit is not zero in #RD9

btg PORTC, #RC13
This command switch the state of a bit, if it is set to one after the instruction its zero.

STATUS Register

STATUS Register Legend

Fig. 2-1 STATUS Register

The STATUS register contains: the arithmetic status of the W register, the RESET status and the bank select bits for data memory. One should be careful when writing a value to this register because if you do it wrong, the results may be different than expected. For example, if you try to clear all bits using the CLRF STATUS instruction, the result in the register will be 000xx1xx instead of the expected 00000000. Such errors occur because some of the bits of this register are set or cleared according to the hardware as well as because the bits 3 and 4 are readable only. For these reasons, if it is required to change its content (for example, to change active bank), it is recommended to use only instructions which do not affect any Status bits (C, DC and Z). Refer to “Instruction Set Summary”.

  • IRP – Bit selects register bank. It is used for indirect addressing.
    • 1 – Banks 0 and 1 are active (memory location 00h-FFh)
    • 0 – Banks 2 and 3 are active (memory location 100h-1FFh)
  • RP1,RP0 – Bits select register bank. They are used for direct addressing.
RP1 RP0 Active Bank
0 0 Bank0
0 1 Bank1
1 0 Bank2
1 1 Bank3

Table 2-1

  • TO – Time-out bit.
    • 1 – After power-on or after executing CLRWDT instruction which resets watch-dog timer or SLEEP instruction which sets the microcontroller into low-consumption mode.
    • 0 – After watch-dog timer time-out has occurred.
  • PD – Power-down bit.
    • 1 – After power-on or after executing CLRWDT instruction which resets watch-dog timer.
    • 0 – After executing SLEEP instruction which sets the microcontroller into low-consumption mode.
  • Z – Zero bit
    • 1 – The result of an arithmetic or logic operation is zero.
    • 0 – The result of an arithmetic or logic operation is different from zero.
  • DC – Digit carry/borrow bit is changed during addition and subtraction if an “overflow” or a “borrow” of the result occurs.
    • 1 – A carry-out from the 4th low-order bit of the result has occurred.
    • 0 – No carry-out from the 4th low-order bit of the result has occurred.
  • C – Carry/Borrow bit is changed during addition and subtraction if an “overflow” or a “borrow” of the result occurs, i.e. if the result is greater than 255 or less than 0.
    • 1 – A carry-out from the most significant bit of the result has occurred.
    • 0 – No carry-out from the most significant bit of the result has occurred.

The status register part came from : http://www.mikroe.com/chapters/view/3/chapter-2-core-sfrs/

If you want some examples of commends go to : http://mrjester.hapisan.com/04_MC68/Sect06Part02/Index.html

Leave a Reply

Your email address will not be published. Required fields are marked *