64bit quad addition on x86 cpu

Bare metal programming in PureBasic, for experienced users
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

64bit quad addition on x86 cpu

Post by Keya »

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: Select all

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()
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: 64bit quad addition on x86 cpu

Post by wilbert »

Usually add/adc is used for addition and sub/sbb for subtraction.

Code: Select all

  ! 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: 64bit quad addition on x86 cpu

Post by Keya »

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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: 64bit quad addition on x86 cpu

Post by wilbert »

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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: 64bit quad addition on x86 cpu

Post by Keya »

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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: 64bit quad addition on x86 cpu

Post by wilbert »

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.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply