0

几个月后,我将为此返回组装,但我无法让两个数字相乘并输出结果。这是我的代码:

.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 回答