PrintWORD

Purpose: Print a string, in decimal format with the numeric value of a WORD.
Assumes:   Leading zeros should be suppressed.
Passed: R25:R24   WORD   wValue   the WORD value to print; output: "0" to "65535" with comma and/or a space after it
R22 BYTE bFlags option BITs BIT_FMT_COMMAS and BIT_FMT_POST_Sp.
Returns: R23:R22 WORD bValue   the same bValue WORD that was passed in R25:R24
Alters: R18, R20 through R25, the FLAGs (SREG) and the COM1 Transmit buffer
Notes: Uses the FormatWORD System Function
 

PrintHexWORD

Purpose: Print a string, in hexadecimal format with the numeric value of a WORD.
Assumes:   Do not suppress any leading zeros; print four (4) characters
Passed: R25:R24   WORD   wValue   the WORD value to print; output: "0000" to "FFFF"
Returns: Nothing.
Alters: R24, the FLAGs (SREG) and the COM1 Transmit buffer
 

Print0xHexWORD

Purpose: Print the 2-BYTE string "0x" then the 4-BYTE string created by PrintHexWORD.
Notes: Everything else is as documented in PrintHexWORD above, since this routine uses it.

Example:  
.ORG  0x1000
PrintWORDDemo:
   LDI     R31, 0                 ; Initialize bFlags to "none"
   ADIW    R26, 0                 ; If there is no command tail, don't try to
   BREQ    ASetupForPrintWORD     ;  parse anything starting at SRAM address 0!

   LDI     R22, ','               ; Specify the character which will set the
   LDI     R21, 1 << BIT_FMT_COMMAS    ;  flag and the bFlag bitmask in R21
   RCALL   ACheckForFlag          ; Call the subroutine below to do it
   LDI     R22, '+'               ; Second option character: post-space (only
   LDI     R21, 1 << BIT_FMT_POST_Sp   ;  applies to the PrintWORD call
   RCALL   ACheckForFlag

   MOVW    R24, R26               ; Move the command tail pointer to R25:R24 in
   CALL    AParseValue            ;  order to parse any value found there
;  SBRS    R21, BIT_PV_VALID      ; Usually check validity; no need this time
   MOVW    R24, R22               ; Use parsed value's LSWord (or 0) to R25:R24

SetupForPrintWORD:
   MOV     R22, R31               ; Move the bFlags byte into place
   MOVW    R30, R24               ; Save the parsed LSWord or zero in R31:R30
   CALL    APrintWORD             ; Print R25:R24 in decimal format
   RCALL   APrintEqualString      ; Print the first " = "
   CALL    APrintHexWORD          ; Print R25:R24 in hexadecimal format
   RCALL   APrintEqualString      ; Print the second " = "
   CALL    APrint0xHexWORD        ; Print R25:R24 in standard hex notation
   JMP     APrintNewLine          ; Exit with a Carriage Return & Line Feed

CheckForFlag:
   MOVW    R24, R26               ; Point at the command tail and see if the
   CALL    Astrchr                ;  BYTE in R22 is in that string
   OR      R24, R25               ; R25:R24 will be NULL if it wasn't found
   CPSE    R24, R1                ; If R25:R24 OR'ed together > 0, it was found
   OR      R31, R21               ; Combine in the flag in R21 into R30
   RET

CSZ_SpaceEqualSpace = 0x3AD2      ; FLASH address of the string " = ", NULL

PrintEqualString:
   LDI     R25, CSZ_SpaceEqualSpace >> 8     ; Point to the string already
   LDI     R24, CSZ_SpaceEqualSpace & 0xFF   ;  in the FLASH memory
   LDI     R22, 3                 ; Since the string length is known, use it
   CALL    APrintFLASHString
   MOVW    R24, R30               ; Restore the parsed WORD before returning
   RET
Test it:
m>@ 0x1000
0 = 0000 = 0x0000
m>@ 0x1000 +
0  = 0000 = 0x0000
m>@ 0x1000 4096
4096 = 1000 = 0x1000
m>@ 0x1000 32760 ,
32,760 = 7FF8 = 0x7FF8
m>@ 0x1000   -1  +,
65,535  = FFFF = 0xFFFF
m>@ 0x1000  0x8765   ,+
34,661  = 8765 = 0x8765
m>@ 0x1000  0x3142  ,
12,610 = 3142 = 0x3142
m>                                                                                        
Notes: The Operating System handles the COM1 Transmit buffer as a circular FIFO (First In, First Out) queue.
The BIT_FMT_COMMAS bit causes a comma to delimit the string at the expected three-digit intervals.
The BIT_FMT_POST_Sp bit pads a space character after the last numeric digit.
Dropin: 
(setup
code)
;   Setup for the PrintWORD() call:
; Passed:  R25:R24 WORD wValue to print in decimal format
;              R22 BYTE bFlags (only BIT_FMT_COMMAS and BIT_FMT_POST_Sp)
; Returns: R23:R22 WORD wValue, the same as wValue passed in R25:R24
; Alters:  R18, R20 through R25, the FLAGs and the COM1 Transmit Buffer
; Notes:   Any leading zeros are suppressed (i.e., not printed)

;   Setup for the PrintHexWORD() or Print0xHexWORD() calls:
; Passed:  R25:R24 WORD wValue to print in hexadecimal format
; Returns: Nothing.
; Alters:  R24, the FLAGs and the COM1 Transmit Buffer
; Notes:   If there are any leading zeros, they are printed
Also see: The System Functions PrintBINARY, PrintCHAR, Print0xPrefix, and the PrintBYTE family.