首页 > 解决方案 > 装配迷宫求解器不修改 si di 和 bx 寄存器

问题描述

我正在尝试在汇编中编写迷宫求解器。我们得到一个驱动程序,它发送 si 寄存器中的当前 x 位置、di 寄存器中的 y 位置、bx 寄存器中的移动方向以及 bp 寄存器中的迷宫本身。

我应该检查鼠标是否可以向左移动,然后向前,然后向右然后向后移动。

我的问题是,当我尝试运行它时,它似乎没有修改 si di 或 bx 寄存器,我无法找出原因。

有人可以看看是否有任何明显的问题可能导致这种情况。我从来没有在这里发帖,但我已经没有选择了,非常感谢。

;---------------------------------------------------------------------
; Program:   nextval subroutine
;
; Function:  Find next mouse move in an array 15 by 30.
;            We can move into a position if its contents is blank ( 20h ).
;
; Input:     Calling sequence is:
;            x    pointer   si
;            y    pointer   di
;            dir  pointer   bx  E=1 S=2 W=3 N=4
;            maze pointer   bp
;
; Output:    x,y,dir modified in caller's data segment
;
; Owner:     Dana A. Lasher
;
; Date:      Update Reason
; --------------------------
; 11/06/2016 Original version
;
;
;---------------------------------------
         .model    small               ;64k code and 64k data
         .8086                         ;only allow 8086 instructions
         public    nextval             ;allow extrnal programs to call
;---------------------------------------


;---------------------------------------
         .data                         ;start the data segment
;---------------------------------------
value db 30
;---------------------------------------
         .code                         ;start the code segment
;---------------------------------------
; Save any modified registers
;---------------------------------------
nextval:
    push   cx                          ; save cx register
    mov    cl, 0                       ; set testing phase to 0 stored in ch
;---------------------------------------
; Code to make 1 move in the maze
;---------------------------------------

testnext:
    push   ax                          ; save ax register
    push   dx                          ; save dx register
    push   bx                          ; save bx register
    push   si                          ; save si register
    push   di                          ; sav  di register
    mov    dh, [si]                    ; load the x value into dh
    mov    dl, [di]                    ; load the y value into dl
    inc    cl                          ; increment the testing phase

direction:
    cmp    word ptr [bx], 1            ; is the moving direction East
    je     goingeast                   ; yes, jump to the going east function
    cmp    word ptr [bx], 2            ; is the moving direction south
    je     goingsouth                  ; yes, jump to the going south function
    cmp    word ptr [bx], 3            ; is the moving direction west
    je     goingwest                   ; yes, jump to the going west function
    cmp    word ptr [bx], 4            ; is the moving direction north
    je     goingnorth                  ; yes, jump to the going north function
    jmp    exit
;---------------------------------------
; Going East        Check order: N-E-S-W
;---------------------------------------
goingeast: 
    cmp    cl, 1                       ; is the testing phase phase 1
    je     checknorth                  ; yes, check to see if a move north is valid
    cmp    cl, 2                       ; is the testing phase phase 2
    je     checkeast                   ; yes, check to see if a move east is valid
    cmp    cl, 3                       ; is the testing phase phase 3
    je     checksouth                  ; yes, check to see if a move south is valid 
    cmp    cl, 4                       ; is the testing phase phase 4
    je     checkwest                   ; yes, check to see if a move west is valid 
    jmp    exit 
;---------------------------------------
; Going South       Check order: E-S-W-N
;---------------------------------------
goingsouth:
    cmp    cl, 1                       ; is the testing phase phase 1
    je     checkeast                   ; yes, check to see if a move east is valid
    cmp    cl, 2                       ; is the testing phase phase 2
    je     checksouth                  ; yes, check to see if a move south is valid 
    cmp    cl, 3                       ; is the testing phase phase 3
    je     checkwest                   ; yes, check to see if a move west is valid 
    cmp    cl, 4                       ; is the testing phase phase 4
    je     checknorth                  ; yes, check to see if a move north is valid
    jmp    exit 
;---------------------------------------
; Going West        Check order: S-W-N-E
;---------------------------------------
goingwest: 
    cmp    cl, 1                       ; is the testing phase phase 1
    je     checksouth                  ; yes, check to see if a move south is valid 
    cmp    cl, 2                       ; is the testing phase phase 2
    je     checkwest                   ; yes, check to see if a move west is valid 
    cmp    cl, 3                       ; is the testing phase phase 3
    je     checknorth                  ; yes, check to see if a move north is valid
    cmp    cl, 4                       ; is the testing phase phase 4
    je     checkeast                   ; yes, check to see if a move east is valid
    jmp    exit 
