装配迷宫解算器不修改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

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

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

过早销毁输入数据

代码中的一个问题是在测试过程中破坏了原始数据。由于您需要多次测试,因此后续的测试将使用垃圾数据
在该过程中,您只能使用加载到DHDL寄存器中的XY的本地副本。

这些必须离开:

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

  • 测试位置部分的dec 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实际上是单词吗。我更希望在字节大小的变量中找到这个小值。

最新更新