Assembly x86程序集(masm32)输出乘以数字会生成垃圾字符

Assembly x86程序集(masm32)输出乘以数字会生成垃圾字符,assembly,x86,masm32,Assembly,X86,Masm32,几个月后,为了这个目的,我又回到了汇编程序中,我很难让两个数字相乘并输出结果。这是我的密码: .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 inclu

几个月后,为了这个目的,我又回到了汇编程序中,我很难让两个数字相乘并输出结果。这是我的密码:

.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&aeffini,

问题:为什么它会输出那个随机字符串,我如何修复它


提前感谢。

您的输出由
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

eax因为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 

我没有看到任何代码将结果(一个数字)转换为字符串,这样就可以打印出来了
StdOut(可能)不是一个函数,因此调用它需要一些半随机值,将其视为一个地址,然后调用该地址——几乎肯定不是您想要的。谢谢,这非常有效。出于兴趣,为什么需要执行
推送偏移lpBuffer
?什么是英基?