flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How this macro convert to asm ?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 12:33
I want write on asm this algorithm.

Code:
;this macro convert text to float. "5.345" do dd 5.345
macro fltDat str* {
  local buffer, next
  define buffer f
  rept 8 i:1 \{
        rept 1 c: str shr ((8-i)*8) and 0FFh - 48 \\{
          match f, buffer \\\{
             define next 0
             match -=48, c \\\\{ define next 1 \\\\}
             match -=2, c \\\\{ define buffer .\\\#f
                                define next 1 \\\\}
             match -=3, c \\\\{ define buffer -f
                                define next 1 \\\\}
             match =0, next \\\\{ define buffer c\\\#f \\\\}
          \\\} \\} \}
  dd buffer
}                     
    

First question how write this to asm ?
Code:
;is shr equivalent asm shr ? 
str shr ((8-i)*8) and 0FFh - 48    


How i understand do this:
Code:
;txt db "5.345",0
     xor ecx,ecx
l1:  mov al,[txt+ecx]
     shr  al,(8-ecx)*8
     and al,0ffh
     sub al,48
   
     inc  ecx
     cmp ecx,8
     jb   l1

    
Post 22 Apr 2023, 12:33
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 921
Location: Russia
macomics 22 Apr 2023, 13:24
Roman wrote:
First question how write this to asm ?
Yes. Technically, as long as the string fits in the fasm number buffer (8 bytes), you can work with it as with a regular number. In fact, this string simply lists bytes from the highest digit in the number to the lowest. Numbers are translated from characters by subtracting '0' and all these numbers are combined into a new string value in the buffer variable, which is written via dd.


Last edited by macomics on 22 Apr 2023, 13:26; edited 1 time in total
Post 22 Apr 2023, 13:24
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 13:45
I try but not correct result.
Code:
        txt db '55445',0
macro mdig t {
     mov ebx,1
     xor edx,edx
     xor eax,eax
l1:  mov al,[t-1+ebx]
     test al,al
     jz   ext     
     mov cl,8
     sub cl,bl
     shl cl,3
     shr  ax,cl ;(8-ecx)*8
     and al,0ffh
     sub al,48
     add edx,eax
     inc  ebx
     cmp ebx,8
     jb   l1
ext:
      } 
mdig txt ;edx=334h wrong. Right edx=D895h
    
Post 22 Apr 2023, 13:45
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 921
Location: Russia
macomics 22 Apr 2023, 14:18
Your problem is that you have already taken the necessary byte and are doing operations on it, which fasm does immediately on the qword to get this same byte.

fasm does not have the ability to address a string constant by indexes to get bytes. You have it. You used it
Code:
mov al, [t-1+ebx]    

Then why do you need the rest of the shift operations?
Code:
     mov cl,8
     sub cl,bl
     shl cl,3
     shr  ax,cl ;(8-ecx)*8
     and al,0ffh    


You can immediately move 0 to ebx and you won't need -1 when indexing
Post 22 Apr 2023, 14:18
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 15:46
Oh ! I do this. Convert virtual text to int value.
Code:
virtual at 0
     ark::
         ark_txt1 db '55445',0 ;i think i can do 64 bit int values !
;ark_txt1 db '44441554451230',0 ;yes i test and get rax=286B5B10FB1Eh
     ark_size:
end virtual
;in code
Start:  
kuuu = 0
repeat ark_size
  load a byte from ark:%-1
  if a = 0
   break
  end if
  kuuu = kuuu*10+a-48
end repeat       
mov eax,kuuu  ;eax=D895h (decimal 55445)
    
Post 22 Apr 2023, 15:46
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 16:51
Found formula sin.
How adapted this for fasm preprocessor ? Enough float not double.
Code:
double sin_e2(double x) {
  double result = 0;
  int sign = 1;
  double xx = x * x;
  double pw = x;
  double fti = 1.0;
  for(int i = 1; i < 25; i += 2) {
    fti /= i;
    result += sign * pw * fti;
    fti /= ( i + 1 );
    sign = -sign;
    pw  *= xx;
  }
  return result;
}
    
Post 22 Apr 2023, 16:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20217
Location: In your JS exploiting you and your system
revolution 22 Apr 2023, 16:57
rept will do preprocessor arithmetic. Good luck.
Post 22 Apr 2023, 16:57
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 16:59
Quote:
rept will do preprocessor arithmetic. Good luck.

Thanks Smile
But not supported 1.0/num.
Post 22 Apr 2023, 16:59
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20217
Location: In your JS exploiting you and your system
revolution 22 Apr 2023, 17:06
Roman wrote:
But not supported 1.0/num.
Work fine.
Code:
rept 1 x:1/2 { display x+'0' }    
You might want to consider fixed point arithmetic using integers.
Post 22 Apr 2023, 17:06
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 17:42
Quote:

rept 1 x:1/2 { display x+'0' }


Show 0
And what is practical using rept 1 x:1/2 ?
Code:
rept 1 x:1/2 { mov eax, x#f }
;get mov eax,0
    

Fiasco Smile
Post 22 Apr 2023, 17:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20217
Location: In your JS exploiting you and your system
revolution 22 Apr 2023, 19:00
revolution wrote:
You might want to consider fixed point arithmetic using integers.
Good luck.
Post 22 Apr 2023, 19:00
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 22 Apr 2023, 22:08
X = 360/angl/180*3.14
1-x
Post 22 Apr 2023, 22:08
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 921
Location: Russia
macomics 22 Apr 2023, 22:40
Code:
fldpi
fild dword [const360]
fidiv dword [angl]
fidiv dword [const180]
fmulp st0, st1

...

const360 dd 2
const180 dd 1
angl dd 5    
Post 22 Apr 2023, 22:40
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 23 Apr 2023, 11:20
Code:
mov     eax,179.0
call    mysin     ;all ok, but failed on 180.0 get 6.01 must 0

mysin:xorps xmm3,xmm3
 cmp eax,180.0
 jz .2
 cmp eax,0
 jz .2
      movd  xmm1,eax   ;x angle
      mov   eax,1.0
      movd  xmm4,eax   ;i
      movd  xmm5,eax
      mov   eax,180.0
      movd  xmm0,eax
      divss xmm1,xmm0    ;failed if xmm1=0 or 180
      mulss xmm1,[.pi]   ;x/180*pi convert to degree 
      movss xmm0,xmm1    ;q
      mulss xmm1,xmm1    ;x*x
      
      mov   ecx,5*2
.1:   addss xmm3,xmm0       ;s+q
      mulss xmm0,[.min]     ;q = q* (-1) * (x*x) / ((2*i+1) * (2*i))
      mulss xmm0,xmm1
      movss xmm6,xmm4
      addss xmm6,xmm6
      movss xmm7,xmm6
      addss xmm6,xmm5
      mulss xmm6,xmm7
      divss xmm0,xmm6    
      addss xmm4,xmm5    ;i+1
      dec   ecx
      jnz   .1
                                    ;xmm3 out sin(x)
.2:   ret
      .pi  dd 3.141592
      .min dd -1.0 
    
Post 23 Apr 2023, 11:20
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20217
Location: In your JS exploiting you and your system
revolution 24 Apr 2023, 10:02
The FPU has fsin.
Post 24 Apr 2023, 10:02
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 24 Apr 2023, 10:20
Quote:
The FPU has fsin.

I know. And have fsincos too.
But fasm preprocessor not have.

And me interesting calculate sin this way , for my education.
And have another way calculated sin, if cpu not have fpu or sse,avx.

Ok. How rewrite mysin to int for regs eax\edx ?
EAX=4 EDX=5455 this equivalent float 4.5455
Post 24 Apr 2023, 10:20
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20217
Location: In your JS exploiting you and your system
revolution 24 Apr 2023, 10:34
Roman wrote:
Ok. How rewrite mysin to int for regs eax\edx ?
EAX=4 EDX=5455 this equivalent float 4.5455
You could try using fixed point.
revolution wrote:
You might want to consider fixed point arithmetic using integers.
Secret lookup word. CORDIC
Post 24 Apr 2023, 10:34
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 24 Apr 2023, 10:44
This ?
Code:
#define cordic_1K 0x26DD3B6A    // 1/k = 0.6072529350088812561694
#define half_pi 0x6487ED51      // pi / 2
#define MUL 1073741824.000000   // 1.0 = 1073741824
#define CORDIC_NTAB 32
int cordic_tab [] = {
    0x3243F6A8, 0x1DAC6705, 0x0FADBAFC, 0x07F56EA6, 0x03FEAB76, 0x01FFD55B, 0x00FFFAAA, 0x007FFF55,
    0x003FFFEA, 0x001FFFFD, 0x000FFFFF, 0x0007FFFF, 0x0003FFFF, 0x0001FFFF, 0x0000FFFF, 0x00007FFF,
    0x00003FFF, 0x00001FFF, 0x00000FFF, 0x000007FF, 0x000003FF, 0x000001FF, 0x000000FF, 0x0000007F,
    0x0000003F, 0x0000001F, 0x0000000F, 0x00000008, 0x00000004, 0x00000002, 0x00000001, 0x00000000
};
 
void cordic32_sin_cos(int alpha, int *s, int *c, int n)
{
    int k, d, tx;
    int z = alpha;
    *c = cordic_1K, *s = 0;
    n = (n > CORDIC_NTAB) ? CORDIC_NTAB : n;
    if(n<3) n = 3;
    for (k=0; k<n; ++k)
    {
        d = z >> 31;    //d = z>=0 ? 0 : -1;
        tx = *c;why need this ?
        *c -= (((*s>>k) ^ d) - d);why need this ?
        *s += (((tx>>k) ^ d) - d);why need this ?
        z -= ((cordic_tab[k] ^ d) - d);
    }
}
 
// y = s, x = c
int cordic32_atan2(int s, int c, int n)
{
    int k, d, angle = 0;
    int re = c, im = s, tx;
    n = (n > CORDIC_NTAB) ? CORDIC_NTAB : n;
    if(n<3) n = 3;
    for (k=0; k<n; ++k)
    {
        d = im >> 31;
        angle += ((cordic_tab[k] ^ d) - d);
        tx = re;
        re += (((im>>k) ^ d) - d);
        im -= (((tx>>k) ^ d) - d);
    }
    return angle;
}    
Post 24 Apr 2023, 10:44
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1722
Roman 24 Apr 2023, 12:58
Post 24 Apr 2023, 12:58
View user's profile Send private message 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.