diff --git a/print16.asm b/print16.asm index 5825978..86f421c 100644 --- a/print16.asm +++ b/print16.asm @@ -5,7 +5,7 @@ print16: .scope .data zp -.space _num 6 +.space _num 4 .space _num_dec 6 .text pha @@ -13,14 +13,13 @@ print16: pha lda #0 - ldx #6 + ldx #10 * dex - sta _num, x - sta _num_dec, x + sta _num - 1, x bne - - `splitbyte s, 4 - `splitbyte s + 1, 2 + `splitbyte s, 3 + `splitbyte s + 1, 1 `carry `print _num_dec @@ -29,43 +28,75 @@ print16: pla rts -_store: - adc _num_dec, y - sta _num_dec, y - ;jsr printbyte - rts +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; mod函数处理的数据为打印方便均使用大端序存储 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -_store2: - adc _num_dec, y - sta _num_dec, y - ;jsr printbyte - dey +mod_4: ; 高字节低4位若大于10,则向高位进1 + lda _num + 3 + cmp #10 + bcc + + sbc #10 ; 此时c为1 + tax lda #1 - sta _num_dec, y + sta _num_dec + 3 ; 进位 + txa +* sta _num_dec + 4 rts -mod_4: `mod10 4, $26 +mod_3: ; 高字节高4位的值相当于个位加6,十位加1 + ldx _num + 2 + bne + ; 如果为0直接返回 + rts +* lda #6 + `carry10 4 + lda #1 + `carry10 3 + dex + bne - + rts -mod_3: `mod10 3, $2c +mod_2: ; 低字节低4位的值相当于个位加6,十位加5,百位加2 + ldx _num + 1 + bne + ; 如果为0直接返回 + rts +* lda #6 + `carry10 4 + lda #5 + `carry10 3 + lda #2 + `carry10 2 + dex + bne - + rts -mod_2: `mod10 2, $28 +mod_1: ; 低字节低4位的值相当于个位加6,十位加9,百位加0,千位加4 + ldx _num + bne + ; 如果为0直接返回 + rts +* lda #6 + `carry10 4 + lda #9 + `carry10 3 + lda #4 + `carry10 1 + dex + bne - + rts -mod_1: `mod10 1, $2a .scend -.macro mod10 - ldy #_1 - lda _num, y - ;jsr printbyte - cmp #10 +.macro carry10 clc - bmi _s - adc #_2 - jsr _store2 - rts -_s: adc #$30 - jsr _store - rts + adc _num_dec + _1 + bcc _skip + pha + adc _num_dec + _1 - 1 + sta _num_dec + _1 - 1 + pla +_skip: + sta _num_dec + _1 + ;jsr printbyte .macend .macro splitbyte @@ -88,15 +119,45 @@ _s: adc #$30 tya pha + sed ; 设置为bcd加减法 jsr mod_4 jsr mod_3 jsr mod_2 - jsr mod_1 - lda #$30 - clc - adc _num_dec - sta _num_dec - + jsr mod_1 ; 此时结果中有些位可能大于10,需要进行进位处理 + ldx #4 +_loop: + lda _num_dec, x + ldy #0 ; y记录进位数 + cmp #10 + bcc _skip ; 小于10不进位 + pha + ora #$f0 + ror + ror + ror + ror + tay + pla + ora #$0f + sta _num_dec, x + tya + dex + adc _num_dec, x + sta _num_dec, x + inx +_skip: + dex + bne _loop + + cld ; 退出bcd模式 + lda #$30 ; 转化为可显示字符 + ldx #5 +_up: + adc _num_dec - 1, x + sta _num_dec - 1, x + dex + bne _up + pla tay .macend diff --git a/printbyte.asm b/printbyte.asm index b9a8c8c..7da63c8 100644 --- a/printbyte.asm +++ b/printbyte.asm @@ -2,6 +2,7 @@ .space _na 1 ; a的临时存放处 .text printbyte: + php pha sta _na txa @@ -18,4 +19,5 @@ printbyte: pla tax pla + plp rts \ No newline at end of file diff --git a/snake.prg b/snake.prg index 8b44b5d..31e306d 100644 Binary files a/snake.prg and b/snake.prg differ