cpu 6502
nam hex2dec
page 128,128
* = $C000  ; assemble at $C000 (49152)
code
	;; db $08, $48, $8A, $48, $98, $48 ;Save Registers 
.start	jmp .1
	;; Data Storage
.nm	db $86
	;; Program Resumes with some setup, put spaces in all the digits
.1	lda #$20
	sta .dh
	sta .dt
	sta .do
	clc
	;; Get the Hex Value and put it in reg-x
	ldx .nm

	;; Compare it with 200
	cpx #$C8

	;; If it's lt 200, check for lt 100
	bcc .lt200

	;; Otherwise, it's gt 200
	;; Si put a "2" in the hundreds place
	lda #$32
	sta .dh

	;; Put the Hex Value that's in x-reg
	;; into the Accumulator
	txa

	;; Subtract 200 from it
	sbc #$C8

	;; Store The result in the data storage
	sta .nm

	;; Clear the Carry Flag
	clc
	
	;; Store a Zero in the Tens Place for now
	;; Because we know that there's a hundreds
	;; digit that is at least a 2
	lda #$30
	sta .dt

	;; Because we've subtracted 200 from it
	;; it MUST be less than 100, so JMP there
	jmp .lt100
	
.lt200	ldx .nm
	cpx #$64
	bcc .lt100
	
	lda #$31
	sta .dh
	txa
	sbc #$64
	sta .nm
	clc
	;; Store a Zero in the Tens Place for now
	lda #$30
	sta .dt

.lt100  ldx .nm
	cpx #$5A
	bcc .lt90
	lda #$39
	sta .dt
	txa
	sbc #$5A
	sta .nm
	clc

.lt90   ldx .nm
	cpx #$50
	bcc .lt80
	lda #$38
	sta .dt
	txa
	sbc #$50
	sta .nm
	clc
	
.lt80	ldx .nm
	cpx #$46
	bcc .lt70
	lda #$37
	sta .dt
	txa
	sbc #$46
	sta .nm
	clc
	
.lt70	ldx .nm
	cpx #$3C
	bcc .lt60
	lda #$36
	sta .dt
	txa
	sbc #$3C
	sta .nm
	clc
	
.lt60	ldx .nm
	cpx #$32
	bcc .lt50
	lda #$35
	sta .dt
	txa
	sbc #$32
	sta .nm
	clc
	
.lt50	ldx .nm
	cpx #$28
	bcc .lt40
	lda #$34
	sta .dt
	txa
	sbc #$28
	sta .nm
	clc
	
.lt40	ldx .nm
	cpx #$1E
	bcc .lt30
	lda #$33
	sta .dt
	txa
	sbc #$1E
	sta .nm
	clc
	
.lt30	ldx .nm
	cpx #$14
	bcc .lt20
	lda #$32
	sta .dt
	txa
	sbc #$14
	sta .nm
	clc
	
.lt20   ldx .nm
	cpx #$0A
	bcc .lt10
	lda #$31
	sta .dt
	txa
	sbc #$0A
	sta .nm
	clc
	
.lt10	lda .nm
	
	adc #$30
	sta .do
	sta .nm
	clc
	;; db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	rts
	;; More Data Storage
.dh	db $20
.dt	db $20
.do	db $20

.disp	lda .dh
	cmp #$20
	beq .3
	jsr $FFD2
.3	lda .dt
	cmp #$20
	beq .4
	jsr $FFD2
.4	lda .do
	jsr $FFD2
	rts


code