flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > fasm1: longmath(65bit+) in assembly time

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 803
Location: Russian Federation, Sochi
ProMiNick 24 Oct 2024, 15:02
Code:
macro longmath [symbolic] {
        if ~definite symbolic#.'size
                symbolic#.'size equ 2
                symbolic#.'0 = (symbolic) and $FFFFFFFF
                symbolic#.'1 = (symbolic shr 32) and $FFFFFFFF
        end if }
;                                a.lo*b.lo
;           a.lo*b.hi+a.hi*b.lo
;a.hi*b.hi
macro longmul dest,src { ;local tmp
        longmath dest,src

        rept dest#.'size+src#.'size %:0 \{ tmp\#.'\#%=0 \}
        rept src#.'size i:0 \{
                rept dest#.'size j:0 \\{
                        rept 1 k:j+i \\\{
                        rept 1 l:k+i \\\\{
                                tmp\\\\#.'\\\\#k = tmp\\\\#.'\\\\#k + dest\\\\#.'\\\\#j * src\\\\#.'\\\\#i
                                tmp\\\\#.'\\\\#l = tmp\\\\#.'\\\\#l+(tmp\\\\#.'\\\\#k shr 32)
                                tmp\\\\#.'\\\\#k = tmp\\\\#.'\\\\#k and $FFFFFFFF
                                \\\\} \\\} \\} \}
        dest#.'0=0
        rept src#.'size i:dest#.'size \{ dest\#.'\#i =0 \}
        rept 1 dest#.'size:dest#.'size+src#.'size\{\}
        rept dest#.'size-1 i:0 \{
                rept 1 j:i+i \\{
                        dest\\#.'\\#j=tmp\\#.'\\#i shr 32 \\} \}
        rept dest#.'size i:0 \{
                rept 1 j:i+i \\{
                        dest\\#.'\\#i=dest\\#.'\\#i + (tmp\\#.'\\#i and $FFFFFFFF)
                        if i<dest#.'size
                                dest\\#.'\\#j=dest\\#.'\\#i shr 32
                                dest#.'\\#i=dest\\#.'\\#i and $FFFFFFFF
                        end if \\} \}
        }


a=$FFFFFFFFFFFFFFFF
b=$FFFFFFFFFFFFFFFF
longmul a,b
;assert a.'3 = $FFFFFFFF
;assert a.'2 =         $FFFFFFFE
;assert a.'1 =                 $00000000
;assert a.'0 =                         $00000001     

