Shop OBEX P1 Docs P2 Docs Learn Events
my call rutine of getting single byte from cog ram long — Parallax Forums

my call rutine of getting single byte from cog ram long

tonyp12tonyp12 Posts: 1,951
edited 2011-07-01 13:30 in Propeller 1
Most of the time, I simple use longs even if the data always are less than the value of 256.
But maybe you try to save space and sometimes you are simple forced to have the data as bytes.

So I come up with this routine, you set the byte you want in bytepnt before you make the call.
Then it comes back with the answer in mybyte.
It does not trash bytepnt, so it can used as a global variable in your code that you add/sub to etc.

I have not tested it, but it should work.
Any shorter routines you Prop Heads have come up with?
main            mov bytepnt,#3            'just an example
                call #getbyte
                movs dosomething,mybyte   'just an example  
                ...
                ...
                jmp #main

getbyte         mov bytepntbuf, bytepnt   'don't trash bytepnt
                shr bytepntbuf,#2         'divide by 4
                movs findbyte,#data       'start location of our byte table
                add findbyte, bytepntbuf  'add the divided-by-4 byte pointer
                
                mov bytepntbuf, bytepnt   'start with a fresh bytepointer
                and bytepntbuf,#%11       'keep only the 2 lower bits
                shl bytepntbuf,#3         'multiply by 8            

findbyte        mov mybyte,0-0
                shr mybyte,bytepntbuf     'find the right byte from a long
                and mybyte,#$ff           'keep only the lower 8 bits
getbyte_ret     ret                       'return, with answer in mybyte


data            byte 0,10,20,30
bytepnt         res 1
mybyte          res 1
bytepntbuf      res 1

Comments

  • tonyp12tonyp12 Posts: 1,951
    edited 2011-06-30 18:32
    I think I was able to shrink the code by two lines (maybe not worth it)
    by using ror as movs only transfer the lower 9bits.
    getbyte         mov bytepntbuf, bytepnt   'don't trash bytepnt
                    ror bytepntbuf,#2         'divide by 4 but roll lower two bits to top.
                    movs findbyte,bytepntbuf  'initiate the start location
                    add findbyte,#data        'add the start location of our byte table
                    shr bytepntbuf,#28        'shift the upper two bits down (edit: use 27 if Ariba is right)
                    ... the rest is the same from findbyte above.
    
  • AribaAriba Posts: 2,690
    edited 2011-06-30 18:37
    You was a bit faster, but only because I have tested it in PASD...

    The shr must be 27 not 28 (30-3)
    main            mov bytepnt,#3            'just an example
                    call #getbyte
                    movs dosomething,mybyte   'just an example  
                    ...
                    ...
                    jmp main
    
    getbyte         mov bytepntbuf, bytepnt   'don't trash bytepnt
                    ror bytepntbuf,#2         'divide by 4
                    movs findbyte,bytepntbuf  'write lower 9 bits as source address
                    add findbyte, #data       'add start location of our byte table
                    
                    shr bytepntbuf,#30-3      'multiply 2 lower bits by 8            
    
    findbyte        mov mybyte,0-0
                    shr mybyte,bytepntbuf     'find the right byte from a long
                    and mybyte,#$ff           'keep only the lower 8 bits
    getbyte_ret     ret                       'return, with answer in mybyte
    
    
    data            byte 0,10,20,30
    bytepnt         res 1
    mybyte          res 1
    bytepntbuf      res 1
    

    Andy
  • Heater.Heater. Posts: 21,230
    edited 2011-07-01 07:31
    Isn't this pointless? I mean any program that uses this has to set up bytepnt and make a call for every byte it accesses. Doesn't that end up costing more space than is saved?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-01 08:16
    It saves space if the byte table is larger than about 12 bytes. At 12 bytes the table will use 3 longs instead of 12 longs.
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-07-01 13:30
    Heater, of course you would only use this rutine inside a loop were it's used often.
    So the long used up by op-code 'call' would not cancel out the space saving.

    And that your table is at least 12 bytes long.

    That I can add 1 or 2 to the bytenct as my code checks for specific char etc is useful and maybe then sub 1.
    Without thinking about byte boundery in a long.

    Compared to this cludge I used first, were I loop the shifter from 0-to24 and reset if it hit 32 and add 1 to longcnt
    Does not easy allow to look forward two bytes and then back up one.
            if_nz add       byteshft,#8                     'if not a zero, prepare next char for next tim          
                  cmp       byteshft,#32 wz
            if_z  add       loop1,#1
            if_z  add       testdot,#1
            if_z  mov       byteshft,#0 
    
Sign In or Register to comment.