Tam Do - 2007-06-12

Hugh,

This is a continuation of the ReadAD10 error thread to which I originally posted. I discovered the cause of the so called word function bug.

To replicate the bug I wrote the following test codes and evaluated the corresponding .asm file outputs.

WORKING CODE

word function test_function
test_function = 9999
end function

sub test_function_t #nr
Dim test_var As Word
test_var = test_function
end sub

Main:
test_function_t
end

This gives the following output.

TEST_FUNCTION_T
    call    FN_TEST_FUNCTION
    movf    TEST_FUNCTION,W
    movwf    TEST_VAR
    movf    TEST_FUNCTION_H,W
    movwf    TEST_VAR_H
    return

Now for the case where the calls are inverted:

sub test_function_t #nr
Dim test_var As Word
test_var = test_function
end sub

word function test_function
test_function = 9999
end function

ASM Output:

TEST_FUNCTION_T
    call    FN_TEST_FUNCTION
    movf    TEST_FUNCTION,W
    movwf    TEST_VAR
    clrw
    movwf    TEST_VAR_H
    return

Now to test this furthermore I wrote the following code in the main method.

Main:
Dim test_var As Word
test_var = test_function
end

This also produced erroneous output.

MAIN
    call    FN_TEST_FUNCTION
    movf    TEST_FUNCTION,W
    movwf    TEST_VAR
    clrw
    movwf    TEST_VAR_H
    goto    BASPROGRAMEND
BASPROGRAMEND
    sleep
    goto    $

To trace the error in the BASIC file I looked for the sequence of asm calls and found this.

    IF NOT IsConst(S$) THEN
     DT$ = TypeOfVar$(S$, SourceSub$)
     'PRINT DT$, S$, SourceSub$
     IF DT$ = "WORD" THEN
      TempData$(CSC%) = " movf " + S$ + "_H,W"
      CSC% = CSC% + 1
      TempData$(CSC%) = " movwf " + V$ + "_H"
     END IF
     IF DT$ <> "WORD" THEN
      TempData$(CSC%) = " clrw"
      CSC% = CSC% + 1
      TempData$(CSC%) = " movwf " + V$ + "_H"
     END IF
    END IF

On lines 5262-5275 in the CompileVars subroutine.

I suspect that there is something wrong in the DataType detection, specifically in the order in which the files are parsed. This would explain the error with the ReadAD10 function since they are added programmatically at the end of the main file, therefore any subroutine would be before the call. I hope this helps.