Shop OBEX P1 Docs P2 Docs Learn Events
Using test as a condition in assembly — Parallax Forums

Using test as a condition in assembly

UnsoundcodeUnsoundcode Posts: 1,532
edited 2011-03-19 17:16 in Propeller 1
Hi , I have an assembly loop that reads byte values. What I want to do is read two of those values as a word value at a certain point in the loop. After reading the word value I then want to adjust the loop counter to the word value minus the byte values I have already read.

For example if initially the loop counter = 1000 and a word value of 650 is contained in bytes 8 and 9 I want to adjust the loop counter from 1000 to 650-9 (641). Will the following code work , low byte is always read first.

:loop

          	read data here

  	test  loop_counter,#8
      if_nc   mov lowbyte,data
          	test  loop_counter,#9
      if_nc   jmp getcount               
  	djnz    loop_counter,#:loop  





getcount
                        mov highbyte,data
                        shl highbyte,#8
                        and highbyte,lowbyte
                        mov loop_counter,highbyte
                        sub loop_count,#9
                       
getcount_ret                ret


It appears to work but I am not absolutely positive , because of the stage I am at I have no feedback as to whether it will be right in all cases.

Jeff T.

Comments

  • tonyp12tonyp12 Posts: 1,951
    edited 2011-03-18 21:09
    Even if you use the test opcode you still have to delclare that you want to write z (or c) result
    test loop_counter,#8 wz

    test does not test bytes, but whole 32bit longs.
    And you are testing if bit 4 (8) and bit 4 and 1 (9) = 1

    And also you have to use JMPRET for jump to subrutine (or pseudocode CALL)

    it's hard to follow what you really want it to do.

    but here is code that loops icounter from 1000 to x -9 (x any value below 1000 you set)
    'init endcounter'
    sub endcounter, #9
    mov icounter,#500
    shl icounter,#1 ' make it 1000
    loop;
    'read my data'
    sub icounter, #1
    cmp icounter,endcounter wz
    if_nz jmp #loop

    icounter res 1
    endcounter res 1
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-03-19 07:46
    tonyp12 , thank you so much for the constructive comments.

    The variables lowbyte , high byte , loop_counter and data are all longs .
    Data is read sequentially form zero to loop_counter and the bytes are read into the first eight bits of the long data variable and later retrieved with a mask.
    Longs 8 and 9 contain two bytes which together form the file length.
    My objective is to read exactly the file length yet start with an arbitary value in loop_counter , with this in mind I want to capture the file length on the fly and thus adjust the loop_counter.

    I have learnt by your example so its back to the drawing board so to speak

    Jeff T.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-03-19 12:25
    Ok taking on board what I have been told I see there were some glaring mistakes in my first attempt.

    Here is what I think I have addressed so far

    To reach my target of byte #8 and byte #9 I needed to be counting DOWN 8 iterations of the loop not waiting for the loop to reach byte #8 , way off.
    The getcount routine now OR's instead of AND's to combine the high and low bytes
    CMP over TEST was suggested simultaneously by two people so that has been included
    WZ and CALL also included

    To make the cmp I have initialized the loop counter at $8000 which is the expected maximum file length. The two compares compare the loop counter against two constant values , $7FF8 and $7FF7 , which should be the 8th and 9th bytes respectively
    :loop
    	read data here
    
    	cmp loop_counter,first_byte  wz
    if_z   	mov low_byte,data
    	cmp  loop_counter,second_byte  wz
    if_z   	call #getcount               
    	djnz    loop_counter,#:loop   
    
    getcount
                            mov high_byte,data
                            shl high_byte,#8
                            or high_byte,low_byte
                            mov loop_counter,high_byte
                            sub loop_counter,#9
                           
    getcount_ret                ret
    
    first_byte	       long	  $7FF8
    second_byte   long	  $7FF7	
    loop_counter   long   $8000
    

    Is this looking any better?

    Thanks
    Jeff T.
  • kuronekokuroneko Posts: 3,623
    edited 2011-03-19 16:33
    The two compares compare the loop counter against two constant values , $7FF8 and $7FF7 , which should be the 8th and 9th bytes respectively ...
    This will work (tested). Minor niggle, $7FF8 and $7FF7 represent the 9th and 10th byte respectively (zero based indices 8 and 9).
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-03-19 17:16
    kuroneko , excellent and for it to be verified through testing is a valuable bonus.

    For me it was a fun learning experience even though it appears to be a fairly simple exercise.

    I think I have the constants in the compare right , when I said byte 8 and 9 I was using the address values $0008 and $0009 which are in fact the 9th and 10th bytes sorry I was a little vague with that.

    Thank you
    Jeff T.
Sign In or Register to comment.