User Tools

Site Tools


base:another_hexadecimal_to_decimal_conversion

This is an old revision of the document!


Another Hexadecimal to Decimal Conversion

by Mace

Garth Wilson used decimal mode in Hexadecimal to Decimal Conversion and the result was in 3 bytes in decimal mode. I needed plain ASCII, the following routine does just that, using more or less the same method as Garth used:

start:
		lda #$30	// clear the result buffer
		ldy #$00
	clear:
		sta result,y
		iny
		cpy #$05
		bne clear
		ldx #$4f
loop1:
		clc
		rol lobyte
		rol hibyte
		bcs calculate	// when a bit drops off, it means we need to calculate
				// if not, go to the next bit
		txa
		axs #$05	// ILLEGAL OPCODE, use the 3 lines below as alternative
				// sec
				// sbc #$05
				// tax
		cpx #$ff
		bne loop1
END:
		rts

calculate:
		clc
		ldy #$04
loop2:
		lda table,x	// get the decimal equivalent of the bit in ASCII numbers
		beq zero	// skip (speed up) when there's nothing to add
		adc result,y	// add to whatever result we alread haven
		cmp #$3a	// passing 10 with the addition?
		bcc notten
		sbc #$0a	// if so, subtract 10, carry will take care of overflow 
notten:
		sta result,y
zero:
		dex
		dey
		bpl loop2	// loop until all 5 digits have been
		jmp loop1
table:
		.byte 0,0,0,0,1
		.byte 0,0,0,0,2
		.byte 0,0,0,0,4
		.byte 0,0,0,0,8
		.byte 0,0,0,1,6
		.byte 0,0,0,3,2
		.byte 0,0,0,6,4
		.byte 0,0,1,2,8
		.byte 0,0,2,5,6
		.byte 0,0,5,1,2
		.byte 0,1,0,2,4
		.byte 0,2,0,4,8
		.byte 0,4,0,9,6
		.byte 0,8,1,9,2
		.byte 1,6,3,8,4
		.byte 3,2,7,6,8

result:
		.byte 0,0,0,0,0
lobyte:
		.byte 11
hibyte:
		.byte 0
base/another_hexadecimal_to_decimal_conversion.1429237827.txt.gz · Last modified: 2015-04-17 04:30 by 127.0.0.1