Hi can someone please help with the explanation of this code below, more importantly how does the uC 'see' the Sqrs: line at the end of the code
I am interested in something similar in a project i am currently doing whereby the input data of a PORT is compared to certain values and the the result stored in a register.
What function does PC have in the code?
org 0 ljmp Main
Main:
mov P3, #0xFF ; Port 3 is an input
loop: mov a, P3
anl a, #0x0F ; Clear bits 7..4 of A
lcall sqrt
mov P1, a
sjmp loop
sqrt: inc a
movc a, @a + PC
ret
Sqrs: db 0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3
Hi can someone please help with the explanation of this code below, more importantly how does the uC 'see' the Sqrs: line at the end of the code<p>
Why shouldn't it "see" it? The processor can "see" anything that is in accessible memory areas.
If the processor is instructed to jump to the start of the sqrs table, it will see the table as a number of instructions - see the instruction sheat to find out what instructions - that will most probably make the processor do very unexpected things.
But in this case, the array of bytes are never used as instructions. They are instead used with the movc a, @a + PC instruction, that will pick up the [PC+a] element from the array. Note that there is a single ret instruction between the movc instruction and the start of the array. That is the reason why the program needs to add an inc a before making the lookup. The lookup will fail - or at least pick up values from an incorrect location - if you happen to add other instructions between movc and ret, or between ret and the sqrs table.
Actually, the processor never "sees" any source code - all it ever sees is the content of memory that results from that source code.
The so-called "bible" tells you how the processor will react to the contents of memory;
The Manuals for your assembler tell you how it will interpret the source code:
http://www.keil.com/support/man/docs/a51/
thanks everyone
i've seemed to grasp is now after a good bit of simulation and debugging:)
"i've seemed to grasp is now after a good bit of simulation and debugging"
You really need to study those documents to lay a proper Foundation - otherwise (to use a truly Biblical metaphor) you are building on sand...
My fingers (or whatever part of the anatomy that is involved) seems to play tricks on me.
sheat -> sheet - i wasn't talking about the fish :)