;---------------------------------------
; Going North       Check order: W-N-E-S
;---------------------------------------
goingnorth:
    cmp    cl, 1                       ; is the testing phase phase 1
    je     checkwest                   ; yes, check to see if a move west is valid 
    cmp    cl, 2                       ; is the testing phase phase 2
    je     checknorth                  ; yes, check to see if a move north is valid
    cmp    cl, 3                       ; is the testing phase phase 3
    je     checkeast                   ; yes, check to see if a move east is valid
    cmp    cl, 4                       ; is the testing phase phase 4
    je     checksouth                  ; yes, check to see if a move south is valid 
    jmp    exit 
;---------------------------------------
; Check East                X + 1 Y same
;---------------------------------------
checkeast:
    inc    byte ptr [si]               ; increment the x position
    inc    dh                          ; incremement dh to the x position being tested
    mov    ch, 1                       ; update the testing direction ch to 1
    jmp    testposition                ; jump to the test position function
;---------------------------------------
; Check South               X same Y + 1
;---------------------------------------
checksouth:
    inc    byte ptr [di]               ; increment the y position
    inc    dl                          ; increment dl to the y position being tested
    mov    ch, 2                       ; update the testing direction ch to 2
    jmp    testposition                ; jump to the test position function
;---------------------------------------
; Check West                X - 1 Y same
;---------------------------------------
checkwest:
    dec    byte ptr [si]               ; decrement the x position
    dec    dh                          ; update dh to the x position being tested
    mov    ch, 3                       ; update the testing direction ch to 3
    jmp    testposition                ; jump to the test position function
;---------------------------------------
; Check North               X same Y - 1
;---------------------------------------
checknorth:
    dec    byte ptr [di]               ; increment the y position
    dec    dl                          ; update dl to the y position being tested
    mov    ch, 4                       ; update the testing direction ch to 4

testposition:
    mov    ax, [di]                    ; move the y position being tested into the ax register
    dec    ax                          ; decrement the ax register for the offset calculation
    mul    [value]                     ; multiply the al register by 30 and store the product in ax
    add    ax, [si]                    ; add the x position to the ax
    dec    ax                          ; decrement the ax register for the offset calculation
    mov    [si], ax                    ; move the offset calculated inside of ax into si
    mov    ax, ds:[bp + si]            ; access the maze using data segment override with the offset in si
    cmp    ax, 20h                     ; position in the maze at the offset empty
    je     exit                        ; yes jump to the exit function
    pop    di                          ; no, restore the di register
    pop    si                          ; no, restore the si register
    pop    bx                          ; no, restore the bx register
    pop    dx                          ; no, restore the dx register
    pop    ax                          ; no, restore the ax register
    jmp    testnext                    ; test the next move direction
;---------------------------------------
; Restore registers and return
;---------------------------------------
exit:
    pop    di                          ; restore the di register
    pop    si                          ; restore the si register
    pop    bx                          ; restore the bx register

    ; here the dx and cx registers should still have the needed information
    mov    byte ptr [si], dh           ; update x position
    mov    byte ptr [di], dl           ; update y position
    mov    byte ptr [bx], ch           ; update moving direction

    pop    dx                          ; restore the dx register
    pop    ax                          ; restore the ax register
    pop    cx                          ; restore the cx register
    ret                                ; return
;---------------------------------------
    end    nextval

标签: assemblyx86-16masm

解决方案


; 输出:调用者数据段中修改的 x,y,dir

鉴于序言中的这句话,我认为迷宫求解器不修改SIDIBX寄存器是一件好事......

过早销毁输入数据

代码中的问题之一是您在测试进行时破坏了原始数据。而且因为你需要多次测试,后续的测试将使用垃圾数据。
在该过程中,您只能使用您在和寄存器中加载的XY的本地副本。DHDL

这些必须去:

inc    byte ptr [si]               ; increment the x position
inc    byte ptr [di]               ; increment the y position
dec    byte ptr [si]               ; decrement the x position
dec    byte ptr [di]               ; increment the y position

虚假地址计算

mov    [si], ax         ; move the offset calculated inside of ax into si
  • 该指令与评论所说的不同。那将是mov si, ax
    同样重要的是,您不应该SI在程序的这一点上进行破坏!

  • testpositiondec ax部分的说明中,我们看到您希望XY是基于 1 的坐标。这很好,但您必须在计算中使用本地修改的值:DHDL

  • 偏移地址计算适合字节大小的数组(迷宫)。因此,您不应该从中比较一个

尝试下一个代码:

mov    al, DL           ; move the y position being tested into the AL register
dec    al               ; decrement the AL register for the offset calculation
mul    [value]          ; multiply the al register by 30 and store the product in ax
add    al, DH           ; add the x position to the ax
adc    ah, 0
dec    ax               ; decrement the ax register for the offset calculation
push   si               ; Preserve SI
mov    si, ax           ; move the offset calculated inside of ax into si
mov    al, ds:[bp + si] ; access the maze using data segment override with offset si
pop    si               ; Restore SI
cmp    al, 20h          ; position in the maze at the offset empty
je     exit             ; yes jump to the exit function

最后一个问题

您确定方向变量dir实际上是一个word我会更早地期望在一个字节大小的变量中找到那个小值。


推荐阅读