程序集新手请求帮助:有符号整数除法 + "truncating to an integer"



几天前我问了一个关于这段代码的问题,所以你们中的一些人可能会觉得很熟悉。对于那些不熟悉它的人来说,这段代码应该做的是向用户请求25个有符号整数,并将它们存储到一个数组中;这是通过requestSignedInts子例程完成的(我确信这部分工作正常)。然后,"calcMean"子例程应该将数组中的所有值相加,除以数组中的元素数,然后"将结果截断为整数"。这就是我被卡住的地方。我试图编写一个calcMean子例程,它将执行我刚才描述的操作,但似乎无法正确地执行除法。最重要的是,我甚至不确定我的calcMean子程序中目前的内容是否能正常工作。有人能提供帮助吗?

INCLUDE    c:irvineirvine32.inc
INCLUDELIB c:irvineirvine32.lib
INCLUDELIB c:masm32libuser32.lib
INCLUDELIB c:masm32libkernel32.lib
.data
theSINTArray BYTE 25 dup(?)
lengthOfArray BYTE ?
indexCounter BYTE 0              
prompt BYTE "Please enter a value: ",0

.CODE
main PROC
    call    requestSignedInts
    call    calculateMean
    exit
main ENDP
requestSignedInts PROC
    mov     edx, offset theSINTArray
Next:
    push    edx                                        
    mov     edx,OFFSET prompt                          
    call    WriteString                                
    call    ReadInt
    pop     edx 
    mov     [edx], al
    inc     edx
    cmp     edx, offset theSINTArray + 25
    jb      Next
    ret
requestSignedInts ENDP

calculateMean PROC
push ecx
mov    ecx,lengthOfArray - theSINTArray ; Determine array length    
xor    eax, eax                         ; Clear EAX
mov    esi, OFFSET theSINTArray         ; Starting point for index into array
calcMean:
movsx  edx, byte ptr[esi]               ; Sign extended move a byte into EDX
add    eax, edx                         ; Accumulate in EAX
inc    esi                              ; Increment source pointer to the next element
loop   calcMean                         ; or cmp esi,endOfArray / jb, then you wouldn't need to touch ecx
mov    ecx,lengthOfArray - theSINTArray ; Determine array length    
cdq                                     ; sign-extend eax into edx:eax
idiv   ecx                              ; Divide edx:eax by ecx
                                        ; eax now contains the integer and edx contains
                                        ; the remainder.
pop    ecx
ret
calculateMean ENDP
END     main

在我看来,您可能缺少注释中提到的idiv的功能。为什么不:

calculateMean PROC
push ecx
mov    ecx,lengthOfArray - theSINTArray ; Determine array length    
xor    eax, eax                         ; Clear EAX
mov    esi, theSINTArray                ; Starting point for index into array
calcMean:
movsx  edx, byte ptr[esi]               ; Sign extended move a byte into EDX
add    eax, edx                         ; Accumulate in EAX
inc    esi                              ; Increment source pointer to the next element
loop   calcMean                         ; or cmp esi,endOfArray / jb, then you wouldn't need to touch ecx
mov    ecx,lengthOfArray - theSINTArray ; Determine array length    
cdq                                     ; sign-extend eax into edx:eax
idiv   ecx                              ; Divide edx:eax by ecx
                                        ; eax now contains the integer and edx contains
                                        ; the remainder.
pop    ecx
ret
calculateMean ENDP

您已经知道列表的长度,只需清除edx并除以ecx即可。这也正确地使用movsx将字节(从数组)符号扩展移动到32位寄存器(edx)中。将总和累加为eax,最后,我们进行符号扩展和除法。

与David的回答相比,您的原始代码还有更多奇怪之处。我发布了一个新的答案,而不是进一步编辑大卫的答案。

注释中注明了更改。删除了旧的注释,因此更改注释非常突出。

.data
SBYTEArray BYTE 25 dup(?)     ; SINT = signed int, which makes most x86 C programmers think 32bit
; lengthOfArray BYTE ?        ; totally bogus: named wrong, and there doesn't need to be any storage there
; SBYTEArray_len equ $ - SBYTEArray  ; that's NASM syntax and IDK the MASM syntax
SBYTEArray_end:                  ; SBYTEArray_end and prompt have the same address.  That's fine.
prompt BYTE "Please enter a value: ",0      ; If MASM/Windows has a .rodata section, you should put constant data there.  It goes in the text section, along with code, because each instance of your process doesn't need a private copy.

.CODE
main PROC
    call    requestSignedInts
    call    calculateMean
    exit
main ENDP
requestSignedInts PROC
    ; indent code one level deeper than labels / directives
    push    esi                     ; save a call-preserved reg
    mov     esi, offset SBYTEArray
Next:
    mov     edx,OFFSET prompt
    call    WriteString
    call    ReadInt
    mov     [esi], al
    inc     esi
    ; cmp     edx, offset SBYTEArray + 25  ; hard-coding the size defeats the purpose of defining lengthOfArray
    cmp     esi, offset SBYTEArray_end
    jb      Next
    pop     esi          ; note that the push/pop are outside the loop
    ret
requestSignedInts ENDP

calculateMean PROC
    ; push ecx          ; The normal function-call ABIs allow clobbering ecx, and I don't see any reason to make this function go beyond the ABI requirements (although that is an option in asm)
    ; push   esi          ; esi *is* call-preserved in the standard 32bit ABIs.  But by changing our function to use fewer registers, we can avoid the save/restore
    xor    eax, eax                         ; start with sum=0
    mov    ecx, offset SBYTEArray
calcMean:
    movsx  edx, byte ptr[ecx]
    add    eax, edx
    inc    ecx
    cmp    ecx, offset SBYTEArray_end
    jb     calcMean                         ; loop while our pointer is below the end pointer
    mov    ecx, SBYTEArray_end - SBYTEArray ; Determine array length.  Does this need OFFSET?
    cdq
    idiv   ecx
    ; pop esi         ; we ended up not needing it
    ret
calculateMean ENDP
END Main

loop很慢,请避免它。特别是当您可以通过使用其他方法来保存寄存器以满足循环条件时。

最新更新