0

例:
num1 = 123 num2 = 24 num1 = 123
num2 = 24
プログラムは num1 の数字の合計を実行しています。この例では合計は 6 で、プログラムはどのように出力する必要がありますか?数字の合計が num2 に等しくなるように、何度も 90 を追加する必要があります。
この例では、1239090--> 1+2+3+9+0+9+0 = 24 であるため、90 を 2 回追加する必要があります。

ここで、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 配列に入れるにはどうすればよいでしょうか。
私のコードは次のとおりです。

.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

ヘルパーに感謝します!

4

1 に答える 1

1

数値が大きくなりすぎると、命令をカスケードする必要があります。ここでは、命令を使用できますadc。64 ビットの結果を表示します。

MulBy100 proc thevar:DWORD
  mov  ecx, 99        ;adding 99 times the number to itself = number*100
  mov  ebx, thevar    ;point to the var dword array
  mov  eax, [ebx]     ;edx:eax hold the num1 that we input (123 for example)
  mov  edx, [ebx+4]
 MulLoop:
  add  [ebx], eax     ;adding the number to itself 99 times
  adc  [ebx+4], edx
  loop MulLoop
 ret
MulBy100 endp

正しいコメントを書くように注意してください。100 倍するには99回足すだけです。

追加のレジスタを使用すると、このコードを簡単に変換して 128 ビットの結果をサポートできます。

于 2015-10-04T19:45:22.280 に答える