【问题标题】:x86 assembly (masm32) output multiplied number produces junk charactersx86 程序集(masm32)输出乘数产生垃圾字符
【发布时间】:2012-08-20 02:35:49
【问题描述】:

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

.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,的东西。

问题:为什么会输出那个随机字符串,我该如何解决?

提前致谢。

【问题讨论】:

  • 我没有看到任何将结果(数字)转换为字符串的代码,因此可以打印出来。 StdOut(可能)不是一个函数,因此调用它会获取一些半随机值,将其视为一个地址,然后调用该地址——几乎可以肯定不是你想要的。

标签: assembly x86 masm32


【解决方案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 

【讨论】:

  • 谢谢,效果很好。出于兴趣,你为什么需要做push offset lpBuffer? inkey 是什么?
【解决方案2】:

您的输出由StdOut 函数中的任何内容控制,您没有向我们展示。很有可能(根据您的描述),它在累加器中获取字符 code 并输出等效字符。

如果是这种情况,您可能需要做的是获取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 的实际作用并调整您的代码以满足该功能的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2015-04-29
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多