;=======================================================
; Utils.asm
;
; This is just a sample for learning to use the assembler.
;
; It contains a couple of routines that will be made
; PUBLIC so the main app can link against them.
;=======================================================

	.list		; This directive causes a list file to be generated

#ifdef M100					; Defined in Project->Settings->Assembler tab
#include	"m100.inc"		; We can put Model specific defines in here
#else
#error "Please define the Model and provide an include file"
#endif

;=======================================================
; Define PUBLIC symbols in other files that we want
; to share with other files.  The PUBLIC keyword makes
; these labels / routines accessible to other files.
;=======================================================

	public	SAMP_HELLO, SAMP_PSTRING
	
;=======================================================
; Make our routines relocatable since we don't really
; care where they execute from.
;=======================================================
	.cseg					; Relocatable code segment

;=======================================================
; Simple routine to print "Hello World"
;=======================================================
SAMP_HELLO:
	lxi  H,S_HELLO			; Load pointer to "Hello World" text
	call M_PRINT_HL			; Print the text
	ret

;=======================================================
; Print the text from a BASIC string
;
; Note:  This routine demonstrates the use of local
;        labels feature of the assembler.  The label &zl
;        below is only defined for the SAMP_PSTRING
;        function and goes out of scope at the next major
;        label.  This means local label names can be 
;        reused by other routines.
;=======================================================
SAMP_PSTRING:
	mov  A,M				; Get BASIC string len
	inx  H					; Point to address of string data
	mov  E,M				; Get LSB of address
	inx  H					; Point to MSB
	mov  H,M				; Get MSB of address
	mov  L,E				; Copy LSB to HL
	ora  A					; Test for zero length string
	jz   &zl				; Jump to local label &zl
	jmp  M_PRINT_HL			; Go print the basic string

	; Local label &zl (Zero Length)
&zl	lxi  H,S_EMPTY			; Load text that the sting is empty
	jmp  M_PRINT_HL			; Go print the text
	
;=======================================================
; Program strings.  These are part of code space
;=======================================================
SAMP_STRINGS	.cseg		; A Named Code segment

S_HELLO		db   "Hello World!", 0Dh, 0Ah, 0
S_EMPTY		db   "String is empty", 0Dh, 0Ah, 0
