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
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.
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 :)