flat assembler
Message board for the users of flat assembler.

Index > Main > which 2-byte nop goes better

Author
Thread Post new topic Reply to topic
l4m2



Joined: 15 Jan 2015
Posts: 648
l4m2
mov ax,ax
ds:nop
xchg bx,bx
xchg eax,eax (66 90)
67 90
rep nop
lea bx,[bx]
fnop


Last edited by l4m2 on 29 Jul 2016, 03:28; edited 1 time in total
Post 28 Jul 2016, 13:23
View user's profile Send private message Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 364
Location: Australia
redsock
FWIW, I use 0x66, 0x90 ... though I am not sure whether any of them make any real difference. The instruction decoders IMO still have to swallow two bytes so I doubt it makes any difference whatsoever.
Post 28 Jul 2016, 21:17
View user's profile Send private message Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
Just for completeness, there are also FPU NOPs:
FXCH ST0
FNOP

Probably a prefixed NOP would be the best choice in terms of CPU execution resources. But at least one prefixed NOP is a different instruction, REP NOP, aka PAUSE.
Post 29 Jul 2016, 00:33
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 648
l4m2
Besides: Is it a good choice if I use REP REP REP NOP to be a 4-byte nop?
If so the nops macro is easy to write:
Code:
macro nops n {
    if n<0
        nops n ;Keep the error itself
    else if n=0
    else if n<16
        repeat n-1
            rep
        end repeat
        nop
    else
        jmp $+n
        db n-2 dup ?
    end if
}    


Last edited by l4m2 on 29 Jul 2016, 03:46; edited 1 time in total
Post 29 Jul 2016, 03:22
View user's profile Send private message Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 364
Location: Australia
redsock
here is the 1..15 list that I use in descending order (AMD/Intel compat, AMD-only code can be a bit nicer still for x86_64):
Code:
        if a = 15
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0x66, 0xf, 0x1f, 0x44, 0x00, 0x00
        else if a = 14
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0xf, 0x1f, 0x44, 0x00, 0x00
        else if a = 13
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0xf, 0x1f, 0x40, 0x00
        else if a = 12
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0xf, 0x1f, 0x00
        else if a = 11
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0x66, 0x90
        else if a = 10
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0x90
        else if a = 9
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
        else if a = 8
                db 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
        else if a = 7
                db 0xf, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00
        else if a = 6
                db 0x66, 0xf, 0x1f, 0x44, 0x00, 0x00
        else if a = 5
                db 0xf, 0x1f, 0x44, 0x00, 0x00
        else if a = 4
                db 0xf, 0x1f, 0x40, 0x00
        else if a = 3
                db 0xf, 0x1f, 0x00
        else if a = 2
                db 0x66, 0x90
        else if a = 1
                db 0x90
        end if
    

_________________
2 Ton Digital - https://2ton.com.au/
Post 29 Jul 2016, 03:45
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 648
l4m2
redsock wrote:
Code:
if a = 15 
                db 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
                db 0x66, 0xf, 0x1f, 0x44, 0x00, 0x00     

That's two commands, isn't it?
Code:
        nop     word    [esp+999]
        nop     word    [esp+3]    
Post 29 Jul 2016, 03:51
View user's profile Send private message Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 364
Location: Australia
redsock
Smile yeah, thats the AMD v. Intel bit of unpleasantness required to get a 15 byte NOP (which ends up being two for Intel). AMD lets you string 0x66's as leading padding bytes (though I am not sure whether there is a decoding penalty for that or not).

_________________
2 Ton Digital - https://2ton.com.au/
Post 29 Jul 2016, 03:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17467
Location: In your JS exploiting you and your system
revolution
l4m2 wrote:
mov ax,ax
ds:nop
xchg bx,bx
xchg eax,eax (66 90)
67 90
rep nop
lea bx,[bx]
fnop
To answer the original question: None.

None of those will "go better" than any of the others in all (or even some) cases. It depends upon the system it is running on. It depends upon the surrounding code. It depends upon how you measure it. It depends upon how it is aligned. And, perhaps most surprisingly for many, it depends upon what code has just been recently executed (because of the caches and things), and what code it to be executed in the future (because of prediction and whatnot).

And these answers have not changed since the last time l4m2 asked "is A better than B". I really wish there were a single known answer to such questions, but there isn't. You just have to realise that it can't be predicted what will happen until you actually run the code in the real context.

And also, if you can't measure any difference in your code when comparing two versions then the answer in that situation is: It makes no difference whatsoever.

And in the unlikely event that your code does show a real measurable difference with one version then the answer is simply: Use the one that gives you the "best" result.
Post 29 Jul 2016, 17:26
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 774
Location: Belarus
DimonSoft
I’ll share this link (somewhat related, though it’s for 32-bit protected mode there):
https://blogs.msdn.microsoft.com/oldnewthing/20130102-00/?p=5663

You can follow links there to find out more about the task they solved at Microsoft with a two-byte nop.
Post 29 Jul 2016, 22:35
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.