如何在汇编中将数字添加到 31 位设置为 1 的无符号双字中,这样它就不会被视为负数?
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这个级别上,除了乘法和除法指令之外,有符号和无符号实际上是同一件事,因此这里的加法没有问题。
我能想到的可能问题:
加法的结果真的在
ebx
中吗?由于广泛使用两种不同的约定,因此对于哪个操作数是目标寄存器存在严重的混淆? (即使这是问题的一部分,它并不能真正解释结果,因为这会得到 5,不是负数,但仍然......)您正在使用
pbuf
作为输出缓冲区,但它不够大。您依赖于汇编器将其放置在紧邻buffer
之前的内存中。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:
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...)this forum post talks about an implementation issue in udw2str.
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 beforebuffer
.does
print
perhaps clobber ebx?I'd pull out my trusty debugger at this point and single-step through the code.