asm Statement

mikroPascal PRO for dsPIC30/33 and PIC24 allows embedding assembly in the source code by means of the asm statement. Note that you cannot use numerals as absolute addresses for register variables in assembly instructions. You may use symbolic names instead (listing will display these names as well as addresses).

You can group assembly instructions with the asm keyword:

asm
   block of assembly instructions
end;

The only types whose name remains the same in asm as it is in the mikroPascal PRO for dsPIC30/33 and PIC24 are registers, e.g. INTCON, PORTB, WREG, GIE, etc.

mikroPascal PRO for dsPIC30/33 and PIC24 comments are allowed in embedded assembly code.

Accessing variables

Depending on the place of declaration, accessing a variable can be done in several ways :

Here is an example of using asm instructions :

program asm_example;

var myvar : word; absolute 0x2678;
const msg = 'Hello'; org 0x3678;
var myvar1 : dword;

procedure proc(); org 0x1234;
begin
  asm
    nop
  end;
end;

begin
  myvar := 5;
  myvar1 := 0xABCD1234;

  asm
      MOV _myvar, w0                      ; move myvar to W0
      nop
      MOV #6, W0                          ; move literal 6 to W0
      MOV W0, _myvar                      ; move contents of W0 to myvar
      MOV #lo_addr(_myvar), w1            ; retrieve low address word of _myvar and move it to W1 (0x2678 -> W1)
      MOV #hi_addr(_myvar), W1            ; retrieve high address word of _myvar and move it to W1 (0x0000 -> W1)
      MOV #lo_addr(_proc), W0             ; retrieve hi address byte of routine proc and move it to W0 (0x0001 -> W1)
      MOV #lo_addr(_msg), W0              ; retrieve low address word of constant msg and move it to W0 (0x3652 -> W1)
      MOV _myvar1+2, w0                   ; accessing hi word of myvar1 variable and move it to W1 (0xABCD -> W1)
  end;
end.

Asm code and SSA optimization

If asm code is mixed with the Pascal code, keep in mind that the generated code can substantially differ when SSA optimization option is enabled or disabled.
This is due to the fact that SSA optimization uses certain working registers to store routine parameters (W10-W13), rather than storing them onto the function frame.

Because of this, user must be very careful when writing asm code as existing values in the working registers used by SSA optimization can be overwritten.
To avoid this, it is recommended that user includes desired asm code in a separate routine.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code