Unsigned 32-bit integers

Bare metal programming in PureBasic, for experienced users
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Unsigned 32-bit integers

Post by Crusiatus Black »

Hi there community!

I've been playing around with inline assembly more and more lately, and I've
been wondering; How can I perform several calculations in fASM and keep the
working variables unsigned? I don't care if the end result which will be returned
to PureBasic will be a signed integer, but how do I keep it all unsigned in ASM?

e.g. I'm reading a byte from a buffer, I always want it to be unsigned. Then, for
example, I would add this byte value to an int (let's say it's a hashing function),
how do I keep the int (EAX) unsigned throughout the calculations with common
arithmetic / bitwise operations? It's quite problematic when it comes to addition
and subtraction.

It could be something so obvious and I'm missing a world of simple facts here, but
I'd love some light being shed on my question.

Thanks all, have a nice day!
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Unsigned 32-bit integers

Post by wilbert »

It depends on what functionality you need.
For a shift to the right for example, there's SHR (unsigned) and there's SAR (signed).
For multiplication, there's MUL (unsigned) and there's IMUL (signed).
For division, there's DIV (unsigned) and there's IDIV (signed).
When loading a smaller type (byte or word) into a 32 bit register, there's MOVZX (zero extended / unsigned) and there's MOVSX (sign extended / signed).
So to work with unsigned variables, you use different opcodes compared to the ones you use for signed variables.

Code: Select all

Procedure AddSignedByteToLong(long.l, byte.b)
  !mov eax, [p.v_long]
  !movsx ecx, byte [p.v_byte]
  !add eax, ecx
  ProcedureReturn
EndProcedure

Procedure AddUnsignedByteToLong(long.l, byte.b)
  !mov eax, [p.v_long]
  !movzx ecx, byte [p.v_byte]
  !add eax, ecx
  ProcedureReturn
EndProcedure

Debug AddSignedByteToLong(1000, $11)
Debug AddSignedByteToLong(1000, $ee)
Debug AddUnsignedByteToLong(1000, $11)
Debug AddUnsignedByteToLong(1000, $ee)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Unsigned 32-bit integers

Post by Crusiatus Black »

Great, thanks for the info Wilbert! Now I know what to look for, I'll read
up on these instructions a bit more.

Cheers / een fijne avond nog! :)
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Post Reply