Shop OBEX P1 Docs P2 Docs Learn Events
IREAD counter question — Parallax Forums

IREAD counter question

jgjonolajgjonola Posts: 27
edited 2005-08-20 23:05 in General Discussion
Hello from las vegas again everyone,
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

  • BeanBean Posts: 8,129
    edited 2005-08-19 00:03
    John,
    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"
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-19 12:09
    John, I think I see your problem. The instruction following snc is a compound statement (is compiled into multiple instructions), you cant follow a conditional skip with a compound instruction. Also there is some inefficiency in your code. Try the following:
     
    ;Initialization code
      mov   mptrlo, #tmsg & 255  ;seed memory pointer to begining of text
      mov   mptrhi, #(tmsg >> 8) 
     
    :DO  
      mov   m, mptrhi         ;move top 4 bits into the M register
      mov   w, mptrlo         ;move bottom 8 bits into W
    
      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
      INC   mptrlo            ;update memory pointer
      SNC
        INC mptrhi
      JMP   @:DO 
     
     
    
     
     
    

    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:
    ;define this so that the first instruction is in the first half of a memory page tmsg
        jmp  pc+w
        retw 'H'
        retw 'e'
        retw 'l'
        retw 'l'
        retw 'o'
        retw  0
     
     
    ;then in your code Initialization
        mov idx, #1        ;start index at 1 a 0 causes an infinite loop in the jump table
     
    ;then in your program body
    :DO
        mov  w, idx        ;load index into w
        call @tmsg         ;get idx'th character
        mov  char, w       ;store character
        jz   :endoftext    ;end of text
        call @LCD_OUT
        inc  idx
        JMP   @:DO 
    :endoftext
    

    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
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-08-19 20:57
    John,

    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:

    :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
    



    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-08-19 21:13
    The mov M,#10 takes 1 word, and does not affect W.

    regards peter
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-08-19 21:26
    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
  • jgjonolajgjonola Posts: 27
    edited 2005-08-20 06:46
    Guenther and everyone...
    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.
    Main:  
      bank    COUNTS
      MOV     idx,#0  
      MOV     idx+1,#0
      mov     char,#LcdCls       ;clear the screen
      call    LCD_OUT  
    Label_Message:
      mov     idx,  #tmsg & 255
      mov     idx+1,#tmsg >> 8
      
    :DO
      MOV M,idx+1                ;move top 4 bits of tmsg into the m register
      mov W,idx                  ;move bottom 8 bits into the W register
      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
      snc
      inc idx+1
      JMP @:DO
    

    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]
  • BeanBean Posts: 8,129
    edited 2005-08-20 15:28
    John,
    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"
    ·
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-08-20 16:45
    John,

    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
  • jgjonolajgjonola Posts: 27
    edited 2005-08-20 23:05
    thanks again guys.

    John Gjonola
Sign In or Register to comment.