flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > [solved](F1) min max simplifying 
Author 

revolution 12 Mar 2024, 16:34
fasm doesn't have min or max as operators. < and > are only valid for comparisons, not for assignment.
You will need a macro. I suggest you use a name different from the already reserved name define. 

12 Mar 2024, 16:34 

Overclick 13 Mar 2024, 14:55
Understood, thanks.
Is it big deal to add such operators support to FASM? 

13 Mar 2024, 14:55 

Tomasz Grysztar 13 Mar 2024, 15:24
As I'm currently focused on showing off fasm 2, it is only natural to post a quick example of how you could achieve something like this with my inline macro package:
Code: include 'macro/inline.inc' inlinemacro bound(value,min,max) if value < min return = min else if value > max return = max else return = value end if end inlinemacro Code: mov al, bound(value,20h,7Fh) As I've been already mentioning a few times on the forums, the main reason why I pushed fasmg's customizability to extreme levels was that I wanted to have an answer to the neverending stream of requests while preserving the simplicity (and thus maintainability) of the assembler core. This made fasmg the ultimate representation of my old "complex solutions with simple features" idea. 

13 Mar 2024, 15:24 

Overclick 13 Mar 2024, 20:23
Let's try again:
Code: calculate_max: mov eax,[edi] mov edx,[edi+4] mov cl,[edi+13] cmp cl,0 jne .max_negative .max_positive: cmp [ebx+13],0 jne calculation_loop cmp [ebx],eax jb calculation_loop ja .set_max cmp [ebx+4],edx jbe calculation_loop .set_max: mov [ebx],eax mov [ebx+4],edx mov [ebx+13],cl jmp calculation_loop .max_negative: cmp [ebx+13],0 je .set_max cmp [ebx],eax ja calculation_loop jb .set_max cmp [ebx+4],edx jae calculation_loop jmp .set_max As I understand that logic. Please have a look and correct if missing something. And let me know how to integrate that short opcode for this function?


13 Mar 2024, 20:23 

Overclick 14 Mar 2024, 11:44
Tomasz please have a look:


14 Mar 2024, 11:44 

Overclick 14 Mar 2024, 12:58
Seems it's working
Not that easy for Float values but more than nothing anyway Just want to rename to some short names like mx mn as I already using variable named max at current project LOL 

14 Mar 2024, 12:58 

Overclick 14 Mar 2024, 13:54
Fixed I hope:
Code: calculate_max: mov eax,[edi] mov edx,[edi+4] mov cl,[edi+13] cmp cl,0 cmp byte[ebx+13],cl jg set_min_max jl calculation_loop cmp [ebx],eax ja set_min_max jb calculation_loop cmp [ebx+4],edx ja set_min_max jmp calculation_loop calculate_min: mov eax,[edi] mov edx,[edi+4] mov cl,[edi+13] cmp cl,0 cmp byte[ebx+13],cl jl set_min_max jg calculation_loop cmp [ebx],eax jb set_min_max ja calculation_loop cmp [ebx+4],edx jb set_min_max jmp calculation_loop set_min_max: mov [ebx],eax mov [ebx+4],edx mov [ebx+13],cl jmp calculation_loop 

14 Mar 2024, 13:54 

CandyMan 14 Mar 2024, 15:45
There is "A min/max B" and I would like "min/max(A,B)"
In addition for Int64 the top DWord must be compared first Code: dq 1 min 7FFFFFFF_00000000h dq 7FFFFFFE_00000000h min 7FFFFFFF_00000000h ; bug _________________ smaller is better 

14 Mar 2024, 15:45 

Overclick 14 Mar 2024, 17:01
CandyMan wrote: There is "A min/max B" and I would like "min/max(A,B)" Don't see any difficult to use it same way as all other math operators. More intuitive that way. CandyMan wrote: for Int64 the top DWord must be compared first) Sure thanks. Usually high parts placed first so I didn't check it enough. Fixed: Code: calculate_max: mov eax,[edi] mov edx,[edi+4] mov cl,[edi+13] cmp cl,0 cmp byte[ebx+13],cl jg set_min_max jl calculation_loop cmp [ebx+4],eax ja set_min_max jb calculation_loop cmp [ebx],edx ja set_min_max jmp calculation_loop calculate_min: mov eax,[edi] mov edx,[edi+4] mov cl,[edi+13] cmp cl,0 cmp byte[ebx+13],cl jl set_min_max jg calculation_loop cmp [ebx+4],eax jb set_min_max ja calculation_loop cmp [ebx],edx jb set_min_max jmp calculation_loop set_min_max: mov [ebx],eax mov [ebx+4],edx mov [ebx+13],cl jmp calculation_loop 

14 Mar 2024, 17:01 

Roman 15 Mar 2024, 05:01
Whear is calculation_loop label ?


15 Mar 2024, 05:01 

Overclick 15 Mar 2024, 05:45
Roman, it's FASM Source modification, everythin is there.
Files to modify: EXPRCALC.INC, TABLES.INC This thread needs to move to Compiler Internals I guess 

15 Mar 2024, 05:45 

< Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.