簡體   English   中英

匯編程序未運行英特爾8086

[英]Assembly Procedures not running Intel 8086

下面的代碼應該獲取20個用戶輸入的數字(6個數字或更少的數字)並計算平均值並對它們進行排序,當我將其設置為6個或更少的數字時,它可以正常工作。 但是,當它設置為獲取7-20個數字時,在獲取數字后,它將跳過下一個過程,某些程序再次運行GetNum過程(從用戶那里獲取數字的過程),當它獲得11個數字時,我會收到此消息“程序已將控制權歸還給操作系統”。

ShowMsg macro msg
mov ah, 09h
mov dx, offset msg
int 21h
endm

NewLine macro
mov ah, 02h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
endm


data segment
sum   dd 0
num   dd 0
ave   dd 0
array dd 20 dup(0)
msg1  db 'Enter 20 numbers:', '$'
msg2  db 0dh,0ah,'Average: ', '$'
temp  dd ?
data ends 

stack segment
dd 100 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
Main Proc Far

    mov ax, data
    mov ds, ax
    mov ax, stack
    mov ss, ax

    ;Printing first message.
    ShowMsg msg1

    call GetNum

    call Average

    call Sort

    call Print

    mov ah, 4ch
    int 21h

Main endp

proc GetNum  

    mov bp, 0
    mov ch, 20

    NextNumber:
    NewLine

    mov cl, 6
    mov word ptr num, 0
    mov word ptr num+2, 0

    GetChar:    
    mov ah, 07h
    int 21h

    cmp al, 0dh
    jz Flag

    cmp al, 30h
    jb GetChar

    cmp al, 39h
    ja GetChar

    mov ah, 02h
    mov dl, al
    int 21h

    sub al, 30h

    mov bl, al
    mov di, 10
    mov ax, num
    mul di

    mov num, ax
    push dx

    mov ax, num+2
    mul di

    mov num+2, ax
    pop dx
    add num+2, dx

    mov bh, 0
    add num, bx
    adc word ptr num+2, 0

    dec cl
    jnz GetChar

    Flag:
    mov ax, num
    mov dx, num+2

    mov array[bp], ax
    mov array[bp+2], dx
    add bp, 4 

    add sum, ax
    adc sum+2, dx

    dec ch
    jnz NextNumber

    ret

GetNum endp

proc Average 

    mov bx, 20
    mov dx, 0
    mov ax, word ptr sum+2
    div bx 

    mov word ptr ave+2, ax
    mov ax, word ptr sum
    div bx
    mov word ptr ave, ax

    ShowMsg msg2

    mov cl, 0
    Next1:
    mov bx, 10
    mov dx, 0
    mov ax, word ptr ave+2
    div bx

    mov word ptr ave+2, ax 
    mov ax, word ptr ave
    div bx
    mov word ptr ave, ax

    push dx
    inc cl

    cmp ave, 0
    jnz Next1

    Next2:
    pop dx
    add dl, 30h
    mov ah, 02h
    int 21h

    dec cl
    jnz Next2

    NewLine

    ret

Average endp 

proc Sort 

    mov ch, 20

    OuterFor:
    mov bp, 0

    Cmp1:
    mov ax, array[bp+2] 
    mov bx, array[bp+6]

    cmp ax,bx
    ja Xchange 

    cmp ax,bx
    jz Cmp2

    jmp Next

    Cmp2:
    mov ax, array[bp]
    mov bx, array[bp+4]
    cmp ax, bx
    ja Xchange

    jmp Next

    Xchange:
    mov ax, array[bp]
    mov dx, array[bp+2]
    mov temp, ax
    mov temp+2, dx
    mov ax, array[bp+4]
    mov dx, array[bp+6]
    mov array[bp], ax
    mov array[bp+2], dx
    mov ax, temp
    mov dx, temp+2
    mov array[bp+4], ax
    mov array[bp+6], dx
    Next:
    add bp, 4
    cmp bp, 76
    jnz Cmp1 

    dec ch
    jnz OuterFor

    ret

Sort endp

proc Print 

    mov bp, 0
    C: 
    mov cl, 0
    A:
    mov bx, 10
    mov dx, 0
    mov ax, array[bp+2]
    div bx

    mov array[bp+2], ax 
    mov ax, array[bp]
    div bx
    mov array[bp], ax

    push dx
    inc cl
    mov ax, array[bp]
    mov dx, array[bp+2]
    or ax, dx
    jnz A

    B:
    pop dx
    add dl, 30h
    mov ah, 02h
    int 21h

    dec cl
    jnz B 

    add bp, 4

    NewLine

    cmp bp, 80
    jnz C

    ret
Print endp

code ends
end main

問題出在這兩行(可能在其他地方類似):

mov array[bp], ax
mov array[bp+2], dx

默認情況下, bp寄存器尋址stack段,而不是array所在的data段。 您必須使用另一個索引寄存器,或者使用

mov ds:array[bp], ax
mov ds:array[bp+2], dx

如果它可以處理少量元素,那么很幸運,沒有任何損壞可以使崩潰或破壞數據。

更新

我建議修改GetNum proc,以便您可以使用bx來索引array ,而不是bp

proc GetNum  
mov bx, 0
mov ch, 20

NextNumber:
push bx
NewLine
...
pop bx
mov array[bx], ax
mov array[bx+2], dx
add bx, 4 
...

與您的排序功能類似-交換bxbp的角色。 最好將bp用作通用寄存器,將bx用作索引寄存器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM