News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Watching negative numbers?

Started by Nate523, September 20, 2024, 04:20:26 AM

Previous topic - Next topic

Nate523

Hi,

new to learning assembly, I am doing an example that has me step into the code and then use the watch window of Visual Studio 2022's to see that the value stored in RCX register is changed from 50 to -50 by using the NEG instruction.

Here is the code :


INCLUDELIB kernel32.lib
ExitProcess PROTO
.data

var QWORD 99                            ; initialize variable mem

.code
mainCRTStartup PROC

XOR RCX, RCX                            ; Clear registry, RCX = 0
XOR RDX, RDX                            ; Clear registry, RCX = 0
INC var ; Increment var by 1, var = 100
MOV RCX, 51 ; Assign reg/imm, RCX = 51
DEC RCX         ; Decrement RCX by 1, RCX = 50
NEG RCX ; Negate RCX, RCX = -50

MOV RCX, 78                             ; Did it exit afterwards?

call ExitProcess
mainCRTStartup ENDP
END


I then use the debugger, the exampe has me watch RCX, RDX, var, and also a "CL,bb", CL is the 8 bit least significant portion of RCX register correct? I do not recognize nor does the example explain what the 'bb' stands for. When I get the binary value of the 'CC,bb' the example has me drag the binary value into another watch item, which I am supposed to see -50 when I do so, but when I do it, it shows me -15474, which seems to be the value that was there before for that item. I attached a photo to show what I am seeing.

You cannot view this attachment.

Any ideas what I might be doing wrong to not see the signed value? For reference this example is from the book "Microsoft Assembly x64 Programming" in easy steps by Mike McGrath. Just learning assembly as a hobby to understand the CPU better and the gap between assembly and languages such as C/C++.

Thanks!

zedd151

"CL,bb"... seems some context is missing. I do not have the book, so I cannot look there.
:azn:

NoCforMe

No idea what CL,bb means, but one thing: if RCX contains -50, CL will definitely not contain that value; the negative number depends on the high bit (in this case bit 63) being set. The value doesn't translate down to the least-significant byte (CL).
Assembly language programming should be fun. That's why I do it.

Nate523

See if I can clarify the mystery maybe. The "CL,bb" I am referring to is in the Watch window during debugging in Visual Studio 2022, so far it looks like the 'bb' portion just shows binary value of the register rather than its numerical value because when I do RCX,bb it shows the binary value of RCX.

The book is having me step through the instructions via the debugger watching the registers with the Watch window, but when I execute the:

NEG RCX
portion of the code, it says to drag the value of the 'CL,bb' in the Watch window to the "Add item to watch" area, and it is supposed to show the signed value of RCX = -50 which they have a picture of showing it and sure enough it says -50 in their image. However, when I do it, I get -15474 which is confusing because the binary value I drag is "11001110" which is 206... so either I am not dragging and dropping correctly, or RCX is not getting negated, or some other newbie issue is occurring that is not apparent to me.

Quote from: NoCforMe on September 20, 2024, 05:53:03 AMNo idea what CL,bb means, but one thing: if RCX contains -50, CL will definitely not contain that value; the negative number depends on the high bit (in this case bit 63) being set. The value doesn't translate down to the least-significant byte (CL).

Confusing how the book has me look at CL then... so after I use the instruction as the image shows, RCX contains instead of the value 50 it turns into this : 18446744073709551566, which is also in their image, so I figured it was normal, maybe it isn't?

Tried to attach a better photo of the watch window

NoCforMe

You need a better understanding of binary arithmetic:

-50 (signed) = 4,294,967,246 (unsigned decimal)
             = FFFFFFFFFFFFFFCE (hex)

Separating the low byte (CL):
CE (hex) = 206 (unsigned decimal)
         = 11001110 (binary)

Which is what you see on the display for CL,bb, whatever that means.

I'm not sure what the book was getting at by telling you to look at CL for the value of the entire register, RCX. As I said, the two will never show the same value if the full register value is negative.

One thing to reduce confusion here: the numbers I gave you were from my little calculator which is limited to 32 bits, so I'm pretty sure that 18446744073709551566 is the unsigned decimal value of -50 in a 64-bit register. The CE hex value of the low byte is the same regardless.
Assembly language programming should be fun. That's why I do it.

Nate523

Quote from: NoCforMe on September 20, 2024, 06:14:07 AMYou need a better understanding of binary arithmetic:

-50 (signed) = 4,294,967,246 (unsigned decimal)
             = FFFFFFFFFFFFFFCE (hex)


Separating the low byte (CL):
CE (hex) = 206 (unsigned decimal)
         = 11001110 (binary)


Which is what you see on the display for CL,bb, whatever that means.

I'm not sure what the book was getting at by telling you to look at CL for the value of the entire register, RCX. As I said, the two will never show the same value if the full register value is negative.

One thing to reduce confusion here: the numbers I gave you were from my little calculator which is limited to 32 bits, so I'm pretty sure that 18446744073709551566 is the unsigned decimal value of -50 in a 64-bit register. The CE hex value of the low byte is the same regardless.

Okay, at least the values are making some sense then because there's another window I can look at and the hex value for RCX is indeed:

RCX = FFFFFFFFFFFFFFCE

Thank you for the last bit too because I was wondering why it was RCX = 18446744073709551566 and the difference between 32 bit and 64 bit in containing signed numbers I didn't understand before you mentioned it. I'm also confused as to what the books is referring too and thought maybe it was just a mistake on my end of configuring the watch window appropriately. This has helped though because now I can at least use the Windows calculator in programming mode to check hex values and such to see if the register actually has the number I put into it. Not sure why they have me view CL register.

NoCforMe

Quote from: Nate523 on September 20, 2024, 06:33:30 AMthe difference between 32 bit and 64 bit in containing signed numbers
In 32-bit land, -50 is FFFFFFCE. In 64-bit land it's FFFFFFFFFFFFFFCE. Two completely different numbers (at least in their unsigned values).
Assembly language programming should be fun. That's why I do it.

Nate523

Quote from: NoCforMe on September 20, 2024, 06:54:30 AM
Quote from: Nate523 on September 20, 2024, 06:33:30 AMthe difference between 32 bit and 64 bit in containing signed numbers
In 32-bit land, -50 is FFFFFFCE. In 64-bit land it's FFFFFFFFFFFFFFCE. Two completely different numbers (at least in their unsigned values).

Ahhh, and I am playing in 64 bit land, chalk that to more stuff I need to learn about and keep in my head.

sinsi

Quote from: NoCforMe on September 20, 2024, 06:14:07 AMSeparating the low byte (CL):
CE (hex) = 206 (unsigned decimal)
         = 11001110 (binary)


206 unsigned byte is -50 signed

CL,bb is a formatting option in the watch window. If you click "Add watch" and type in CL, when you type the comma it will bring up a list of formats. I didn't know about that, so thanks :eusa_clap:


NoCforMe

Quote from: sinsi on September 20, 2024, 08:04:03 AM206 unsigned byte is -50 signed

Thanks for the correction. Indeed it is. So I was wrong; as long as the value doesn't exceed the maximum (in the case of a negative number, minimum) value of an signed byte (-128), the byte register (CL) will contain the same value as the whole register (RCX). -50 will work, but -300 won't.
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: sinsi on September 20, 2024, 08:04:03 AMCL,bb is a formatting option in the watch window. If you click "Add watch" and type in CL, when you type the comma it will bring up a list of formats. I didn't know about that, so thanks :eusa_clap:
Mystery solved, for this at least. Thanks, sinsi.

Must be hard (for a newcomer) to learn 64 bit assembly, and how to use Visual Studio concurrently. @Nate523, Visual Studio is overkill, imo. It only adds complexity to an already complex endeavor (learning 64 bit assembly).
:azn:

ognil

 Hi zedd151, :smiley:

Quoteby zedd151:  Must be hard (for a newcomer) to learn 64 bit assembly, and how to use Visual Studio concurrently.

Does this mean that the old member (the smart one) will find it easy to learn 64-bit assembler, while the newcomer (the stupid one) will find it difficult?  Isn't that some different kind of "racism"?

Quote"Visual Studio is overkill, imo. It only adds complexity to an already complex endeavor (learning 64 bit assembly).

Why do you think so, if you never learn and use 64 bit assembly with Visual Studio? If I am wrong pls, show us your code!

What is your opinion where better are you going to learn 64bit assembler with Visual Studio:
on this site or at university?

Thank you! :thup:

NoCforMe

Quote from: ognil on Today at 07:44:34 AM
Quoteby zedd151:  Must be hard (for a newcomer) to learn 64 bit assembly, and how to use Visual Studio concurrently.

Does this mean that the old member (the smart one) will find it easy to learn 64-bit assembler, while the newcomer (the stupid one) will find it difficult?  Isn't that some different kind of "racism"?

No, even the "smart ones" (I'm no genius but I'm not stupid either) may find it somewhat difficult to learn 64-bit programming (because of all the quirks in the ABI), even ones like me who are pretty proficient at 32-bit. Your "-ism" observation is noted, but probably doesn't apply here.

QuoteWhat is your opinion where better are you going to learn 64bit assembler with Visual Studio:
on this site or at university?

Lots of folks here write 64-bit code without any use of Visual Studio. All the tools are here.
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: ognil on Today at 07:44:34 AMon this site or at university?

The level of university courses is laughable. Some of us have Assembly sources in the tens of thousands of lines.

ognil

Thank you NoCforMe, :thumbsup: 

Quoteby NoCforMe: Lots of folks here write 64-bit code without any use of Visual Studio. All the tools are here.

Pls, change "Lots of" with "Some" and "All the tools are there." :thup:

Visual Studio was built and maintained by a large and experienced staff and has more 400 AI-infused extensions.

Thanks. You've just made my day. :badgrin:  :badgrin: