2019-09-27 20:57:34 +00:00
|
|
|
*``````````````````````````````*
|
|
|
|
* HEX2INTASC (NATHAN RIGGS) *
|
|
|
|
* *
|
|
|
|
* CONVERT A SIGNED HEXADECIMAL *
|
|
|
|
* VALUE TO AN INTEGER STRING. *
|
|
|
|
* *
|
|
|
|
* INPUT: *
|
|
|
|
* *
|
2021-06-06 01:40:51 +00:00
|
|
|
* ZPW1 = HEX TO CONVERT *
|
2019-09-27 20:57:34 +00:00
|
|
|
* *
|
2021-06-04 02:58:21 +00:00
|
|
|
* DESTROYS: NZCIDV *
|
|
|
|
* ^^^ ^ *
|
2019-09-27 20:57:34 +00:00
|
|
|
* *
|
2021-06-04 02:58:21 +00:00
|
|
|
* CYCLES: 268+ *
|
|
|
|
* SIZE: 157 *
|
2019-09-27 20:57:34 +00:00
|
|
|
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
|
|
|
|
*
|
2021-06-06 01:40:51 +00:00
|
|
|
]NGFLAG EQU ZPW2 ; NEGATIVE FLAG
|
|
|
|
]VALSTR EQU ZPW1 ; HEXADECIMAL TO CONVERT
|
|
|
|
]MOD10 EQU ZPW3 ; VALUE MODULUS 10
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
HEX2INTASC
|
|
|
|
*
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA ]VALSTR+1 ; {4C3B} STORE VALUE HIGH BYTE
|
|
|
|
STA ]NGFLAG ; {4C3B} IN THE NEGATIVE FLAG
|
|
|
|
BPL :GETBP ; {3C2B} IF VALUE IS POSITIVE, BRANCH
|
|
|
|
LDA #0 ; {3C2B} ELSE SUBTRACT LOW BYTE
|
|
|
|
SEC ; {2C1B}
|
|
|
|
SBC ]VALSTR ; {4C3B}
|
|
|
|
STA ]VALSTR ; {4C3B} STORE AS NEW LOW BYTE
|
|
|
|
LDA #0 ; {3C2B} ADJUST HIGH BYTE
|
|
|
|
SBC ]VALSTR+1 ; {4C3B}
|
|
|
|
STA ]VALSTR+1 ; {4C3B}
|
2019-09-27 20:57:34 +00:00
|
|
|
:GETBP
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA #0 ; {3C2B} SET BUFFER TO EMPTY
|
|
|
|
LDY #0 ; {3C2B}
|
|
|
|
STA RETLEN,Y ; {5C2B} BUFFER(0) = 0
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
:CNVERT ; CONVERT VALUE TO STRING
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA #0 ; {3C2B} RESET MODULUS
|
|
|
|
STA ]MOD10 ; {4C3B}
|
|
|
|
STA ]MOD10+1 ; {4C3B}
|
|
|
|
LDX #16 ; {4C3B}
|
|
|
|
CLC ; {2C1B} CLEAR CARRY
|
2019-09-27 20:57:34 +00:00
|
|
|
:DVLOOP
|
2021-06-04 02:58:21 +00:00
|
|
|
ROL ]VALSTR ; {7C3B} SHIFT CARRY INTO DIVBIT 0
|
|
|
|
ROL ]VALSTR+1 ; {7C3B} WHICH WILL BE THE QUOTIENT
|
|
|
|
ROL ]MOD10 ; {7C3B} + SHIFT DIV AT SAME TIME
|
|
|
|
ROL ]MOD10+1 ; {7C3B}
|
|
|
|
SEC ; {2C1B} SET CARRY
|
|
|
|
LDA ]MOD10 ; {4C3B} SUBTRACT #10 (DECIMAL) FROM
|
|
|
|
SBC #10 ; {4C3B} MODULUS 10
|
|
|
|
TAY ; {2C1B} SAVE LOW BYTE IN .Y
|
|
|
|
LDA ]MOD10+1 ; {4C3B} ADJUST HIGHBYTE
|
|
|
|
SBC #0 ; {4C3B} SUBTRACT CARRY
|
|
|
|
BCC :DECCNT ; {3C2B} IF DIVIDEND < DIVISOR, DEC CTR
|
|
|
|
STY ]MOD10 ; {4C3B} ELSE STORE RESULT IN MODULUS
|
|
|
|
STA ]MOD10+1 ; {4C3B} NEXT BIT OF QUOTIENT IS A 1,
|
2019-09-27 20:57:34 +00:00
|
|
|
; DIVIDEND = DIVIDEND - DIVISOR
|
|
|
|
:DECCNT
|
2021-06-04 02:58:21 +00:00
|
|
|
DEX ; {2C1B} DECREASE .X COUNTER
|
|
|
|
BNE :DVLOOP ; {3C2B} IF NOT 0, CONTINUE DIVIDING
|
|
|
|
ROL ]VALSTR ; {7C3B} ELSE SHIFT IN LAST CARRY
|
|
|
|
ROL ]VALSTR+1 ; {7C3B} FOR QUOTIENT
|
2019-09-27 20:57:34 +00:00
|
|
|
:CONCH
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA ]MOD10 ; {4C3B}
|
|
|
|
CLC ; {2C1B} CLEAR CARRY
|
|
|
|
ADC #$B0 ; {4C3B} ADD '0' CHARACTER TO VALUE
|
2019-09-27 20:57:34 +00:00
|
|
|
; TO GET ACTUAL ASCII CHARACTER
|
2021-06-04 02:58:21 +00:00
|
|
|
JSR :CONCAT ; {6C3B} CONCATENATE TO STRING
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
** IF VALUE <> 0 THEN CONTINUE
|
|
|
|
*
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA ]VALSTR ; {4C3B} IF VALUE STILL NOT 0,
|
|
|
|
ORA ]VALSTR+1 ; {7C3B} OR HIGH BIT, THEN KEEP DIVIDING
|
|
|
|
BNE :CNVERT ; {3C2B}
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
:EXIT
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA ]NGFLAG ; {4C3B} IF NEGATIVE FLAG IS SET
|
|
|
|
BPL :POS ; {3C2B} TO ZERO, THEN NO SIGN NEEDED
|
|
|
|
LDA #173 ; {3C2B} ELSE PREPEND THE STRING
|
|
|
|
JSR :CONCAT ; {6C3B} WITH A MINUS SIGN
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
:POS ; VALUE IS POSITIVE
|
2021-06-04 02:58:21 +00:00
|
|
|
RTS ; {6C1B} RETLEN
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
:CONCAT ; STRING CONCATENATION SUBROUTINE
|
2021-06-04 02:58:21 +00:00
|
|
|
PHA ; {3C1B} SAVE CHAR ON STACK
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
** MOVE BUFFER RIGHT ONE CHAR
|
|
|
|
*
|
2021-06-04 02:58:21 +00:00
|
|
|
LDY #0 ; {3C2B} RESET INDEX
|
|
|
|
LDA RETLEN,Y ; {5C3B} GET CURRENT STRING LENGTH
|
|
|
|
TAY ; {2C1B} CURRENT LENGTH IS NOW INDEX
|
|
|
|
BEQ :EXITMR ; {3C2B} IF LENGTH = 0, EXIT CONCAT
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
|
|
|
:MVELP
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA RETLEN,Y ; {5C3B} GET NEXT CHARACTER
|
|
|
|
INY ; {2C1B} INCREASE INDEX
|
|
|
|
STA RETLEN,Y ; {5C3B} STORE IT
|
|
|
|
DEY ; {2C1B} DECREASE INDEX BY 2
|
|
|
|
DEY ; {2C1B}
|
|
|
|
BNE :MVELP ; {3C2B} LOOP UNTIL INDEX IS 0
|
2019-09-27 20:57:34 +00:00
|
|
|
:EXITMR
|
2021-06-04 02:58:21 +00:00
|
|
|
PLA ; {3C1B} GET CHAR BACK FROM STACK
|
|
|
|
LDY #1 ; {4C3B}
|
|
|
|
STA RETLEN,Y ; {5C3B} STORE CHAR AS 1ST CHARACTER
|
|
|
|
LDY #0 ; {3C2B} RESET INDEX
|
|
|
|
LDA RETLEN,Y ; {5C3B} GET LENGTH BYTE
|
|
|
|
CLC ; {2C1B} CLEAR CARRY
|
|
|
|
ADC #1 ; {3C2B} INC LENGTH BY ONE
|
|
|
|
STA RETLEN,Y ; {5C3B} UPDATE LENGTH
|
2019-09-27 20:57:34 +00:00
|
|
|
*
|
2021-06-04 02:58:21 +00:00
|
|
|
LDA RETLEN ; {4C3B}
|
|
|
|
RTS ; {6C1B}
|