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!
Unsigned 32-bit integers
- Crusiatus Black
- Enthusiast
- Posts: 389
- Joined: Mon May 12, 2008 1:25 pm
- Location: The Netherlands
- Contact:
Re: Unsigned 32-bit integers
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.
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)
Raspberry Pi OS (Arm64)
- Crusiatus Black
- Enthusiast
- Posts: 389
- Joined: Mon May 12, 2008 1:25 pm
- Location: The Netherlands
- Contact:
Re: Unsigned 32-bit integers
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!
up on these instructions a bit more.
Cheers / een fijne avond nog!