flat assembler
Message board for the users of flat assembler.

 Index > High Level Languages > Fasm calc c math
Author
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
This problem https://board.flatassembler.net/topic.php?p=215729#215729
For this reason I implement somthing like c math.
Code:
```c_math:
db 'int(ark ,a ,b ) flt(foh ) vec4(z1 ) mat()',13,10
db 'ark = 10*2; a = 3; b = a +5; foh = a + b +2',13,10
db 'z1 = a ,b ,foh ,4+ark ',13,10,0
```

I have big 3d level and i need to use floats data.

Now i search in first line 'int(' than search in next lines 'ark'
If i found ark(ark =) i put in TabInt pToText and calc ark value and get 20(put value in TabInt+4)
Than search a(a =), than search b(b = ) and foh(foh =) and z1(z1 =).

My problem how i must do ? Replace in next lines all ark to value 20 and replace a to 3 ?
I don`t understood how do this more beautiful.
Than cal b.
I do not understand clear how to proceed next steps.

Last edited by Roman on 30 Aug 2020, 07:10; edited 4 times in total
30 Aug 2020, 03:45
bitRAKE

Joined: 21 Jul 2003
Posts: 3045
Location: vpcmipstrm
bitRAKE
If 'ark' is on the right-hand side of equation then it must be found and replaced with value at location. To find previous 'ark' will need a back-reference of some type. Could be as simple as a table of pointers to first 'ark' string and it's value. Searching the table, you'd get the pointer and see if strings match.

_________________
30 Aug 2020, 03:56
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
Ok.
If i start from end text.
Code:
```'z1 = a ,b ,foh ,4+ark '
```

I found ark.
And what do than next ? Because z1 = a,b,foh,4+20(i don`t want copy 4 times z1)
I want copy one time z1 to another text buffer with digits(without a,b,ark,foh)
And get this TextBuf2 'z1 = 3,8,13,4+20'
30 Aug 2020, 04:04
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
How i see(my vision).
For z1 i find all values(in z1) and put in TabVec4 text pointers to 'a','b','ark','foh'

In this case i get for z1 5 text pointers. First pointers to ' =' start z1, second pointer to text 'a =' and etc

Than i copy z1 to TextBuf2 'z1 = 3,8,13,4+20'
30 Aug 2020, 04:15
bitRAKE

Joined: 21 Jul 2003
Posts: 3045
Location: vpcmipstrm
bitRAKE
That seems like it would work for most cases.

_________________
30 Aug 2020, 06:27
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
Quote:

That seems like it would work for most cases.

But write parser is not simple job.
30 Aug 2020, 07:06
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
Revolution placed my thread in HLL !
For this reason i using phyton !

In phyton i could do:
Code:
```ark = 10*2; a = 3; b = a +5; foh = a + b +2
z1 = a ,b ,foh ,4+ark
```

And get in out file z1 dd 3f,8f,13f,24f
Its good for 3D games.

PS: It's a pity I didn't this in fasm
30 Aug 2020, 11:41
bitRAKE

Joined: 21 Jul 2003
Posts: 3045
Location: vpcmipstrm
bitRAKE
Roman wrote:
But write parser is not simple job.
IMHO. the easiest way to start is with stack-based machines because you'll need to delay calculations. The stack can act like local context. Unless you want to use RPN - that's easier to code.

Let me know if you want an example - I can adapt my four-state method to parse this. I'm almost done with my next example in that series.

_________________
30 Aug 2020, 15:28
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
Quote:
Let me know if you want an example - I can adapt my four-state method to parse this.

Yes.
It be very good.
Code:
```c_math:
db 'int(ark ,a ,b ) flt(foh ) vec4(z1 ) mat()',13,10
db 'ark = 10*2; a = 3; b = a +5; foh = a + b +2',13,10
db 'z1 = a ,b ,foh ,4+ark ',13,10,0    ```
31 Aug 2020, 06:46
bitRAKE

