2

我正在尝试在 MASM32 中将 5 添加到 3234567890。这是完整的示例代码:

;--------------------------------------------------------------------------
include \masm32\include\masm32rt.inc
.data
;--------------------------------------------------------------------------
.code

start: 
call main                   ; branch to the "main" procedure
exit
main proc
local pbuf: DWORD
local buffer[32]: BYTE

mov pbuf, ptr$(buffer)

mov ecx, uval("5") ; converting string to unsigned dword and storing in ecx
mov ebx, uval("3234567890") ;  converting string to unsigned dword and storing in ebx

invoke udw2str, ebx, pbuf ; converting unsigned value to string and storing results in pbuf
print pbuf, 13,10 ; everything is fine so far - 3234567890

add ecx, ebx

invoke udw2str, ebx, pbuf ; once again coverting 
print pbuf, 13,10 ; negative number

ret

main endp    
end start                       ; Tell MASM where the program ends

向 unsigned dword 添加内容的正确方法是什么?现在我得到负数,预期结果是 3234567895。

更新: 问题确实出在使用的宏中。我已经将样本编辑到最低限度并且它工作正常。这里没有什么神秘之处。:)

;--------------------------------------------------------------------------
include \masm32\include\masm32rt.inc
.data
;--------------------------------------------------------------------------
.code

start: 
call main                   ; branch to the "main" procedure
exit
main proc
local pbuf: DWORD
local buffer[40]: BYTE
local nNumber: DWORD

mov pbuf, ptr$(buffer)

mov ecx, 5 ; no need to convert anything at this point
mov ebx, 3234567890 ;  no need to convert anything at this point

add ebx, ecx

invoke udw2str, ebx, pbuf ; now converting result in ebx to the string (pointed by pbuf)
print pbuf, 13, 10 ; printing pbuf, success

ret

main endp    
end start                       ; Tell MASM where the program ends

感谢大家!

4

1 回答 1

1

在这个级别,有符号和无符号实际上是一回事,除了乘法和除法指令,所以加法在这里没有错。

我能想到的可能问题:

  1. 加法的结果真的在ebx吗?由于广泛使用两种不同的约定,目标寄存器是哪个操作数存在很大的混淆?(即使这是问题的一部分,它并不能真正解释结果,因为这会给出 5,而不是负数,但仍然......)

  2. 这个论坛帖子讨论了 udw2str 中的一个实现问题。

  3. 您正在pbuf用作输出缓冲区,但这还不够大。您依赖于将它放在内存中之前的汇编器buffer

  4. 也许会print破坏ebx?

此时我会拿出我信任的调试器并单步执行代码。

于 2011-01-17T16:10:32.453 回答