我需要在设定范围内生成随机数,并将其加载到数组中。然后,我需要将所有数字以及所有10个数字的总和打印到屏幕上。
我编写了整个代码,它可以正常运行,并且在调试过程中,我发现总和已正确存储在EAX中,但是在调用“求和”过程后,我看不到如何打印它。
我的代码:
include c:\asmio\asm32.inc
includelib c:\asmio\asm32.lib
includelib c:\asmio\user32.lib
includelib c:\asmio\kernel32.lib
; Proc name: Sum,Par-1: Pointer to array,Par-2: Array size
Sum proto pA: PTR dword,sz: dword
; ------------------------------------------------------------
.const
NULL = 0
SIX = 6
; -----------------------------------
.data ; Data section
number dword 10 dup(?) ; Array not initialized
Min dword 5
Max dword 37
msgC byte "Array cells: ",NULL
msgB byte " ",NULL
msgA byte "Sum: ",NULL
; -------------------------------------------------------------
.code
start:
main proc
call randomize
mov ecx,10 ; Sets the loop count
mov esi,OFFSET number
Next: mov eax,Max ; Loop starts here
sub eax,Min ; EAX = 37 - 5
call RandomRange ; generate 0 -> 32
add eax,Min ; shifted: 5 ->
mov [esi],eax ; Copies value to pointer location
add esi,TYPE dword ; Move pointer to next cell
loop Next
mov edx,OFFSET msgC ; Address of string must be in EDX
call WriteString ; Display string = cout in C++
mov ecx,10 ; Must be ECX. Set to loop 10 times
mov esi,OFFSET number ; esi -> Array
Parr: mov eax,[esi] ; Copy cell to EAX
call WriteDec ; only positive numbers
mov edx,OFFSET msgB ; Print blank
call WriteString
add esi,TYPE dword ; Advance pointer
loop Parr ; Repeat until ECX is zero
mov edx,OFFSET msgA ;
call WriteString
invoke Sum,number,LENGTHOF number
ret 0 ; Must return 0 in ASM
main endp ; Ends the main procedure
Sum proc uses ecx esi,pArray: PTR dword,Asz: dword
mov eax,0 ; EAX will keep sum,must set sum = 0
mov ecx,Asz ; Array size,number of times to loop
mov esi,pArray ; esi -> Array
L1: add eax,esi
add esi,TYPE dword
loop L1 ; Label to branch back to.
call WriteString
ret
Sum endp
end main ; Ends the whole program