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