It is currently Wed Jan 20, 2021 3:35 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Unsigned 32-bit integers
PostPosted: Wed Nov 27, 2013 5:40 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 12, 2008 1:25 pm
Posts: 374
Location: The Netherlands
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned 32-bit integers
PostPosted: Wed Nov 27, 2013 6:10 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3710
Location: Netherlands
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:
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)

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Unsigned 32-bit integers
PostPosted: Wed Nov 27, 2013 7:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 12, 2008 1:25 pm
Posts: 374
Location: The Netherlands
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!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye