Using test as a condition in assembly
Unsoundcode
Posts: 1,532
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.
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.
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
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
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.
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
Is this looking any better?
Thanks
Jeff T.
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.