Joined: 21 Jul 2003
Posts: 3045
Location: vpcmipstrm
bitRAKE
The changes need to the parser are trivial.
• change comment character to "#" (any will do)
• let letter groups consist of numbers, too.
This results in:
Code:
```Tokenizer:
enter 32,0
; assume every token is one byte w/o whitespace! yikes!
imul rax,[input_bytes],27
mov [result_bytes],rax
invoke HeapAlloc,[hHeap],4,[result_bytes]
mov [result_buffer],rax
leave

push rsi rdi rbx
mov rsi,[input_buffer]
mov r11,[result_bytes]
mov rdi,[result_buffer]
add r11,rdi ; memory limit of output
call FourState
sub r11,rdi ; unused bytes
sub [result_bytes],r11 ; actual output bytes (not including null byte)
xor eax,eax
stosb
pop rbx rdi rsi

retn

CLASS_NUMBER    = 0000_0000b
CLASS_LETTER    = 0100_0000b
CLASS_SYMBOL    = 1000_0000b
CLASS_CTRL      = 1100_0000b

CTRL_ENDOFINPUT = 0
CTRL_LINECOMMENT= 1
CTRL_IGNORE     = 2

FourState:
namespace FourState
mov rbx,tab_UTF8
; go from the empty class to the class of first character
init:   lodsb
xlatb                           ; byte classes and decoding
test al,1100_0000b
jz number_start
jns letter_start
jpe other_start
symbol_start: ; punc, math, etc ...     ; state initialization
lea rdx,[rsi-1]                 ; preserve name start
symbol_more:
lodsb
xlatb
test al,1100_0000b
jns symbol_end
jpo symbol_more
symbol_end:                             ; state termination
push rax
pushfq
lea rcx,[rsi-1]
mov rax,' symbol' or (\$0D shl 56)
call _display
popfq
pop rax
js other_start
jpe number_start
letter_start:
lea rdx,[rsi-1]
letter_more:
lodsb
xlatb
test al,1100_0000b
jns letter_more
letter_end:
push rax
pushfq
lea rcx,[rsi-1]
mov rax,' letter' or (\$0D shl 56)
call _display
popfq
pop rax
jpo symbol_start
js other_start
number_start:
lea rdx,[rsi-1]
number_more:
lodsb
xlatb
test al,1100_0000b
jz number_more
number_end:
push rax
pushfq
lea rcx,[rsi-1]
mov rax,' number' or (\$0D shl 56)
call _display
popfq
pop rax
jns letter_start
jpo symbol_start
other_start:
and eax,0x3F
cmp byte [Control_Table-1],al
jbe _errorX
push rax
call qword [Control_Table+rax*8]
jc _error
pop rax
jmp init
_errorX:
movzx eax,byte [Control_Table-1]
push rax
_error:
mov rax,('ERROR: ' shl 8) or 13
stosq
pop rax
push rsi
mov rsi,[Error_Table+rax*8]
lodsb
movzx ecx,al
rep movsb
pop rsi
retn
end namespace ; FourState

EndOfInput:
stc
retn

LineComment:
@@:   lodsb
test al,al
jz EndOfInput
cmp al,10
jz @F
cmp al,13
jnz @B
@@:   sub rsi,1

IgnoreCtrl:
clc
retn

_display:
push rax

; don't exceed buffer - just give up if full
mov eax,ecx
sub eax,edx
lea rax,[rdi+rax+8*3+3]
cmp r11,rax
jc .buffer_full

mov rax,('class:' shl 16) or \$090D
stosq
pop rax
stosq
mov rax,('value: ' shl 8) or \$09
stosq
mov al,'"'
stosb
; copy data string to output
push rsi
mov rsi,rdx
sub ecx,edx
rep movsb
stosb
pop rsi
retn

.buffer_full:   ; just don't produce more output
pop rax
retn

.data

align 64
rb 7
db (sizeof Control_Table) shr 3
Control_Table Δ dq \
EndOfInput,\
LineComment,\
IgnoreCtrl

Error_Table dq \
Error_EOI,\
Error_EOI,\
Error_NoError,\
Error_CtlRng

Error_EOI       Δ db sizeof Error_EOI-1,       'end of input expected'
Error_NoError   Δ db sizeof Error_NoError-1,   "this doesn't happen"
Error_CtlRng    Δ db sizeof Error_CtlRng-1,    'unsupported control byte'

align 64
tab_UTF8:
repeat 256,i:0
match A =i B,: 9 10 11 12 13 32 :
db CTRL_IGNORE or CLASS_CTRL
else
if i > 127
db \$3F or CLASS_LETTER
else if i = 0
db CTRL_ENDOFINPUT or CLASS_CTRL
else if i = '#'
db CTRL_LINECOMMENT or CLASS_CTRL
else if (i >= 'A') & (i <= 'Z')
db (i-'A'+10) or CLASS_LETTER
else if (i >= 'a') & (i <= 'z')
db (i-'a'+10) or CLASS_LETTER
else if (i >= '0') & (i <= '9')
db (i-'0') or CLASS_NUMBER
else
db (i and \$3F) or CLASS_SYMBOL
end if
end match
end repeat    ```
Now, when I test your langauge,
Code:
```int(ark ,a ,b ) flt(foh ) vec4(z1 ) mat()
ark = 10*2; a = 3; b = a +5; foh = a + b +2
z1 = a ,b ,foh ,4+ark    ```
...I get the following ouput:
Code:
```        class: letter   value: "int"
class: symbol   value: "("
class: letter   value: "ark"
class: symbol   value: ","
class: letter   value: "a"
class: symbol   value: ","
class: letter   value: "b"
class: symbol   value: ")"
class: letter   value: "flt"
class: symbol   value: "("
class: letter   value: "foh"
class: symbol   value: ")"
class: letter   value: "vec4"
class: symbol   value: "("
class: letter   value: "z1"
class: symbol   value: ")"
class: letter   value: "mat"
class: symbol   value: "()"
class: letter   value: "ark"
class: symbol   value: "="
class: number   value: "10"
class: symbol   value: "*"
class: number   value: "2"
class: symbol   value: ";"
class: letter   value: "a"
class: symbol   value: "="
class: number   value: "3"
class: symbol   value: ";"
class: letter   value: "b"
class: symbol   value: "="
class: letter   value: "a"
class: symbol   value: "+"
class: number   value: "5"
class: symbol   value: ";"
class: letter   value: "foh"
class: symbol   value: "="
class: letter   value: "a"
class: symbol   value: "+"
class: letter   value: "b"
class: symbol   value: "+"
class: number   value: "2"
class: letter   value: "z1"
class: symbol   value: "="
class: letter   value: "a"
class: symbol   value: ","
class: letter   value: "b"
class: symbol   value: ","
class: letter   value: "foh"
class: symbol   value: ","
class: number   value: "4"
class: symbol   value: "+"
class: letter   value: "ark"
ERROR: end of input expected    ```
Now, you just need to decided what to do at each token. Instead of printing the display, the logic to realize your language is needed.

_________________
01 Sep 2020, 00:31
Roman

Joined: 21 Apr 2012
Posts: 815
Roman

bitRAKE

Code from Fasmg ?
01 Sep 2020, 03:23
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
Quote:
Instead of printing the display, the logic to realize your language is needed.

Now we need get result:
z1 = 3,8,13,24

And save text to file dataFlt.asm
Or save data as binary float
01 Sep 2020, 03:28
bitRAKE

Joined: 21 Jul 2003
Posts: 3045
Location: vpcmipstrm
bitRAKE
I only do asm in fasmg these days - sometimes it works in fasm! Back converting to fasm is not terrible -- just need to know both. :/

"z1 = 3,8,13,24" isn't valid assembly language. So, you have me confused. I'll assume you want to output a float vector: "z1 dd 3.0,8.0,13.0,24.0"

Here is the approach I'd take:

1. Every letter token gets stored on the stack: <start address>,<end address>

2. Every number is stored on stack like this: <0>,<number>

3. create control character "(" which checks if token on stack is a function name. If it is then store function pointer to active state, and set top of stack to <0,0>. Otherwise error.

4. create control character ")" which processes tokens on stack until zero; using active function pointer. Set active funtion pointer to zero when done.

5. create control character "=" which checks if token on top of stack has been defined then sets active function pointer to assign value of that type, push <0,0>. Otherwise error.

6. create control character ";" which executes active function on stack parameters.

There are many ways to evaluate the math operations.

Some questions come to mind: do you just output the last operation? you aren't interested in keeping any intermediate results?

The way you'll tackle this is just to break it into manageable pieces.

_________________
01 Sep 2020, 03:54
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
Quote:
"z1 = 3,8,13,24" isn't valid assembly language. So, you have me confused. I'll assume you want to output a float vector: "z1 dd 3.0,8.0,13.0,24.0"

Why ?!
Code:
```z1 = 3,8,13,24
or
z1 = 3f,8f,13f,24f ;fasm understood symbol f
```

Symbol f nice. Because in float might be 2,2.2
If we do 2f,2.2f fasm understood this.

If we apply .0 we get 2.0,2.2.0 and fasm get error.

Last edited by Roman on 01 Sep 2020, 06:09; edited 1 time in total
01 Sep 2020, 06:07
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17669
revolution
Code:
```z1 = 3,8,13,24
processed: z1=3,8,13,24
error: extra characters on line.    ```
01 Sep 2020, 06:09
Roman

Joined: 21 Apr 2012
Posts: 815
Roman
z1 = 3,8,13,24 its text.
For fasm need:

z1 dd 3f,8f,13f,24f

And fasm compile this float data(from file floatData.asm)
01 Sep 2020, 06:10
bitRAKE

Joined: 21 Jul 2003
Posts: 3045
Location: vpcmipstrm
bitRAKE
The parser is setup to parse floats like <#><.><#> -- three separate tokens. fasmg could actually parse your syntax, too (not that easy); or maybe an easier natively supported construction is possible.

For my own laguage I start by documenting why it exists, intent of operation, functionality to avoid, ..., syntax, etc. This way I have a well defined meaningful plan. Knowing what to do and why I'm doing it.
01 Sep 2020, 17:03
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum