8086 asm递归的问题

时间:2015-02-11 12:04:51

标签: assembly recursion x86-16

我的ITEM3程序应该做的是检查鹅的游戏"游戏牌,地标的最终目的地已被占用,如果是,则返回直到找到空单元格。所以我想用一个递归程序。 编辑:

这是代码: 在掷出骰子之后按下地标标记的计算位置(或者已经计算出单元格给予玩家的奖励)之后调用该过程。

    ITEM3 PROC
    MOV BP, SP
    PUSHA
    MOV BX, [BP+2]    ;BX now contains the position to be checked.
    CMP BX, 0         ;if the placemarker is in the start cell,
    JE no_collision   ;there's no collision
    MOV DL, PLAYER    ;PLAYER is a variable containing 1, 2 or 3, pointing the player
;now some thoughts: if the player doesn't actually move (some "negative award")
;then there is no collision. So I check if the future position is already occupied, but
;I check if it is occupied by the same player, too. The following 6 labels are for this purpose
    CMP BL, P1_POS
    JE collisionp1
p2collision:
    CMP BL, P2_POS
    JE collisionp2
p3collision:
    CMP BL, P3_POS
    JE collisionp3
    JMP no_collision
collisionp1:
    CMP DL, 1
    JE p2collision
    JMP collision
collisionp2:
    CMP DL, 2
    JE p3collision
    JMP collision
collisionp3:
    CMP DL, 3
    JE no_collision
collision:             ;there's a collision. The placemarker goes back by 1 cell. Then the recursion start.
    DEC BL
    PUSH BX
    CALL ITEM3
    POP BX
    MOV [BP+2], BX
no_collision:
    POPA
    RET
ITEM3 ENDP

但是,当调用该过程时,地标标记没有任何反应。哪里出错了?感谢。

Here您可以找到整个代码。

1 个答案:

答案 0 :(得分:0)

这是一个老帖子,但我认为应该发布一个明确的答案。

问题是对ITEM3程序的调用已更改BP registry,因此以下涉及BP的说明无法正常执行。

在调用该过程然后立即弹出之前,解决方案只是PUSH BP

    ITEM3 PROC
    MOV BP, SP
    PUSHA
    MOV BX, [BP+2]
    CMP BX, 0
    JE no_collision
    MOV DL, PLAYER
    CMP BL, P1_POS
    JE collisionp1
p2collision:
    CMP BL, P2_POS
    JE collisionp2
p3collision:
    CMP BL, P3_POS
    JE collisionp3
    JMP no_collision
collisionp1:
    CMP DL, 1
    JE p2collision
    JMP collision
collisionp2:
    CMP DL, 2
    JE p3collision
    JMP collision
collisionp3:
    CMP DL, 3
    JE no_collision
collision:
    DEC BL
    PUSH BP                 ; HERE
    PUSH BX
    CALL ITEM3
    POP BX
    POP BP                  ; AND HERE
    MOV [BP+2], BX
no_collision:
    POPA
    RET
ITEM3 ENDP