0001 0020 org $20 0002 0020 05 output fcb 5 0003 0004 f800 org $f800 0005 0006 start 0007 f800 ce 00 00 ldx #0 * nice safe starting value 0008 f803 8e 00 ff lds #$ff * stack at top of ram 0009 0010 f806 ce 00 20 ldx #output * where the answer goes 0011 f809 cc 04 d2 ldd #1234 * what goes there 0012 f80c bd f8 2e jsr hex_word * make it happen 0013 f80f 7e f8 0f loop: jmp loop * and then wait in a safe place 0014 0015 * convert the low 4 bits of A to a single character which is 0016 * returned in the address pointed to by X. X is incremented. 0017 hex_nybble: 0018 f812 36 psha * cleanliness as a policy 0019 f813 84 0f anda #$0f * only the low nybble 0020 f815 80 0a suba #10 * check for 0-9 versus a-f 0021 f817 2d 02 blt hex_low 0022 f819 8b 27 adda #'a-'0-10 * note that 10-10+('a-'0-10)+('0+10) = 'a 0023 hex_low: 0024 f81b 8b 3a adda #'0+10 * fixes digits, finishes fixing a-f 0025 f81d a7 00 sta 0,x * store result 0026 f81f 08 inx * point to next character 0027 f820 32 pula * cleanliness again 0028 f821 39 rts 0029 0030 * convert the 8 bit number in A into the 2 character ASCII number. 0031 * the result is stored in memory pointed to by X which is incremented 0032 * to point past the result. 0033 hex_byte: 0034 f822 36 psha * save low nybble for later 0035 f823 44 lsra * get high nybble to print first 0036 f824 44 lsra 0037 f825 44 lsra 0038 f826 44 lsra 0039 f827 bd f8 12 jsr hex_nybble * and pump it out 0040 f82a 32 pula * get low nybble back 0041 f82b 7e f8 12 jmp hex_nybble * jmp = jsr, rts 0042 0043 * convert the 16 bit number in D into the 4 character ASCII 0044 * representation of its value in base 16 0045 0046 * args are D, the number to convert and 0047 * X, which contains the address for the result 0048 0049 * on exit, all regs are unchanged and the 5 bytes pointed to by X 0050 * contain a null terminated string which, when printed, is the 0051 * hexadecimal value of D 0052 hex_word: 0053 f82e 3c pshx * save x for later 0054 f82f bd f8 22 jsr hex_byte * put high byte from a 0055 f832 36 psha * save a for return 0056 f833 17 tba * now do low byte (from b) 0057 f834 bd f8 22 jsr hex_byte 0058 f837 86 00 lda #0 * store terminator 0059 f839 a7 00 sta 0,x 0060 f83b 32 pula * and restore a 0061 f83c 38 pulx * and x 0062 f83d 39 rts * so we return without changing regs 0063 0064 fffe org $fffe * reset vector 0065 fffe f8 00 fdb start