IREAD counter question
jgjonola
Posts: 27
Hello from las vegas again everyone,
I have a small problem with using iread in the code below:
If i am using a large amount of data, and idx rolls over 255 to 0 then it starts at the top of the page that the data is on.· I could make idx 16 bits long, but i already have m...· How would that effect it?· I've tried so many things, and just cant seem to get it!
John Gjonola
I have a small problem with using iread in the code below:
:DO MOV M,#tmsg >> 8 ;move top 4 bits of tmsg into the m register mov W,#tmsg & 255 ;move bottom 8 bits into the W register add W, idx ;add the index to the address to get the position snc ;skip adding 1 to the top 4 bits if W didnt roll over to 0 mov M,#(tmsg >> 8) +1 ;problem child IREAD ;reads the char and pos w+m into W mov char, W ;moves the char at w into char CJE char,#0,@scroll ;done with the top line, do the bottom line now call @LCD_OUT ADD idx,#1 JMP @:DO
If i am using a large amount of data, and idx rolls over 255 to 0 then it starts at the top of the page that the data is on.· I could make idx 16 bits long, but i already have m...· How would that effect it?· I've tried so many things, and just cant seem to get it!
John Gjonola
Comments
Of course when idx rolls over it start at zero again. That has nothing to do with IREAD.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"One experiment is worth a thousand theories"
·
But I notice you are not using the top 4 bits stored at the memory location. When you only need 8 bits of data from the program memory area, don't use IREAD, use a jump table as such:
this method is much faster and can handle strings upto 254 characters.
<edit> I just realized something, there is no return from page with constant in W. So the table needs to be in the same page as the "call tmsg", so eliminate the @ in that function call and place the jump table in the same page. If you absolutely need the table on another page and none of the strings are more than 127 characters long, I can show you a method of constructing the jump table that can be on a different page</edit>
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Post Edited (Paul Baker) : 8/19/2005 12:57:22 PM GMT
as Paul mentioned, follwing a skip instruction by a compound statement is a real killer! The latest version of the SX-Key IDE should give you a warning though. Besides this, your original code contains another "killer". By no means do I want to offend you - I made the same mistakes more than once but I think this is a "nice" example, helping the SX fellow programmers avoiding such bugs. Here is your original code again:
In the second and third lines, you prepare W to hold the lower eight bits for the IREAD. In the fifth line, there is the compound
mov M, #(tmsg >> 8) + 1
instruction that will be replaced by SASM as follows:
mov W, #(tmsg >> 8) + 1
mov M, W
Assuming that the SNC instruction would not be executed as this would lead to chaos anyway, it means that the original contents of W (the lower 8 bits for the IREAD) are overwriten here, as the W register is used as temporary storage for the compound instruction.
IOW: As almost all compound instructions make use of the W register for temporary storage, any initialization of W done before executing a compound statement is most likely lost afterwards.
Sometimes, you may make use of the fact that W is used as temporary storage. For example:
mov Foo1, #5
mov Foo2, #5
is expanded into
mov W, #5
mov Foo1, W
mov W, #5
mov Foo2, W
As you can see, W is initialized to #5 twice, so you could write
mov Foo1, #5
mov Foo2, W
instead which expands into
mov W, #5
mov Foo1, W
mov Foo2, W
Nevertheless, I can't recommend such "tricky" constructs at all. Imagine the following context:
mov W, #1
mov Foo1, #5
mov Foo2, W
Thius might make someone believe that Foo2 receives #1 which is definitely not the case - Foo2 will be 5 (see above).
Therefore, I highly recommend not to use compound statements at all in such context. Replacing the code by
mov W, #1
mov W, #5
mov Foo1, W
mov Foo2, W
makes it very obvious that the first mov W,#1 has no meaning at all.
Happy programming!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
regards peter
Arghhhh - you are absolutely right. Maybe, I should look at the docs before posting "tutorials" - please beat me for that.
John,
To make it clear
mov M, #(tmsg >> 8) + 1
is NOT replaced by two instructions - the
mov M, #literal
and
mov M, W
instructions are both single-word instructions. I messed that up with
MOV M, fr
which is, in fact, a compound instruction using W as temporary storage that is expanded to
MOW W, fr
MOW M, W
Nevertheless, I still stand to the remainder of my "quick tutorial" - sorry for that hazzle.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
I would in NO WAY be offended by programing tips.· I am very new to this, and any help is greatly appreciated!· I was about to reply to your first message Guenther that i had copied those couple lines from the compiled sx/b code, so ...· well anyways· [noparse]:)[/noparse]· I see you have all cleared up the mistakes I was making.· I was not using a 16 bit index...· I had tried the 16 bit index but then i was copying it wrong.· Anyways here is what i came up with and it seems to work.
Paul,
I was thinking about using a jump table, but this is going to be using more than 300 characters (eventually, i will just use an eeprom).· And thanks for the pointers, they helped alot.
Bean,
Yea, you are right, duh on my part.· I think i was just getting frustrated.· (at least that's my story)· Thanks!
One more question, I can't find an answer to in the manuals...· Does the line: [noparse][[/noparse]MOV idx+1, #tmsg >> 8] do 8 rotate rights?· I am assuming that from the outcome, but I am not sure.· I have your book Guenther, (and its the BEST) but I can't seem to find where something like that would be explained.· I have looked at the MOV instruction and the >> but none seem to fit.
Thanks all you SX god-like people,
John Gjonola [noparse]:)[/noparse]
The value of "tmsg >> 8" is a constant that is evaluated when your code is assembled.
Just like "idx+1".
Let's assume that idx = 8 and tmsg = $123. The assembler would convert
"MOV idx+1,#tmsg >> 8" into "MOV 9,#1"
So you see the SX is not performing any math at all.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
"One experiment is worth a thousand theories"
·
you can find more information about the >> and other modifiers in the FAQ text that I publish here in the Forum. See the "sticky" topic at the very beginning of this SX sextion.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
John Gjonola