FormatCopyAndPrint

Purpose: Copy an ASCIIz string in SRAM to an SRAM location and/or output that same string to the COM1 serial buffer.
Assumes:  There is sufficient buffer space if copying is enabled.
The routine will block if insufficient room in the print buffer.
Passed: R25:R24  BYTE *   bPtrDest   starting SRAM address at which to copy the string; < 0x300 skips copying
R23:R22 BYTE *   bPtrSource   starting SRAM address from which to copy and/or print
R20 BYTE bFlags in which, only option BIT BIT_FMT_PRINT is checked
Returns: R25:R24 BYTE * bPtrDest the SRAM copy destination address, unchanged
Alters: R20, R21, R26, R27, the FLAGs, and the caller's copy buffer and/or print buffer, as specified
Example:  
.ORG  0x1000
ADestBuffer = 0x06E0

   ADIW    R26, 0                  ; If 'X' is zero, the command line tail is
   BREQ    ALocal_Ret              ;  empty, so just exit

   LDI     R20, 0                  ; Assume the string will NOT be printed and
   ADIW    R26, 1                  ;  point to the character after the space
   MOVW    R24, R26                ; Example command "@ 0x1000 ~Skip this"
   LD      R22, X+                 ; If the string starts with the tilde, the
   CPI     R22, '~'                ;  assumption is correct, so skip over the
   BREQ    AContinueSetup          ;  instructions which turn printing on
   LDI     R20, 1 << BIT_FMT_PRINT ; Set the bit in bFlags to also print it
   LD      R22, -X                 ; Back up to print the first non tilde BYTE

ContinueSetup:
   MOV     R30, R20                ; Save a copy of bFlags with BIT_FMT_PRINT
   LDI     R25, ADestBuffer >> 8   ; Point R25:R24 at the destination buffer
   LDI     R24, ADestBuffer & 0xFF
   MOVW    R22, R26                ; The source buffer is the 'X' pointer
   CALL    AFormatCopyAndPrint
   CPSE    R30, R1                 ; If the string was not printed, no need
   CALL    APrintNewLine           ;  to print an additional line of CR, LF

Local_Ret:
   RET
Test it:
m>DS+ 0x6E0 48
SRAM contents:
06E0:  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
06F0:  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
0700:  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
m>@ 0x1000 Print and copy this line.
Print and copy this line.
m>DS+- 0x6E0 48
06E0:  50 72 69 6E 74 20 61 6E-64 20 63 6F 70 79 20 74  Print and copy t
06F0:  68 69 73 20 6C 69 6E 65-2E 00 00 00 00 00 00 00  his line........
0700:  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
m>@ 0x1000 ~Copy but do not print this one.
m>DS+- 0x6E0 48
06E0:  43 6F 70 79 20 62 75 74-20 64 6F 20 6E 6F 74 20  Copy but do not
06F0:  70 72 69 6E 74 20 74 68-69 73 20 6F 6E 65 2E 00  print this one..
0700:  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................
m>@ 0x1000  ~ Note the tilde is over 1 place.
 ~ Note the tilde is over 1 place.
m>DS+- 0x6E0 48
06E0:  20 7E 20 4E 6F 74 65 20-74 68 65 20 74 69 6C 64   ~ Note the tild
06F0:  65 20 69 73 20 6F 76 65-72 20 31 20 70 6C 61 63  e is over 1 plac
0700:  65 2E 00 00 00 00 00 00-00 00 00 00 00 00 00 00  e...............
m>                                                                              
Notes: This routine uses the strcpy and PrintSRAMASCIIz System Functions.
Any destination buffer address less than 0x300 will not be copied, but that the print request is unaffected by that.
The BIT_FMT_PRINT bit causes the string to be transferred into the COM1 serial port buffer.
Dropin: 
(setup
code)
;   Setup for the FormatCopyAndPrint() call:
; Passed:  R25:R24 BYTE * bPtrTo pointer to the SRAM copy target buffer or NULL             
;          R23:R22 BYTE * bPtrFrom pointer to the SRAM ASCIIz string to copy
;              R20 BYTE   bFlags, of which only BIT_FMT_PRINT is used
; Returns: R25:R24 BYTE * bPtrTo, same as the R25:R24 passed
; Alters:  R20, R21, R26, R27, the FLAGs, and the buffer(s) selected
Also see:  The FormatULONGHex, FormatULONG, and FormatCopyAndPrint System Functions, each of which call this one.