6502 汇编随机数问题
6502 assembler random number problems
我在这里玩 6502 汇编程序:http://skilldrick.github.io/easy6502
我做的只是将随机颜色像素放在屏幕上的随机位置:
LDY #[=10=] ; Clear Y
loop:
JSR genPos ; Put new position in memory
LDA $fe ; Get random number for color
STA (), Y ; Put pixel on screen
JMP loop
genPos:
STA ; Store accumulator in low
LDA $fe ; Get new random number (WHY, oh why?)
AND # ; Mask out low two bits (=numbers 0-3)
CLC ; Clear carry flag
ADC #2 ; Add 2 (= numbers 2-5)
STA ; Store number in high
RTS
我正在尝试使用尽可能少的指令。我的问题是,如果我不在 genPos
子例程中放置额外的 LDA $fe
,像素将以一种非常奇怪的模式绘制,如果我有额外的 LDA,代码将完美运行。我不明白为什么 - 谁能给我提示?
此致,
雅各布
LDA应用的算法是:
[random 1] -> [11:10]
[random 1] -> [10]
[random 2]&3 + 2 -> [11]
(repeat)
如果您无法从 $fe 重新加载 a
那么您正在做的是:
[random 1] -> [11:10]
[ramdom 1] -> [10]
[random 1]&3 + 2 -> [11]
(repeat)
因此,寻址像素的低字节和高字节都是同一随机数的函数。他们不是独立的。这在它们之间建立了相关性。这体现在你看到的模式中。如果你假装的屏幕是 256 像素宽,那它就是一条直对角线;基于你看到 8 条的事实,我可以推断出你的输出实际上必须是 256/8 = 32 像素宽。
问题不在于您说 "the random number in the accumulator ... should make a fine low byte for the screen address." 是错误的,而是您随后也将其用于高字节。你正在为两者使用它。高字节是低字节的直接函数。所以有一堆你永远不会碰到的高字节,因为它们不满足 high=(low&3)+2
的约束。这些像素保持不亮。
它已经在做一个很好的低字节了!这一行:
LDA $fe ; Get new random number (WHY, oh why?)
继续决定high字节,如果不生成新的随机数,y
值将依赖于 x
值的最低两位 ,导致您看到的对角线:x & 3
的值始终等于该值绘制在屏幕的哪个部分,这意味着您获得类似
的模式
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 0 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 1 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 2 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 3 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
我在这里玩 6502 汇编程序:http://skilldrick.github.io/easy6502
我做的只是将随机颜色像素放在屏幕上的随机位置:
LDY #[=10=] ; Clear Y
loop:
JSR genPos ; Put new position in memory
LDA $fe ; Get random number for color
STA (), Y ; Put pixel on screen
JMP loop
genPos:
STA ; Store accumulator in low
LDA $fe ; Get new random number (WHY, oh why?)
AND # ; Mask out low two bits (=numbers 0-3)
CLC ; Clear carry flag
ADC #2 ; Add 2 (= numbers 2-5)
STA ; Store number in high
RTS
我正在尝试使用尽可能少的指令。我的问题是,如果我不在 genPos
子例程中放置额外的 LDA $fe
,像素将以一种非常奇怪的模式绘制,如果我有额外的 LDA,代码将完美运行。我不明白为什么 - 谁能给我提示?
此致, 雅各布
LDA应用的算法是:
[random 1] -> [11:10]
[random 1] -> [10]
[random 2]&3 + 2 -> [11]
(repeat)
如果您无法从 $fe 重新加载 a
那么您正在做的是:
[random 1] -> [11:10]
[ramdom 1] -> [10]
[random 1]&3 + 2 -> [11]
(repeat)
因此,寻址像素的低字节和高字节都是同一随机数的函数。他们不是独立的。这在它们之间建立了相关性。这体现在你看到的模式中。如果你假装的屏幕是 256 像素宽,那它就是一条直对角线;基于你看到 8 条的事实,我可以推断出你的输出实际上必须是 256/8 = 32 像素宽。
问题不在于您说 "the random number in the accumulator ... should make a fine low byte for the screen address." 是错误的,而是您随后也将其用于高字节。你正在为两者使用它。高字节是低字节的直接函数。所以有一堆你永远不会碰到的高字节,因为它们不满足 high=(low&3)+2
的约束。这些像素保持不亮。
它已经在做一个很好的低字节了!这一行:
LDA $fe ; Get new random number (WHY, oh why?)
继续决定high字节,如果不生成新的随机数,y
值将依赖于 x
值的最低两位 ,导致您看到的对角线:x & 3
的值始终等于该值绘制在屏幕的哪个部分,这意味着您获得类似
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 0 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 1 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 2 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /
█ █ █ █ █ █ █ █ \
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ | x & 3 == 3 in 0-FF
█ █ █ █ █ █ █ █ |
█ █ █ █ █ █ █ █ /