CPU模拟器延时代码

CPU simulator time delay code

我无法在网上找到任何关于此的信息,因此将不胜感激。 我正在尝试将一些涉及时间延迟的示例代码合并到我的大学告诉我们制作的蛇程序中,我们在 "Microcontroller simulator" 中 运行。 有人可以解释一下这段代码是如何工作的吗?我会 post 下面:

; ---------------------------------------------------------------

;  A general purpose time delay procedure.

;  The delay is controlled by the value in AL.

;  When the procedure terminates, the CPU registers are
;  restored to the same values that were present before 
;  the procedure was called. Push, Pop, Pushf and Popf
;  are used to achieve this.  In this example one procedure 
;  is re-used three times.  This re-use is one of the main 
;  advantages of using procedures.

;------ The Main Program ----------------------------------------
Start:
    MOV AL,8    ; A short delay.
    CALL    30  ; Call the procedure at address [30]

    MOV AL,10   ; A middle sized delay.
    CALL    30  ; Call the procedure at address [30]

    MOV AL,20   ; A Longer delay.
    CALL    30  ; Call the procedure at address [30]

JMP Start   ; Jump back to the start.

; ----- Time Delay Procedure Stored At Address [30] -------------
    ORG 30  ; Generate machine code from address [30]

    PUSH    AL  ; Save AL on the stack.
    PUSHF       ; Save the CPU flags on the stack.
Rep:
    DEC AL  ; Subtract one from AL.
    JNZ REP ; Jump back to Rep if AL was not Zero.

    POPF        ; Restore the CPU flags from the stack.
    POP AL  ; Restore AL from the stack.

    RET     ; Return from the procedure.
; ---------------------------------------------------------------
    END
; ---------------------------------------------------------------

如果您需要更多信息,请告诉我。

让我们看看造成延迟的代码

; ----- Time Delay Procedure Stored At Address [30] -------------
    ORG 30  ; Generate machine code from address [30]

    PUSH    AL  ; Save AL on the stack.
    PUSHF       ; Save the CPU flags on the stack.
Rep:
    DEC AL  ; Subtract one from AL.
    JNZ REP ; Jump back to Rep if AL was not Zero.

    POPF        ; Restore the CPU flags from the stack.
    POP AL  ; Restore AL from the stack.

    RET     ; Return from the procedure.

ORG 30 语句确保代码从内存地址 30 开始。这是您在调用子例程时指定的地址。

代码随后将 AL 寄存器压入堆栈,因为它即将使用它。 PUSHF 保存 CPU 标志的状态,以便您以后可以检索它们。

DEC AL / JNZ REP 部分是一个循环。它迭代与 AL 中存储的数字一样多的次数。

POPF 和 POP AL 恢复代码开始延迟循环之前的状态。具体来说,它使 AL 和 CPU 标志处于调用者在调用此代码之前放置它们的相同状态。

RET returns 控制权给调用者。

现在我们看看这段代码是怎么调用的

MOV AL,8    ; A short delay.
CALL    30  ; Call the procedure at address [30]

为 AL 指定值 8 并调用延迟例程,您使用 ORG 30 语句将其放置在内存地址 30。

延迟程序会循环8次,因为你在AL中传入了8的值