装配程序未运行Intel 8086

时间:2021-12-26 03:18:40

The code below, is supposed to get 20 user-entered numbers (6 digit numbers or less) and compute the average as well as sorting them, When I set it to get 6 or less numbers, it works fine. But when it is set to get 7-20 numbers, after getting the numbers, it skips the next procedures and someimes runs the GetNum procedure ( The one that gets the numbers from user) again and when it gets 11 numbers, I get this message "PROGRAM HAS RETURNED CONTROL TO THE OPERATING SYSTEM".

下面的代码应该得到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

1 个解决方案

#1


The problem lies with these two lines (and possibly similar elsewhere):

问题在于这两条线(并且可能在其他地方类似):

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

By default, the bp register addresses the stack segment, not the data segment where array is. You must either use another index register, or over ride the segment with

默认情况下,bp寄存器用于寻址堆栈段,而不是数组所在的数据段。您必须使用另一个索引寄存器,或者使用

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

If it worked with a small number of elements, that was by luck that nothing was corrupted to make a crash or spoil the data.

如果它与少量元素一起使用,那就是幸运的是,没有任何东西因为崩溃或破坏数据而被破坏。

UPDATE

I would suggest modifying the GetNum proc so you can use bx to index array, instead of bp.

我建议修改GetNum proc,这样你就可以使用bx来索引数组,而不是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 
...

Similarly with your sorting function - swap the roles of bx and bp. It it better to use bp as a general purpose register and bx as an indexing register.

与您的排序功能类似 - 交换bx和bp的角色。最好使用bp作为通用寄存器,bx作为索引寄存器。

#1


The problem lies with these two lines (and possibly similar elsewhere):

问题在于这两条线(并且可能在其他地方类似):

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

By default, the bp register addresses the stack segment, not the data segment where array is. You must either use another index register, or over ride the segment with

默认情况下,bp寄存器用于寻址堆栈段,而不是数组所在的数据段。您必须使用另一个索引寄存器,或者使用

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

If it worked with a small number of elements, that was by luck that nothing was corrupted to make a crash or spoil the data.

如果它与少量元素一起使用,那就是幸运的是,没有任何东西因为崩溃或破坏数据而被破坏。

UPDATE

I would suggest modifying the GetNum proc so you can use bx to index array, instead of bp.

我建议修改GetNum proc,这样你就可以使用bx来索引数组,而不是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 
...

Similarly with your sorting function - swap the roles of bx and bp. It it better to use bp as a general purpose register and bx as an indexing register.

与您的排序功能类似 - 交换bx和bp的角色。最好使用bp作为通用寄存器,bx作为索引寄存器。