我正在尝试做一个需要 2 个数字的程序,例如:
num1 = 123
num2 = 24
程序正在对 num1 中的数字求和,在这个例子中,总和是 6,程序需要打印如何很多时候我需要加 90,数字的总和将等于 num2。
在本例中,我们需要将 90 添加两次,因为:1239090--> 1+2+3+9+0+9+0 = 24
现在,我加 90 的方法是取 num1,乘以 100 并加 90:
123*100 = 12300 --------> 12300+90 = 12390(第一次)
我的问题是当输入是这样的:
num1 = 123
num2 = 42
现在, num1 最后应该是 = 123 90 90 90 90 ,它大于 dword 大小!
现在我认为我需要一个 dword 数组,所以如果结果大于 dword 大小,它将把结果的其余部分放在数组的下一个 dword 单元中。但我不知道该怎么做,我试图通过指向EBX
数组的来放入结果offset
,然后放入[EBX]
值,但发生的是它只是压缩了第一个 dword 单元格中的值大批。
那么如何将大于 DWORD 大小的值放入 dword 数组中?
我的代码是:
.386
.MODEL Flat, STDCALL
option casemap:none
SomeFunc proto :DWORD
MulBy100 proto :DWORD
include \masm32\include\windows.inc
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib
.data
var dd 30 dup (0) ;The array to put the result
count dd 0 ;count how many 90 to add
SumHash dd 0
SumDigits dd 0
fmt2 db '%d',0
.code
ReturnHashCode proc Number:DWORD
mov SumHash,0
Lop:
mov eax,Number
mov ebx,10
xor edx,edx
div ebx
add SumHash,edx ;num%10 add to SumHash (123-->3)
mov Number,eax ;num/10 go to Number (123-->12)
cmp Number,0 ;check if there is no more digits to add
ja Lop
ret
ReturnHashCode endp
MulBy100 proc thevar:DWORD
mov ecx,99 ;adding 100 times the number to itself = number*100
mov ebx,thevar ;point to the var dword array
mov eax,[ebx] ;eax hold the num1 that we input (123 for example)
MulLoop:
add [ebx],eax ;adding the number to itself 100 times
loop MulLoop
ret
MulBy100 endp
start:
INVOKE crt_scanf,offset fmt2,offset var ;in this example = 123
invoke crt_scanf,offset fmt2,offset SumDigits ;in this example = 24
countNop:
invoke ReturnHashCode,var ;return sum of digits to SumHash variable
mov eax,SumDigits ;in this example = 24
cmp eax,SumHash
je End_countNop
push offset var ;send the pointer to the var array dword size that suppost to hold the value (1239090....90)
call MulBy100 ;multiply by 100
add var,90 ;add 90
inc count ;add to count 1 because we add 90
jmp countNop
End_countNop:
invoke crt_printf,offset fmt2, count ;printing how many times we added 90
end start
感谢帮助者!