我已经绞尽脑汁好几天了,仍在积极调试。我正在尝试计算几个数字的平均值并将结果向上或向下舍入。以下是必要的变量声明:
totalNum DWORD ?
sum DWORD ? ; sum of numbers
avg DWORD ?
rem DWORD ?
decimal REAL4 0.5
代码片段:
; ********************
; * calculateAverage *
; ********************
mov eax, sum
mov ebx, totalNum
cdq
div ebx
mov avg, eax ; save average
mov rem, edx ; save remainder
mov eax, rem ; divide divisor (totalNum) by remainder to round up/down
mov ebx, totalNum
cdq
div ebx
cmp eax, decimal ; compare result to .5
jge roundUp ; if >= .5, jump to roundUp
jmp endRoundUp
roundUp: ; round up average
add avg, 1
endRoundUp:
; ******************
; * displayResults *
; ******************
mov edx, OFFSET countText1 ; display total numbers entered
call WriteString
mov eax, totalNum
call WriteDec
mov edx, OFFSET countText2
call WriteString
call Crlf
mov edx, OFFSET sumText ; display sum of numbers entered
call WriteString
mov eax, sum
call WriteDec
call Crlf
mov edx, OFFSET avgText ; display rounded average of numbers entered
call WriteString
mov eax, avg
call WriteDec
当我尝试仅以decimal显示值时,我mov eax, decimal并获得一个 10 位数字。该程序正确计算总和和总数,甚至正确计算平均值,但我无法正确舍入或显示浮点数。