good - algotithm not crashed - calculations looks correct
bad - calculations are wrong result

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 24 Oct 2024, 15:02
View user's profile Send private message Send e-mail Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1022
Location: Russia
macomics 24 Oct 2024, 16:30
ProMiNick wrote:
Code:
rept 1 l:k+i \\\\{    
It's definitely +i, not +1. At i = 2, the transfer occurs through a digit, at i = 3 - through two, and not into the next one.
Post 24 Oct 2024, 16:30
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1022
Location: Russia
macomics 24 Oct 2024, 17:30
Code:
include 'ProMiNick-longmath.asm'
macro displaylong value {
  macro displayhex val \{ local temp
    rept 8 i:0 \\{ temp = (val shr (28 - 4 * i)) and 15
    if temp < 10
      display '0' + temp
    else
      display 'A' + temp - 10
    end if
  \\} \}
  display 'a.size='
  displayhex a#.'size
  display 13,10,'a.value='
  rept value#.'size i:0 \{ rept 1 j:value\#.'size-i-1 \\{ displayhex value\\#.'\\#j \\} \}
  display 13,10
}
displaylong a    
Code:
$ fasm test_longmath.asm
flat assembler  version 1.73.32  (16384 kilobytes memory)
a.size=00000002
a.value=FFFFFFFF00000000
1 passes, 0 bytes.    


ADD: But!
Code:
include 'ProMiNick-longmath.asm'
macro displaylong value {
  macro displayhex val \{ local temp
    rept 8 i:0 \\{ temp = (val shr (28 - 4 * i)) and 15
    if temp < 10
      display '0' + temp
    else
      display 'A' + temp - 10
    end if
  \\} \}
  display 'a.size='
  displayhex a#.'size
  display 13,10,'a.value='
  rept value#.'size+2 i:0 \{ rept 1 j:value\#.'size-i+1 \\{ displayhex value\\#.'\\#j \\} \}
  display 13,10
}
displaylong a    
Code:
$ fasm test_longmath.asm
flat assembler  version 1.73.32  (16384 kilobytes memory)
a.size=00000002
a.value=0000000000000000FFFFFFFF00000000
1 passes, 0 bytes.    
e.g. a.'3 = 0, a.'2 = 0, a.'1 = -1, a.'0 = 0

ADD: And!
Code:
include 'ProMiNick-longmath.asm'
macro displaylong value {
  macro displayhex val \{ local temp
    rept 16 i:0 \\{ temp = (val shr (60 - 4 * i)) and 15
    if temp < 10
      display '0' + temp
    else
      display 'A' + temp - 10
    end if
  \\} \}
  display 'a.size='
  displayhex a#.'size
  display 13,10,'a.value='
  rept value#.'size+2 i:0 \{ rept 1 j:value\#.'size-i+1 \\{ displayhex value\\#.'\\#j \\} \}
  display 13,10
}
displaylong a    
Code:
$ fasm test_longmath.asm
flat assembler  version 1.73.32  (16384 kilobytes memory)
a.size=00000002
a.value=0000000000000000000000000000000000000000FFFFFFFF0000000000000000
1 passes, 0 bytes.    
Post 24 Oct 2024, 17:30
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 803
Location: Russian Federation, Sochi
ProMiNick 24 Oct 2024, 20:20
macomics wrote:
It's definitely +i, not +1.
thanks
Code:
macro longmath [symbolic] {
        irpv any, symbolic#.'size \{
        rept 0 \{\} rept 1 \{
                symbolic#.'size equ 2
                symbolic#.'0 = (symbolic) and $FFFFFFFF
                symbolic#.'1 = (symbolic shr 32) and $FFFFFFFF
        \} } 

macro displaylong [value] {
  macro displayhex val \{ local temp
    rept 8 i:0 \\{ temp = (val shr (28 - 4 * i)) and 15
    if temp < 10
      display '0' + temp
    else
      display 'A' + temp - 10
    end if
  \\} \}
  display `value,'.size='
  displayhex value#.'size
  display 13,10,`value,':',13,10
  rept value#.'size i:0 \{ reverse displayhex value\#.'\#i \}
  display 13,10
}

;                                a.lo*b.lo
;           a.lo*b.hi+a.hi*b.lo
;a.hi*b.hi
macro longmul dest,src { ;local tmp
        longmath dest,src
        ;tmp#.'size equ 4
        rept dest#.'size+src#.'size %:0 \{ tmp#.'\#%=0 \}
        rept src#.'size i:0 \{
                rept dest#.'size j:0 \\{
                        rept 1 k:j+i \\\{
                        rept 1 l:k+1 \\\\{
                                tmp#.'\\\#k = tmp#.'\\\#k + dest#.'\\#j * src#.'\#i
                                tmp#.'\\\\#l = tmp#.'\\\\#l+(tmp#.'\\\#k shr 32)
                                tmp#.'\\\\#k = tmp#.'\\\\#k and $FFFFFFFF
                                \\\\} \\\} \\} \}
                                ;displaylong tmp
        rept 1 i:dest#.'size+src#.'size\{ dest#.'size equ i\}
        rept dest#.'size i:0 \{ dest#.'\#i =tmp#.'\#i \}
        }


a=$FFFFFFFFFFFFFFFF
b=$FFFFFFFFFFFFFFFF
longmul a,b
displaylong a
assert a.'3 = $FFFFFFFF
assert a.'2 =         $FFFFFFFE
assert a.'1 =                 $00000000
assert a.'0 =                         $00000001       
Code:
a.size=00000004
a:
FFFFFFFFFFFFFFFE0000000000000001    

unsigned correctly multyplied

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 24 Oct 2024, 20:20
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.