使用循环计算学生平均值

calculating students avg using loop

这个程序应该计算学生的平均成绩。 我正在使用一个循环让用户输入 5 个等级。 我试图将 div 部分保留在循环之外,但它没有用。 我不得不把它放在循环中,尽管从逻辑上讲并不能使它始终打印 1.

代码如下:

include irvine32.inc

; Write a MASM program that calculates the avg grade of a student(Five grades are inputted by the user).

.data

mesg byte "     *********This program calculates average grades of students *********", 0dh, 0ah, 0
mesg1 byte "Enter 5 grades: ", 0dh, 0ah, 0
mesg2 byte "The average grade is: ", 0dh, 0ah, 0

grade1 dword ?
grade2 dword ?
grade3 dword ?
grade4 dword ?
grade5 dword ?


.code
main proc

mov eax, 0

mov edx, OFFSET mesg
call writestring

mov edx, OFFSET mesg1
call writestring

mov ecx, 5

myLoop :

call readint
mov grade1, eax
call readint
mov grade2, eax
call readint
mov grade3, eax
call readint
mov grade4, eax
call readint
mov grade5, eax

add eax, grade1
add eax, grade2
add eax, grade3
add eax, grade4
mov grade5, eax

call writeint

call dumpregs

    LOOP myLoop

mov edx, OFFSET mesg2
call writestring

mov eax, grade5
mov cl, 5
sub edx, edx
div cl

call writeint
call dumpregs


exit
main endp
end main

正在计算平均值,循环内有 div

include irvine32.inc

; Write a MASM program that calculates the avg grade of a student(Five grades are inputted by the user).

.data

mesg byte "     *********This program calculates average grades of students *********", 0dh, 0ah, 0
mesg1 byte "Enter 5 grades: ", 0dh, 0ah, 0
mesg2 byte "The average grade is: ", 0dh, 0ah, 0

grade1 dword ?
grade2 dword ?
grade3 dword ?
grade4 dword ?
grade5 dword ?


.code
main proc

    mov eax, 0

    mov edx, OFFSET mesg
    call writestring

    mov edx, OFFSET mesg1
    call writestring

    mov ecx, 5

myLoop:
    call readint
    mov grade1, eax
    call readint
    mov grade2, eax
    call readint
    mov grade3, eax
    call readint
    mov grade4, eax
    call readint
    mov grade5, eax

    add eax, grade1
    add eax, grade2
    add eax, grade3
    add eax, grade4
    mov grade5, eax

    call writeint

    call dumpregs


    mov edx, OFFSET mesg2
    call writestring

    mov eax, grade5
    mov ebx, 5
    sub edx, edx
    div ebx

    call writeint
    call dumpregs

    LOOP myLoop


    exit
main endp
end main

你的第二个版本唯一明显的问题是你没有使用 grade5。也许尝试 add grade5, eax,而不是 mov grade5, eax,但这不会将 WriteInt 的总和留在 eax 中。

使用调试器单步执行代码。如果您还有什么不明白的地方,请更新您的问题,具体说明您希望您的代码做什么,以及 确切地 它实际上在做什么。问题是什么指令,如果你能用调试器缩小范围的话。

add eax, grade1
add eax, grade2
add eax, grade3
add eax, grade4
mov grade5, eax

上面的代码有两个问题。你一开始没有清除 EAX 寄存器,你忘了在 5 年级添加。更正如下:

mov eax, grade1  <-- MOV = start with a clean EAX
add eax, grade2
add eax, grade3
add eax, grade4
add eax, grade5
mov grade5, eax

mov eax, grade5
mov cl, 5
sub edx, edx
div cl

要计算平均值,您需要将 EAX 中的双字除以 5,但是通过编写 div cl,您只需要将 AX 中的字除以。最好使用以下内容:

mov eax, grade5
mov ecx, 5
sub edx, edx    ;Will divide EDX:EAX by ECX
div ecx         ;Quotient in EAX

I had to put it inside the loop although logically doesn't make sense

为 5 个学生每人输入 5 个成绩确实有意义。
没有意义的是第一个程序的 myLoop 循环。它只会给出最后一组 5 个成绩的平均值!