Z80 DAA指令
对于这个看似小问题的歉意,但我似乎无法在任何地方找到答案 - 我只是要在我的 Z80 模拟器中实现 DAA 指令,我在 Zilog 手册中注意到它是为了调整的目的用于二进制编码十进制算术的累加器。它表示该指令旨在在加法或减法指令之后立即运行。
我的问题是:
- 如果在另一条指令之后运行它会发生什么?
- 它如何知道它之前的指令是什么?
- 我意识到有 N 标志 - 但这肯定不会明确表明前一条指令是加法或减法指令?
- 它是否只是根据 DAA 表中规定的条件修改累加器,而不管前面的指令如何?
Apologies for this seemingly minor question, but I can't seem to find the answer anywhere - I'm just coming up to implementing the DAA instruction in my Z80 emulator, and I noticed in the Zilog manual that it is for the purposes of adjusting the accumulator for binary coded decimal arithmetic. It says the instruction is intended to be run right after an addition or subtraction instruction.
My questions are:
- what happens if it is run after another instruction?
- how does it know what instruction preceeded it?
- I realise there is the N flag - but this surely wouldnt definitively indicate that the previous instruction was an addition or subtraction instruction?
- Does it just modify the accumulator anyway, based on the conditions set out in the DAA table, regardless of the previous instruction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。该文档仅告诉您 DAA 的用途。也许您指的是此链接处的表格:
我必须说,我从来没有看到了一个较迟的指令规范。如果仔细检查该表,您会发现指令的效果仅取决于
C
和H
标志以及累加器中的值 - 它并不依赖于完全取决于之前的指令。此外,它也没有透露,例如,C=0
、H=1
以及累加器中的低位数字是 4 或 5 时会发生什么。因此,您在这种情况下,必须执行 NOP,或者生成错误消息或其他内容。Yes. The documentation is only telling you what DAA is intended to be used for. Perhaps you are referring to the table at this link:
I must say, I've never seen a dafter instruction spec. If you examine the table carefully, you will see that the effect of the instruction depends only on the
C
andH
flags and the value in the accumulator -- it doesn't depend on the previous instruction at all. Also, it doesn't divulge what happens if, for example,C=0
,H=1
, and the lower digit in the accumulator is 4 or 5. So you will have to execute aNOP
in such cases, or generate an error message, or something.只是想补充一下,当他们谈论之前的操作时,N 标志就是他们的意思。加法设置 N = 0,减法设置 N = 1。因此,A 寄存器和 C、H 和 N 标志的内容决定结果。
该指令旨在支持 BCD 算术,但还有其他用途。考虑以下代码:
它最终将 A 寄存器的低 4 位转换为 ASCII 值“0”、“1”、...“9”、“A”、“B”、...、“F”。换句话说,二进制到十六进制的转换器。
Just wanted to add that the N flag is what they mean when they talk about the previous operation. Additions set N = 0, subtractions set N = 1. Thus the contents of the A register and the C, H and N flags determine the result.
The instruction is intended to support BCD arithmetic but has other uses. Consider this code:
It ends converting the lower 4 bits of A register into the ASCII values '0', '1', ... '9', 'A', 'B', ..., 'F'. In other words, a binary to hexadecimal converter.
这是生产中的代码,正确实现 DAA 并通过了 zexall/zexdoc/z80test Z80 操作码测试套件。
基于未记录的 Z80 记录,第 17-18 页。
为了可视化 DAA 交互,出于调试目的,我编写了一个小型 Z80 汇编程序,该程序可以在实际的 ZX Spectrum 或准确模拟 DAA 的仿真中运行:https://github.com/ruyrybeyro/daatable
至于它的行为方式,之前得到了一个标志 N、C、H 的表并注册了 A使用上述汇编程序生成 DAA 后: https://github.com/ruyrybeyro /daatable/blob/master/daaoutput.txt
This is code in production, implementing DAA correctly and passes the zexall/zexdoc/z80test Z80 opcode test suits.
Based on The Undocumented Z80 Documented, pag 17-18.
For visualising the DAA interactions, for debugging purposes, I have written a small Z80 assembly program, that can be run in an actual ZX Spectrum or in an emulation that emulates accurately DAA: https://github.com/ruyrybeyro/daatable
As how it behaves, got a table of flags N,C,H and register A before and after DAA produced with the aforementioned assembly program: https://github.com/ruyrybeyro/daatable/blob/master/daaoutput.txt
我发现这条指令也相当令人困惑,但我发现它的行为描述来自 z80-heaven 是最有帮助的。
这为指令提供了一个简单的模式:
它可以随时运行。
I found this instruction rather confusing as well, but I found this description of its behavior from z80-heaven to be most helpful.
This provides a simple pattern for the instruction:
Also, while DAA is intended to be run after an addition or subtraction, it can be run at any time.