Hi,
The background to this thread is the programs that I wrote in response to this recent enquiry.
The OP wanted to sort the bytes stored in five consecutive variables, but it seemed to me that the "correct" method would be to write generic code, using values accessed via the byte pointer (@bptr). Also that it could be quite "elegant" and compact to use the post-increment/decrement facility (@bptrinc....). Two versions are shown in that thread and appeared to simulate correctly.
However, some further simulations seemed to acquire a spurious "0", only when sorting the first set of digits. This is not yet resolved, but I decided that the FOR bptr with subsequent bptrinc/decs might be "a step too far", so I returned to a simpler DO ... LOOP WHILE ... structure. Thus my "final" program became:
But when downloaded to a real chip, the program didn't run at all.
To cut a long story short(er), the syntax required by the real chips is IF @bptr < @bptrinc THEN . Note not just the post-increment in the "wrong" place (for strict left to right execution) but the change of > to < . To me, it looks as if it's the Compiler that is "incorrect" and the Simulator is more to specification/expectations.
Any comments (except that I should write simpler program code) ?
Cheers, Alan.
The background to this thread is the programs that I wrote in response to this recent enquiry.
The OP wanted to sort the bytes stored in five consecutive variables, but it seemed to me that the "correct" method would be to write generic code, using values accessed via the byte pointer (@bptr). Also that it could be quite "elegant" and compact to use the post-increment/decrement facility (@bptrinc....). Two versions are shown in that thread and appeared to simulate correctly.
However, some further simulations seemed to acquire a spurious "0", only when sorting the first set of digits. This is not yet resolved, but I decided that the FOR bptr with subsequent bptrinc/decs might be "a step too far", so I returned to a simpler DO ... LOOP WHILE ... structure. Thus my "final" program became:
Code:
#terminal 4800
#no_data
pause 2000 ; Give the terminal time to open
symbol FIRST = 2 ; Pointer to byte variables
symbol MIDDLE = 4
symbol LAST = 6
sertxd("Start",cr,lf) ; Confirm the program has started
do
bptr = FIRST
for bptr = FIRST to LAST ; Create and Report random list
random w4
@bptr = w4 // 10 ; Select the range for Random numbers
sertxd(#@bptr," ")
next
sertxd(cr,lf)
sort:
bptr = FIRST
do
if @bptrinc > @bptr then ; Exchange @bptr with @bptr+1
;// if @bptr < @bptrinc then ; This is the structure required for real chips //
@bptr = @bptrdec xor @bptr ; )
@bptr = @bptrinc xor @bptr ; } Swap without using an additional variable
@bptr = @bptrdec xor @bptr ; )
goto sort
endif
loop while bptr < LAST
for bptr = FIRST to LAST ; Report final list
sertxd(#@bptr," ")
next
bptr = MIDDLE
sertxd(cr,lf,"Middle Value is ",#@bptr,cr,lf)
loop
To cut a long story short(er), the syntax required by the real chips is IF @bptr < @bptrinc THEN . Note not just the post-increment in the "wrong" place (for strict left to right execution) but the change of > to < . To me, it looks as if it's the Compiler that is "incorrect" and the Simulator is more to specification/expectations.
Any comments (except that I should write simpler program code) ?
Cheers, Alan.