Thanks to Laurie Tunnicliffe for the following notes re these problems.
You probably know this already but just in case... the bug with Pascal51 returning wrong function values of type word/integer can be fixed as follows......
The compiler allocates 2 bytes for the function value in the stack frame It moves the value to A/B registers on return.... So at the end of any function you will have
MOV R0, SP ;Sp now points to the
High byte of the return value
MOV B, @R0 ;and the value is moved to B
INC R0 ; ***THIS IS THE ERROR AND SHOULD BE... DEC R0
MOV 01H, @R0 ;move the low byte to R1 for safe keeping
MOV A, SP ;Now reset stack pointer
ADD A,#0FBH ;to remove locals and
MOV SP, A ;the return value from the stack frame
MOV A, R1 ;get the Low byte in A
RET ;and return
This maybe of help to others that may ask you, although you may know all of
this anyway.
Bug in the DIV code with mixed
types e.g.
var
x : byte; y : word;
begin
x := 10; y := 500 DIV Word(x); gives wrong value
end.
Also, an issue with enumerated types
;program test;
;type
; Enumerate = (one, two, three);
;var
; x at 8 : internal array[Enumerate] of byte;
;
;begin
test: MOV SP,#42H
MOV P2,#0
MOV PSW,#0
MOV 2CH,#0
; x[one] := 1;
MOV 00H,#01H
; x[two] := 2;
MOV 01H,#02H ;
; x[three] := 3;
MOV 02H,#03H
;end.
Another one concerning functions as parameters
{$C+}
program test;
var
b at 8 : internal Byte;
function Func1(b : Byte) : Byte;
begin
Func1 := 2*b;
end;
function Func2(b1, b2 : Byte) : Byte;
begin
Func2 := b1 + b2;
end;
procedure ShowBug(b : Byte);
var
x : Byte;
begin
{ the bug occurs here due to the incorrect calculation of the address
of "b" for the second Function call. The stack pointer has been
incremented by 1, due to pushing the result of the first function call }
x := Func2( Func1(b), Func1(b) );
end;
begin
b := 10;
ShowBug(b);
end.
(*----------------------------------------------------------------------*)
Pas51_Rel_Part SEGMENT CODE
RSEG Pas51_Rel_Part
;program test;
;var
; b at 8 : internal Byte;
;
;function Func1(b : Byte) : Byte;
;begin
func1:
Line___7:
MOV A,SP
ADD A,#01H
MOV SP,A
; Func1 := 2*b;
Line___8:
MOV A,SP
ADD A,#0FDH
MOV R0,A
MOV A,@R0
MOV B,#02H
MUL AB
MOV R0,SP
MOV @R0,A
;end;
P51v001_2b_b DATA 0FDH
P51B001 CODE func1
Line___9:
MOV R0,SP
MOV 01H,@R0
MOV A,SP
ADD A,#0FFH
MOV SP,A
POP DPH
POP DPL
MOV A,SP
ADD A,#0FFH
MOV SP,A
PUSH DPL
PUSH DPH
MOV A,R1
RET
;
;function Func2(b1, b2 : Byte) : Byte;
;begin
func2:
Line___12:
MOV A,SP
ADD A,#01H
MOV SP,A
; Func2 := b1 + b2;
Line___13:
MOV A,SP
ADD A,#0FDH
MOV R0,A
MOV A,SP
ADD A,#0FCH
MOV R1,A
MOV A,@R1
ADD A,@R0
MOV R0,SP
MOV @R0,A
;end;
P51v002_2b_b1 DATA 0FCH
P51v002_2b_b2 DATA 0FDH
P51B002 CODE func2
Line___14:
MOV R0,SP
MOV 01H,@R0
MOV A,SP
ADD A,#0FFH
MOV SP,A
POP DPH
POP DPL
MOV A,SP
ADD A,#0FEH
MOV SP,A
PUSH DPL
PUSH DPH
MOV A,R1
RET
;
;procedure ShowBug(b : Byte);
;var
; x : Byte;
;begin
showbug:
Line___19:
MOV A,SP
ADD A,#01H
MOV SP,A
; x := Func2( Func1(b), Func1(b) );
Line___20:
MOV A,SP ; get the stack pointer and calc
ADD A,#0FDH ; negative offset for "b"
MOV R0,A
MOV A,@R0
PUSH ACC ; load it for the Func1 call
LCALL func1 ; after this call the stack is as it was
PUSH ACC ; load result onto stack as parameter for Func2
MOV A,SP ; get the stack pointer and now
ADD A,#0FDH ; negative offset for "b" is now wrong should be #0FCH
MOV R0,A
MOV A,@R0
PUSH ACC
LCALL func1
PUSH ACC
LCALL func2
MOV R0,SP
MOV @R0,A
;end;
P51v003_2b_b DATA 0FDH
P51v003_2b_x DATA 00H
P51B003 CODE showbug
Line___21:
MOV A,SP
ADD A,#0FFH
MOV SP,A
POP DPH
POP DPL
MOV A,SP
ADD A,#0FFH
MOV SP,A
PUSH DPL
PUSH DPH
RET
;
;
;begin
test:
Line___24:
MOV SP,#42H
MOV P2,#0
MOV PSW,#0
MOV 2CH,#0
; b := 10;
Line___25:
MOV 08H,#0AH
; ShowBug(b);
Line___26:
MOV A,08H
PUSH ACC
LCALL showbug
;end.
P51v000_0b_RT_PSW DATA 2CH
P51v000_0b_b DATA 08H
P51B000 CODE test
Line___27:
NOP
;---------------------------------------------
; ----- End Of Pascal51 Relocate Segment -----
;---------------------------------------------
CSEG At 00H
ORG 0
LJMP test
LJMP NOISR
ORG 0BH
LJMP NOISR
ORG 13H
LJMP NOISR
ORG 1BH
LJMP NOISR
ORG 23H
LJMP NOISR
NOISR: RETI
ORG 027H
END