开发者

How to add number to the unsigned dword with 31bit set to 1 in assembly so it wouldn't be treated as negative?

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 end开发者_运维知识库s

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