如何在汇编中将数字添加到 31 位设置为 1 的无符号双字中,这样它就不会被视为负数?

发布于 2024-10-12 09:58:54 字数 1834 浏览 0 评论 0原文

我正在尝试在 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

向无符号双字添加内容的正确方法是什么? 现在我得到负数,预期结果是 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

谢谢大家!

I am trying to add 5 to 3234567890 in MASM32.
Here is the full sample code:

;--------------------------------------------------------------------------
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

What is the right way to add something to the unsigned dword?
Right now i am getting negative number and expected result is 3234567895.

Update:
Problem was indeed somewhere in the MACRO used. I've edited sample to the bare minimum and it worked correctly. No mystery here. :)

;--------------------------------------------------------------------------
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

Thanks everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2024-10-19 09:58:54

在这个级别上,除了乘法和除法指令之外,有符号和无符号实际上是同一件事,因此这里的加法没有问题。

我能想到的可能问题:

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

  2. (即使这是问题的一部分,它 masm32.com/board/index.php?PHPSESSID=68210a7bd7a93034b130ed103b37cae5&topic=15427.0" rel="nofollow" title="关于“udw2str”算法不健全的通知。">此论坛帖子讨论了实现 的问题。
  3. 您正在使用pbuf作为输出缓冲区,但它不够大。您依赖于汇编器将其放置在紧邻 buffer 之前的内存中。

  4. print 是否可能破坏 ebx?

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

At this level, signed and unsigned are really the same thing, except for multiply and divide instructions, so the addition is not at fault here.

Possible problems I can think of:

  1. is the result of the addition really in ebx? There is significant confusion about which operand is the destination register, as there are two different conventions in widespread use? (even if that is part of the problem, it doesn't really explain the result, as that would give 5, not a negative number, but still...)

  2. this forum post talks about an implementation issue in udw2str.

  3. you are using pbuf as the output buffer, but that is not large enough. You are relying on the assembler placing it in memory immediately before buffer.

  4. does print perhaps clobber ebx?

I'd pull out my trusty debugger at this point and single-step through the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文