如何重新构造此代码以提高效率? 8086升降模拟器

时间:2015-04-15 22:41:04

标签: assembly x86-16

我如何更改此代码以更高效甚至以任何方式不同?使用过程而不是循环,任何减少RAM使用的东西都是一个好主意,因为我真的很挣扎。电梯模拟器基本上需要再次上下移动而不会崩溃。

; ------------------------ THE LIFT ----------------------------
Start:
CLO
OR  AL, 00 
OUT 06      ; Display lift window

; --------------------------------------------------------------
; A LOOP THAT CONTROLS THE LIFT WHEN IDLE
Idle:
IN  06      ; Read lift status
AND AL, 20      ; Isolate UP button bit
JNZ Up      ; If UP button pressed, jump to UP
IN  06      ; Read lift status
AND AL, 10      ; Isolate DOWN button bit
JNZ Down        ; If DOWN button pressed, jump to DOWN
JMP Idle        ; Jump back to Idle

; --------------------------------------------------------------
; A LOOP TO CONTROL THE LIFT WHEN GOING UP
Up:
MOV AL, 21      ; Move hex value 21 to AL
OUT 06      ; Display lift status
MOV [AF], AL    ; Move contents of AL to RAM location AF
IN  06      ; Read lift status
AND AL, 4       ; Clear Bit 4 for use
JNZ Stop        ; Stop if lift is close enough
MOV AL, [AF]    ; Move contents of RAM location AF to AL
JMP Idle        ; Jump back to Idle

; --------------------------------------------------------------
; A LOOP TO CONTROL THE LIFT WHEN GOING DOWN
Down:
MOV AL, 12      ; Move hex value 12 to AL
OUT 06      ; Display the lift status
MOV [AF], AL    ; Move contents of AL to RAM location AF
IN  06      ; Read lift status
AND AL, 8       ; Clear Bit 8 for use
JNZ Stop        ; Stop if lift is close enough
MOV AL, [AF]    ; Move contents of RAM location AF to AL
JMP Idle        ; Jump back to Idle

; --------------------------------------------------------------
; A PROCEDURE TO STOP THE LIFT AT THE TOP AND BOTTOM
Stop:
MOV AL, AF      ; 
MOV AL, 0       ; 
OUT 06      ;
JMP Idle

; --------------------------------------------------------------
END
; --------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

您可以做两件事。

第一个Idle:部分可以从7条指令减少到5条指令......

 Idle:

    IN      06              ; Read lift status
    AND     AL, 30h         ; Check the Up and down buttons
    JZ      Idle            ; Nope, neither button pushed

    TEST    AL, 20h         ; Up button pressed ?
    JNZ     Up              ; Yes, go do it

                            ; ---ELSE--- Down button is pushed, so fall through
 Down:
    :
    :
    :

另外,你有一条被它后面的指令销毁的指令,所以你可以完全删除它......

Stop:

    MOV AL, AF              ; This instruction can be removed
    MOV AL, 0               ; 
    OUT 06                  ;
    JMP Idle