mkl0815 wrote:
I'm using the following code for that in my monitor program. It is written in Ophis Assembler and for the 6502, so optimization for the 65C02 can be done.
k_wchr is the subroutine to output one character.
Code:
;
; print out the 8bit value in A as binary code
; X,Y is preserved, A is destroyed
;
.scope
u_bin8out:
txa ; move X to A
pha ; save (X)
tya ; move Y to A
pha ; save (Y)
ldx #$08 ; counter for 8 bit
_loop: clc ; clear carry flag
asl ; shift byte by one position
bcs _p1
tay ; save A
lda #'0
jmp _cont
_p1: tay ; save A
lda #'1 ; print "1"
_cont: jsr k_wchr
tya ; get A back
dex ; decrement counter
bne _loop
pla
tay ; restore Y
pla ; restore X
tax
rts ; return
.scend
I believe your code is incomplete. As you specify that the A reg holds the byte value to be printed, your first instruction (TXA) overwrites that value. As it's written, the value in the Y reg would be used. You also don't need the CLC instruction as the carry flag will be set based on the ASL instruction.
Below is the code I use in my monitor to print the status register. CHROUT sends a character and the CROUT send a CR/LF and returns. You can eliminate the LDA PREG and just have the A reg loaded with the value you want to print. You can also replace the JMP CROUT with a RTS if you don't want to print a CR/LF.
Code:
LDA PREG ;Get Status register preset
LDX #$08 ;Get the index count for 8 bits
SREG_LP LDY #$30 ;Get Ascii "zero"
ASL A ;Shift bit into carry
PHA ;Save Current status register value
BCC SRB_ZERO ;If clear, print a "zero"
INY ;Else increment Y reg to Ascii "one"
SRB_ZERO TYA ;Transfer Ascii character to A reg
JSR CHROUT ;Print it
PLA ;Restore current status register value
DEX ;Decrement bit count
BNE SREG_LP ;Branch back until all bits are printed
JMP CROUT ;Send CR/LF and return