echo icon indicating copy to clipboard operation
echo copied to clipboard

Optimisations

Open palavrov opened this issue 8 years ago • 5 comments

If the goal is to write the code manually as small and elegant as possible then it can be improved IMHO :)

It was long time since my last low level x86 codding (damn - 15-20 years?) and the code can be wrong but hope that you'll get the idea. Back then one of my favorite hacks were based on cwd x86 instruction but in this case don's see any benefit to use it.

pop edi              ; \
pop edi              ;  >  Load EDI with the first positional argument
pop edi              ; /

push ds              ; \ Shorter than 'mov ax, ds' 'mov es, ax'
pop es               ; / and doesn't destroy any CPU register

cld
xor ecx, ecx     ; \ Shorter than 'mov ecx, -1' 
dec ecx          ; /

xor eax, eax
repne scasb
lea  edx, [edi+ecx]    ; > may be this will need to be fixed with +-1
xchg ecx, edx
neg edx
dec edx           ; > remove the trailing null character
mov ebx, 1
mov eax, 4
int 0x80

xor ebx, ebx
mov eax, 1
int 0x80

palavrov avatar Jan 02 '17 07:01 palavrov

@palavrov This feedback is awesome! Thanks for taking the time to review and provide some tips.

kelseyhightower avatar Jan 02 '17 13:01 kelseyhightower

@palavrov is there a chance that xor ecx, ecx; dec ecx could be interpreted as 4,294,967,295 instead of -1?

jflopezfernandez avatar Jun 07 '19 22:06 jflopezfernandez

@jflopezfernandez, it is the same. -1 in binary is presented as 2^32-1 which is exactly 4,294,967,295. Check here about two's complement

palavrov avatar Jun 08 '19 04:06 palavrov

That's what I mean, is there a chance that since you're subtracting -1, the next instruction might be interpreted as trying to index the array at position 2^32 - 1 instead of being interpreted as indexing the address before position 0?

jflopezfernandez avatar Jun 10 '19 12:06 jflopezfernandez

ECX will be used as count limit for REPNE SCASB instruction i.e. we want that it is the maximum possible value here i.e. all bits set to 1 which is unsigned 0xFFFFFFFF or signed -1.

I.e. it is not used for addressing if that worries you. But even it is used it is totally OK to do signed arithmetic on unsigned addresses (i.e. adding signed -1 to unsigned address for example) because of the two's complement that the x86 CPU uses.

palavrov avatar Jun 10 '19 14:06 palavrov