x86程序集(masm32)输出相乘的数字会产生垃圾字符

时间:2012-08-20 02:35:50

标签: 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 
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,

的内容

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

提前致谢。

2 个答案:

答案 0 :(得分:1)

因为StdOut打印NULL终止字符串NOT数字。您需要先将数字转换为字符串。 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 

答案 1 :(得分: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实际执行的操作,并调整代码以满足该功能的要求。