(* program to demostrate adding lookup tables in the code segment *)

{$C+}
PROGRAM LookUpTable;
VAR
	i : internal Byte;

FUNCTION LookUp(X : Byte) : Byte;
BEGIN
	(* function rapper for the assembler code *)
END;


BEGIN
	i := LookUp(5);
END.

(* now the assembler listing with the necessary guts of the LookUp function *)

Pas51_Rel_Part	SEGMENT CODE
        RSEG Pas51_Rel_Part
;PROGRAM LookUp;
;VAR
;	i : internal Byte;
;
;FUNCTION LookUp(X : Byte) : Byte; (* multiplies by 10 *)
;BEGIN
lookup:
;------------- Normal code to allocate space for the return value --------
  MOV A,SP
	ADD	A,#01H
	MOV	SP,A
;--------------------------- Add the code here
	MOV A,SP												 ; subtract 3
	ADD A,#0FDH 										 ; from the stack pointer to get
	MOV R0,A												 ; the address of the parameter
	MOV A,@R0 											 ; X is now in the Accumulator
	ADD A,#02H											 ; add 2 to jump over the SJMP instruction
	MOVC A,@A+PC										 ; get the data into Acc
	SJMP EndData										 ; jump over the data
	dB 0,10,20,30,40,50,60,70,80,90  ; this is the table
EndData:
	MOV R0,SP 											 ; puts the result into the
	MOV @R0,A 											 ; address set aside for the return value

;END;
;---------------------- Normal function return code ---------------------
  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	 
;
;
;BEGIN
lookuptable:
	MOV	SP,#43H
	MOV	P2,#0
	MOV	PSW,#0
	MOV	2CH,#0
;	i := LookUp(5);
	MOV	R0,#05H
	PUSH	00H
	ACALL	lookup
	MOV	42H,A
;END.
	NOP	 
;---------------------------------------------
; ----- End Of Pascal51 Relocate Segment -----
;---------------------------------------------
           CSEG At 00H
        ORG 0      
				LJMP lookuptable
        AJMP NOISR 
        ORG 0BH    
        AJMP NOISR 
        ;------    
        ;--------- 
        ;------    
        ;--------- 
        ;------    
        ;--------- 
NOISR:  RETI       
        ;--------  
	END	 




		{$C+}
    (* Demonstrates how to add string constants to your program and assign
	     them to a string variable *)

    PROGRAM test;
    CONST
	    StringLength = 7;
    TYPE
	    Strings = (String1, String2, String3);
	    String	= array[0..StringLength - 1] of Byte;

    VAR
	    TheString at 16 : internal String;


		PROCEDURE StrCopy(Which : Strings; VAR Str : internal String);
    BEGIN
	    (* the strings and code are put in here in the assembly listing *)
    END;


    BEGIN
	    StrCopy(String2, TheString);
    END.


		 Pas51_Rel_Part  SEGMENT CODE
             RSEG Pas51_Rel_Part
     ;program test;
     ;const
     ;	StringLength = 7;
     ;type
     ;	Strings = (String1, String2, String3);
     ;	String	= array[0..StringLength - 1] of Byte;
     ;
     ;var
     ;	TheString at 16 : internal String;
     ;
     ;
     ;procedure StrCopy(Which : Strings; VAR Str : internal String);
     ;begin
     strcopy:
     ;
     ;----------------------- Add this code here ------------------------
	     MOV A,SP			; get stack pointer
	     ADD A,#0FDH 	; subtract 3 from it
	     MOV R1,A			; R1 points to "which"
	     MOV B,@R1 		; B contains the value of "which"    (B := R1^)
     ;
	     INC R1				; R1 now points to the Str parameter
			 MOV 00,@R1 	; R0 contains the address of the destination string (16)
     ;
	     MOV A,#7			; length of each string...in this case 7
			 MUL AB 			; String length X string number = offset of wanted string
     ;
	     MOV R2,#7 		; init the loop counter to the length of the strings
     Loop:
			 PUSH 0E0H		; save the Acc, base address for each time around the loop
	     ADD A,#9			; so we pass the next few instructions
	     MOVC A,@A+PC	; get the character
	     MOV @R0,A 		; and put it in the destination string
	     POP 0E0H			; get bach the base address in the string table
	     INC A 				; next source char
	     INC R0				; next destination char
	     DJNZ R2,Loop	; dec loop counter and jump back if not zero
	     SJMP EndData	; jump over string table
     ;
     ;------------------- Put Your Strings Here -----------------------
	     dB 'String1'
	     dB 'String2'
	     dB 'String3'
     EndData:

     ;end;
     ;------------------------ Now normal exit code --------------------
       POP DPH
	     POP	DPL
	     MOV	A,SP
	     ADD	A,#0FEH
	     MOV	SP,A
	     PUSH	DPL
	     PUSH	DPH
	     RET
     ;
     ;
     ;begin
     test:
	     MOV	SP,#42H
     ;  MOV P2,#0
     ;  MOV PSW,#0
     ;  MOV 2CH,#0
     ;	StrCopy(String2, TheString);
	     MOV R0,#01H 									 ; 1 is String2
	     PUSH	00H 										 ; push parameter
	     MOV A,#10H										 ; Address of "TheString"
	     PUSH	ACC 										 ; push VAR parameter
	     ACALL strcopy
     ;end.
	     NOP
     ;---------------------------------------------
     ; ----- End Of Pascal51 Relocate Segment -----
     ;---------------------------------------------
                CSEG At 00H
             ORG 0
             LJMP test
             AJMP NOISR
             ORG 0BH
             AJMP NOISR
             ;------
             ;---------
             ;------
             ;---------
             ;------
             ;---------
     NOISR:  RETI
             ;--------
	     END


