It is currently Mon Sep 23, 2019 8:58 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: 64bit quad addition on x86 cpu
PostPosted: Thu Oct 13, 2016 1:57 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
im just wondering how to do 64bit addition on 32bit cpu, i came up with the following which seems to work ok (although im not sure if sign/unsigned is an issue). just wondering if this is the way to go? (can 'adc' be used here?)
Code:
Procedure Test() 
  Protected quad1.q, *quad1 = @quad1 
  Quad1 = $FFFFFFF0          ;nearly overflowing lower 32bit
 
  Debug "Before = 0x" + Hex(Quad1) + " / " + Str(Quad1)             ; 0xFFFFFFF0 / 4294967280
 
  ! mov eax, [p.p_quad1]    ;eax = lower 32bits of quad
  ! add dword [eax], 20     ;add to it, overflowing lower 32bit
  ! setc dl                 ;dl=carry flag, now set from overflow
  ! add [eax+4], dl         ;increment the upper 32bits if dl was set
 
  Debug "After  = 0x" + Hex(Quad1) + " / " + Str(Quad1)             ;0x100000004 / 4294967300
EndProcedure

Test()

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: 64bit quad addition on x86 cpu
PostPosted: Thu Oct 13, 2016 4:23 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3443
Location: Netherlands
Usually add/adc is used for addition and sub/sbb for subtraction.
Code:
  ! mov eax, [p.p_quad1]    ;eax = lower 32bits of quad
  ! add dword [eax], 20     ;add to it, overflowing lower 32bit
  ! adc dword [eax+4], 0

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: 64bit quad addition on x86 cpu
PostPosted: Thu Oct 13, 2016 5:21 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
sweet, thanks! glad i was on the right track at least hehe
Note to future self: i just observed that while ADD sets the Carry flag, INC doesnt - have to check the Zero flag instead! I guess that makes sense because 0 isn't a carry, but Add still sets CF=1 even when it goes to 0

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: 64bit quad addition on x86 cpu
PostPosted: Thu Oct 13, 2016 6:06 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3443
Location: Netherlands
Keya wrote:
i just observed that while ADD sets the Carry flag, INC doesnt

INC and DEC both leave the carry flag untouched.
This way (without changing the carry flag result) you can create a loop with the ADC instruction in it.
Now you are only asking for 64 bit addition but by using a loop, you can also add 128 or 256 bit values together (or even bigger).
The same goes for shifting an entire memory area one bit to the left or right. When using INC / DEC for the loop, you can use a rotate with carry instruction like RCL / RCR inside the loop.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: 64bit quad addition on x86 cpu
PostPosted: Thu Oct 13, 2016 6:40 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
ahh, very cool!! yes this did make me think about big number libraries - it seemed easy enough to extend add/sub to more than 64bits, but extending the other operators seems a bit daunting :D

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: 64bit quad addition on x86 cpu
PostPosted: Thu Oct 13, 2016 7:34 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3443
Location: Netherlands
Keya wrote:
it seemed easy enough to extend add/sub to more than 64bits, but extending the other operators seems a bit daunting :D

Bitwise operations like and, or, xor, not are also easy.
Multiply and divide are easy when the number you want to multiply or divide by is a 32 bit value but when both numbers are big, multiply and divide are complicated; especially divide.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 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