flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [solved](F1) min max simplifying

Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 12 Mar 2024, 15:21
Hi
Do you know how to add math operations like min and max?
Usage for example:
Value = Operand max 100 min 1
Or like this:
mov eax,Operand max 100 min 1

The only way I see is to overwrite Define macro:
define Value Operand max 100 min 1

Or I missing something with < > operators?


Last edited by Overclick on 14 Mar 2024, 12:59; edited 1 time in total
Post 12 Mar 2024, 15:21
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
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.
Post 12 Mar 2024, 16:34
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 13 Mar 2024, 14:55
Understood, thanks.
Is it big deal to add such operators support to FASM?
Post 13 Mar 2024, 14:55
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
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    
And use the macro like this:
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 never-ending 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.
Post 13 Mar 2024, 15:24
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
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?


Description:
Filesize: 13.54 KB
Viewed: 2565 Time(s)

Capture.PNG


Post 13 Mar 2024, 20:23
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 14 Mar 2024, 11:44
Tomasz please have a look:


Description:
Filesize: 19.88 KB
Viewed: 2531 Time(s)

fasm3.PNG


Description:
Filesize: 20.35 KB
Viewed: 2531 Time(s)

fasm2.PNG


Description:
Filesize: 12.92 KB
Viewed: 2531 Time(s)

fasm1.PNG


Post 14 Mar 2024, 11:44
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 14 Mar 2024, 12:58
Seems it's working Cool
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
Post 14 Mar 2024, 12:58
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
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    
Post 14 Mar 2024, 13:54
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
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
Post 14 Mar 2024, 15:45
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
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    
Post 14 Mar 2024, 17:01
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 15 Mar 2024, 05:01
Whear is calculation_loop label ?
Post 15 Mar 2024, 05:01
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
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
Post 15 Mar 2024, 05:45
View user's profile Send private message Visit poster's website 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.