Assembler Code in Unit - asm Statement

General discussion on mikroPascal for AVR.
Post Reply
Author
Message
JohannesH
Posts: 68
Joined: 23 Apr 2007 07:51

Assembler Code in Unit - asm Statement

#1 Post by JohannesH » 25 Apr 2007 08:08

Hi !

I am new here in the forum and with mikroPascal AVR but I like it very much. Pascal was my first language I learned and programmed in at university. I ordered EasyAVR4 Board and Pascal licence but currently I wait for delivery and in the meantime I work with the demo version 4.0.1.

I want to write a unit and use a mixture of Pascal and Assembly language which is supported by the "asm" statement.

Questions/Problems:

1. Bit number constants
It looks like I cannot use AVR bit number constants with assember statements. E.g.:
TWINST is defined as $07 within ATMega8 Definition file

asm
....
SBRS TWCR,TWINT ==>> Compiler Error
SBRS TWCR,7 ==>> works OK
....
end;
The register name TWCR is recognized fine, but the TWINT constant is not. Is this the way it should work or do I make something wrong?

2. Access Pascal variables within asm
What is the correct way to access a Pascal byte variable from within asm? Where must the variable be defined with a "var" statement? Define it at the very start of the unit before "implementation", after "implementation"? Can I access variables that are local to a procedure or function? Can I access actual parameters passed to the routine wherein the asm statement lives? How to do this correctly? Keep in mind that my assembly code is within a unit, not within the main program - does that make any difference?
It would be very nice if you can give me some simple example how to do this correctly. All I tried till now either lead to an error while compiling (mostly "out of adress limit" or such) or the access to the variable did not actually access the right memory address. I searched here within the forum but could not find answers to my questions.

All feedback is welcome - Please Help - Thank You.
Regards from Vienna/Austria/Europe
Johannes

JohannesH
Posts: 68
Joined: 23 Apr 2007 07:51

#2 Post by JohannesH » 26 Apr 2007 08:19

Hi again!

I did work some more hours on this. For everyone interested here are my findings.

1. Bitnumber Constants
Nothing new - seems that the compiler does not recognize Constants within asm statements. Even a local constant defined within the procedure were the asm statement wants to use the constant leads to compiler error. I am thinking about making a post within the list of feature requests.

2. Using pascal variables within asm
I think here I found out a lot about how it works.
- Global variables (defined outside any routine) from main program or from a unit (defined before "implementation" keyword) can be accessed like this:
LDS R16,_varname or
STS _varname,R16
So, you have to put an underscore character in front of the variable name.

- Variables from a unit defined within the "implementation" part but outside of any routine:
The variable name gets constructed like this
<unitname>_global_main_<variablename> (hope I got it correct)
Again you can use LDS and STS instructions to load and store the content of the variable.

- Variables lokal to a routine and actual parameters passed by value
These are stored on the stack, so one can access this variables but with extreme care. You have to know the relative displacement within the stack and that can easily change with changes in the program code.

- Actual parameters passed by reference
I have not investigated this in very detail because that gets complicated soon. I think it depends on which variables actually are passed (global or local variable when calling routine).

Maybe this is useful for someone.
Comments and feedback are welcome.

Regards
Johannes

brmdjoko
Posts: 2
Joined: 31 Aug 2005 01:47

#3 Post by brmdjoko » 19 Apr 2008 15:34

Hi, your work was great.

I'm newbie in programming on micro controller.
I usually used Delphi for programming on PC, where Delphi prepared all
initialization process in routine using assembler.

Could you continue your explanation/finding how to make function/procedure using actual parameters passed by reference in mixture Pascal and Assembly?

JohannesH
Posts: 68
Joined: 23 Apr 2007 07:51

#4 Post by JohannesH » 20 Apr 2008 09:40

Thank You!

Currently I have no new findings because I use mikroPascal very seldom now. I am waiting for the "new major" version which should fix a lot of problems. When this will be available maybe I find the time to do more indepth evaluation.
Normally this information (called "Calling Conventions") is part of the product documentation but I could not find this for mikroPascal?!

Regards
Johannes

brmdjoko
Posts: 2
Joined: 31 Aug 2005 01:47

#5 Post by brmdjoko » 21 Apr 2008 04:15

Thank you for your soon response.

Though it is not answering my question, I really appreciate your reply.
May be I could find in another place or even I could find my self based on your finding and we could share in this forum.

JohannesH
Posts: 68
Joined: 23 Apr 2007 07:51

#6 Post by JohannesH » 21 Apr 2008 06:25

Yes, if you find anything it would be nice if you can share it here.

Johannes

Post Reply

Return to “mikroPascal for AVR General”