flat assembler
Message board for the users of flat assembler.

Index > Main > This code requires 65536 passes

Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20333
Location: In your JS exploiting you and your system
revolution 16 Jun 2023, 09:45
Code:
~ fasm 65536-passes.asm -p 65536
flat assembler  version 1.73.08  (3910136 kilobytes memory)
65536 passes, 1148.2 seconds, 0 bytes.    
Current fasm has a passes limit of 65536. If the code was a tiny bit more complex then it could not be assembled by fasm,

But wait, why does it produce zero bytes of output, I hear you ask. Isn't that kind of useless? Yes, of course it is useless code, but useful for testing limits and see how it performs in extreme cases. The point being that it passes the passes limit test and produces the expected result.

The source that produces the maximised passes uses back-referencing in each pass to create a newly "used" section and forcing a new pass to be required. Until finally reaching the final back-reference and completing the assembly. The source is posted below.

Note that if the order of references were reversed then the code can compile in just a handful of passes and finish very quickly. So if you want to reduce your passes count for your own code, put "main" first and the follow with all second level functions, and then the third level, etc. You will see fewer passes and higher throughput.
Code:
virtual
        rept 65534 L {
                if used label_#L
                        label_#L: rept 1 N:L-1 \{ mov eax,[label_0 + label_\#N * 0] \}
                end if
                last equ label_#L
        }
        label_0: dd last
end virtual    
Post 16 Jun 2023, 09:45
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1791
Roman 16 Jun 2023, 16:47
65535 this maximum for one rept ?
What will happen if write three rept ?
rept 65534 L { }
rept 65534 L { }
rept 65534 L { }
Post 16 Jun 2023, 16:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20333
Location: In your JS exploiting you and your system
revolution 16 Jun 2023, 18:21
Roman wrote:
65535 this maximum for one rept ?
No. The number makes the maximum number of passes. You can't set 65537 passes. You can do more than 65534 rept. Just not in that code because it won't compile.
Roman wrote:
What will happen if write three rept ?
rept 65534 L { }
rept 65534 L { }
rept 65534 L { }
Try it.
Post 16 Jun 2023, 18: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: 20333
Location: In your JS exploiting you and your system
revolution 02 Jul 2023, 09:19
With fasmarm v1.44 it is possible to have a source that maximises the usage of passes. The source requires all passes to complete.
Code:
~ for P in 11 43 69 123 5555 65536 ; do fasmarm maximal-passes.asm -p $P ; done
flat assembler for ARM  version 1.44 (built on fasm 1.73.30)  (16384 kilobytes memory)
11 passes, 32 bytes.
flat assembler for ARM  version 1.44 (built on fasm 1.73.30)  (16384 kilobytes memory)
43 passes, 32 bytes.
flat assembler for ARM  version 1.44 (built on fasm 1.73.30)  (16384 kilobytes memory)
69 passes, 32 bytes.
flat assembler for ARM  version 1.44 (built on fasm 1.73.30)  (16384 kilobytes memory)
123 passes, 32 bytes.
flat assembler for ARM  version 1.44 (built on fasm 1.73.30)  (16384 kilobytes memory)
5555 passes, 32 bytes.
flat assembler for ARM  version 1.44 (built on fasm 1.73.30)  (16384 kilobytes memory)
65536 passes, 32 bytes.    
The pass values aren't special, choose other values and it will use between P-2 and P passes. Usually it needs all, but occasionally it can complete in one or two fewer.

The reason for this behaviour is the "oscillation problem" with instruction encoding dependent upon the alignment of the operands.

The example code that can do this is very simple.
Code:
thumb
rept 8 {local x
adr r0,x
x:}    
The code has 8 alignment dependent ADR instructions that interact with each other. As some grow others will shrink, and each pass produces a different set of long-vs-short opcodes compared to the previous pass. Eventually the assembler gets bored and decides to encode long form instructions for all the problematic lines. This doesn't kick-in until the last 6 passes, giving it enough time to figure out a solution and produce a correct output.

If you change ADR to ADR.W then the job of the assembler is simplified and it only needs two passes. The same is also true if you add ALIGN 4 before declaring x, only two passes are needed, because the alignment is guaranteed.
Post 02 Jul 2023, 09:19
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.