0

数か月後にアセンブリに戻ってきましたが、2 つの数値を乗算して結果を出力するのに問題があります。これが私のコードです:

.386
.model flat, stdcall 
option casemap :none 

include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\masm32.inc 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\masm32.lib

.data 
     sum sdword 0
.code 
start:
mov ecx, 6        
xor eax, eax                  
mov edx, 7               
mul edx                  
push eax
pop sum
lea eax, sum
call StdOut
push 0 
call ExitProcess
end start 

のようなものを出力しますP &aeffiini,

質問: ランダムな文字列が出力されるのはなぜですか? どうすれば修正できますか?

前もって感謝します。

4

2 に答える 2

1

StdOut は数値ではなく NULL で終了する文字列を出力するためです。最初に数値を文字列に変換する必要があります。MASM32 には dwtoa があります。また、あなたの掛け算が間違っています。あなたはeaxで掛けます

include masm32rt.inc

.data?
lpBuffer    db 12 dup (?)

.code 
start:
    mov     ecx, 6        
    mov     eax, 7               
    mul     eax    

    push    offset lpBuffer
    push    eax
    call    dwtoa 

    push    offset lpBuffer             
    call    StdOut

    inkey
    push    0 
    call    ExitProcess
end start 
于 2012-08-20T02:56:01.380 に答える
0

あなたの出力は、StdOutあなたが私たちに示していない関数にあるものによって制御されます。おそらく(あなたの説明を考えると)、アキュムレータの文字コードを取得し、同等の文字を出力します。

その場合、おそらく行う必要があるのは、値を取り込んで、eaxそれに基づいてどの文字を出力する必要があるかを判断することです。次に、StdOutそれぞれを呼び出します。

そのための擬似コードは次のようになります。

eax <- value to print
call print_num
exit

print_num:
    compare eax with 0
    branch if not equal, to non_zero

    eax <- hex 30                       ; Number was zero,
    jump to StdOut                      ;   just print single digit 0

non_zero:
    push eax, ebx, ecx                  ; Save registers that we use
    ecx <- 0                            ; Digit count
loop1:
    compare eax with 0                  ; Continue until digits all pushed
    jump if equal, to num_pushed

    increment ecx                       ; Another digit
    ebx <- eax                          ; Work out what it is
    eax <- eax / 10
    eax <- eax * 10
    ebx <- ebx - eax
    push ebx to stack                   ; Then push it,
    jump to loop1                       ;  and carry on

num_pushed:
    pop eax from stack                  ; Get next digit
    eax <- eax + hex 30                 ; Print it out
    call StdOut
    decrement ecx                       ; One less digit to go
    jump if not zero, to num_pushed     ; Continue if more

    pop ecx, ebx, eax from stack        ; Clean up stack and return
    return

そうでない場合は、実際に何が行われているかを把握しStdOut、その機能の要件を満たすようにコードを調整する必要があります。

于 2012-08-20T02:54:29.687 に答える