There is a question which comes up in the mock exam which involves doing maths. Attached is the exact question. From what I understand, doing any kind of addition, division etc on a PIC in assembly is extremely difficult, this is some example code ive found:
Does this question really have to be this difficult, or is there any easier way of doing it?
This particular problem is quite straight forward : As (L) and (S) are 25% this a simple divide by 4 or (shift right 2), and (A) is 50) or divide by 2 (shift right 1). To do it properly you need two routines Multiply and divide.You must remember that with fixed point arithmetic you need to take care of the decimal place.
Just use the two routines needed by Peter Hemsley.
Thank you for the reply. So I understand this correctly, to divide a number by two, you shift the binary once to the right, so 0100 would become 0010. To divide by four it would be 0001.
Then in binary we would have:
L = 0010 1000
S = 0011 0010
A = 0011 1100
In order to get the right percentages:
L = 0000 1010 (moved twice to the right)
S = 0000 1100 (moved twice to the right)
A = 0001 1110 (moved once to the right)
Then we need to add them up and divide by the number of entries, in this case, 3. Is this all right up to this point?
Yes! but if you look at S then you will notice that 50 * 25% = 12.5, but as this is integer math you have lost the 0.5, so your end calculation will be wrong. In this case you are going to need Multiplication!!
Look at it this way 50 * 25 = 12500 or (for precision ) 12.500, No division needed. You really need to know how precise they need the results.
To prevent loss of accuracy do multiplication first.
Answer = (L+S+(A*2))/4
So, shift A left once, add L and S then shift it right twice.
Unfortunately this requires a two byte location for the answer so a compromise might be,
Answer = (L/2+S/2+A)/2
In the example
Answer = (40/2+50/2+60)/2 = 52.5 rounded = 52%
in code,
Code:
bcf STATUS,C ;clear Carry
rrf L,W ;get L/2
movwf Temp ;save it
bcf STATUS,C
rrf S,W ;get S/2
addwf Temp,W ;add L/2
addwf A,W ;Add A
movwf Temp ;save it
bcf STATUS,C
rrf Temp,W ;divide by 2
;answer in W
But, don't be tempted to use this code as your answer. It's far too efficient to have been done by someone at your level and your lecturer just won't believe you. He will probably ask so many questions until he knows it's not you code.
KISS im quite confused by some of the terminology in your post, like "Push", stack and stack pointer. I really think this is too advance for me, even if it may seem simple.
Your code should work fine. The method of pushing variables and functions onto a stack is how some systems function but it is not suitable for a pic microcontroller.
On the contrary, Mike!! I program in C (like most people) and the stack on the pic needs due consideration..
Spadez there are two types of stack Hardware and software. On the pic the hardware stack is used EVERY function call and EVERY loop. All you need to do is keep an eye on stack is to remeber how many "nested" routines there are example..
Code:
SomeFunction // program counter on stack stack = 1
do one thing
call otherfunction
do something else
return
otherFunction // program counter on stack stack = 2
do another thing
call yetAnotherFunction
finish
return
yetanotherFunction // program counter on stack stack = 3
do // program counter on stack stack = 4
adjust someting
call adjustFunction // program counter on stack stack = 5
while
return
adjustFunction
make adjustment
return
On small pics there are ONLY 8 levels you can see how quickly they disappear, dosn't matter what language you're programming in!!
When an interrupt fires It takes two!! stack will equal 7
The software stack is the one you really don't need to know about just yet!!
I think you will find that all C compilers for the 16 series pic avoid the use of the stack as much as possible and they would definitely not use it for a do ... while loop.Whilst the stack may be used on PCs and the like for this kind of thing, it isn't on pics. You may program in C but you obviously haven't looked at the compiled code.
I ditched the pic16f877a for that reason, I moved to the pic18f4520 with a 256 level hardware stack.. I suppose the type of compiler is ass an issue, first c compiler I used was SDCC open sourced (a bit buggy), I now use C18 and C24.
Anyway this is definitely a pit fall in SOME compilers.
I think you will find that all C compilers for the 16 series pic avoid the use of the stack as much as possible and they would definitely not use it for a do ... while loop.Whilst the stack may be used on PCs and the like for this kind of thing, it isn't on pics. You may program in C but you obviously haven't looked at the compiled code.
Sorry I stand corrected!! 31 level hardware stack, its the software stack that is 256 by default, I was under the impression that the software stack was for function arguments and local variables only!