装配迷宫解算器不修改 si di 和 bx 寄存器

Assembly maze solver not modifying si di and bx registers

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

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

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

有人可以看看是否有任何明显的问题可能导致此问题。我从来没有 post 在这里但是我 运行 没有选择非常感谢你。

;---------------------------------------------------------------------
; 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

; Output: x,y,dir modified in caller's data segment

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

过早销毁输入数据

您的代码中的一个问题是您在测试过程中破坏了原始数据。而且因为需要多次测试,后面的测试会使用垃圾数据。
在该过程中,您只能使用您在 DH 和 [=17= 中加载的 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

  • testposition 部分的 dec ax 说明中,我们看到您期望 XY 为基于 1 的坐标。没关系,但你必须在计算中使用 DHDL 中本地修改的值:

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

尝试下一个代码:

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。我希望在 byte 大小的变量中找到那个小值。