问题描述
我试图将 20 除以 2,但我的 6502 计算机打印的是 254254
而不是 10
。
代码如下:
PORTB = $6000
PORTA = $6001
DDRB = $6002
DDRA = $6003
E = %10000000
RW = %01000000
RS = %00100000
N = $2000 ; 1 byte
D = $2001 ; 1 byte
Q = $2002 ; 1 byte
R = $2003 ; 1 byte
value = $0204 ; 2 bytes
mod10 = $0206 ; 2 bytes
message = $0208 ; 6 bytes
.org $e000
error_message: .asciiz "An error occured"
loop:
jmp loop
error:
ldx #0
error_loop:
lda error_message,x
beq loop
jsr print_char
inx
jmp error_loop
print_char:
jsr lcd_wait
sta PORTB
lda #RS
sta PORTA
lda #(RS | E)
sta PORTA
lda #RS
sta PORTA
rts
lcd_wait:
pha
lda #%00000000 ; Set all pins on port B to input
sta DDRB
lcd_busy:
lda #RW ; Set RW pin
sta PORTA
lda #(RW | E) ; Set RW and E pins
sta PORTA
lda PORTB ; Read port B
and #%10000000
bne lcd_busy
lda #%11111111 ; Set all pins on port B to output
sta DDRB
pla
rts
lcd_instruction:
jsr lcd_wait
sta PORTB
lda #0 ; Clear RS/RW/E bits
sta PORTA
lda #E ; Set E bit to send instruction
sta PORTA
lda #0 ; Clear RS/RW/E bits
sta PORTA
rts
divide:
lda N
beq error
lda D
beq error
lda N
cmp D
bcc error
lda #0
sta Q
lda N
sta R
divloop:
sec
inc Q
lda R
sbc D
sta R
bcs divloop
dec Q
jsr print_R
jsr print_Q
rts
print_R:
lda #0
sta message
; Initialize value to be the number to convert
lda R
sta value
lda #0
sta value + 1
jmp divide_int
print_Q:
lda #0
sta message
; Initialize value to be the number to convert
lda R
sta value
lda #0
sta value + 1
divide_int:
; Initialize the remainder to zero
lda #0
sta mod10
sta mod10 + 1
clc
ldx #16
divloop_int:
; Rotate quotient and remainder
rol value
rol value + 1
rol mod10
rol mod10 + 1
; a,y = dividend - divisor
sec
lda mod10
sbc #10
tay ; save low byte in Y
lda mod10 + 1
sbc #0
bcc ignore_result ; branch if dividend < divisor
sty mod10
sta mod10 + 1
ignore_result:
dex
bne divloop_int
rol value ; shift in the last bit of the quotient
rol value + 1
lda mod10
clc
adc #"0"
jsr push_char
; if value != 0,then continue dividing
lda value
ora value + 1
bne divide_int ; branch if value not zero
ldx #0
print:
lda message,x
beq return
jsr print_char
inx
jmp print
push_char:
pha ; Push new first character onto stack
ldy #0
char_loop:
lda message,y ; Get char on string and put into X
tax
pla
sta message,y ; Pull char off stack and add it to the string
iny
txa
pha ; Push char from string onto stack
bne char_loop
pla
sta message,y ; Pull the null off the stack and add to the end of the string
return:
rts
reset:
ldx #$ff
txs
lda #%11111111 ; Set all pins on port B to output
sta DDRB
lda #%11100000
sta DDRA
lda #20
sta N
lda #2
sta D
lda #%11100000 ; Set top 3 pins on port A to output
sta DDRA
lda #%00000001 ; Clear display
jsr lcd_instruction
lda #%00111000 ; Set 8-bit mode; 2-line display; 5x8 font
jsr lcd_instruction
lda #%00001110 ; display on; cursor on; blink off
jsr lcd_instruction
lda #%00000110 ; Increment and shift cursor; don't shift display
jsr lcd_instruction
lda #0
sta message
jsr divide
jmp loop
.org $fffc
.word reset
.word $0000
我希望有人看到我的错误,我已经调试了 5 个小时了。 我的 6502 的灵感来自 Ben Eater 的一款。 我使用 vasm 作为具有旧式语法的编译器。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)