ASM space optimization: EAX vs EBX

Danilo Bargen

I'm working on an x86 assembly code golf puzzle. I'm assembling the source file using NASM:

nasm -f elf32 -O0 main.s
ld -m elf_i386 -s -O0 -o main main.o

Using -O0, all optimizations should be turned off. The goal is to reduce the size of the ELF binary.

While working on the "reference implementation" for the puzzle, I stumbled over a strange behavior. This is a reduced code sample:

section .text
    global _start        ; Must be declared for linker

_start:                  ; Entry point for linker

read_stdin:
    add    esp, 8        ; Ignore argc and argv[0] on stack
    pop    eax           ; Store pointer to 'argv[1]' into EAX
    mov    eax, [eax]    ; Dereference pointer
    and    eax, 0xff     ; We only want the least significant byte
    add    eax, -0x30    ; Subtract ascii offset

exit:
    mov    eax, 1        ; Syscall: sys_exit
    mov    ebx, 0        ; Exit code 0
    int    0x80          ; Invoke syscall

The binary is 264 bytes:

$ wc -c main
264 main

Now when I simply replace all occurrences of eax in the read_stdin section with ebx, ecx or edx, the binary gets larger:

$ wc -c main
268 main

When comparing the sizes of the object files, the difference is even larger (480 vs 496 bytes). What's special about the eax register that this happens? Is NASM doing some kind of optimization, even though -O0 has been specified?

spudone

EAX is the accumulator register. It has special one-byte opcodes for all nine basic operations (ADD, ADC, AND, CMP, OR, SBB, SUB, TEST, and XOR). Also the MOV instruction has a one-byte opcode for moving data into the accumulator from a constant memory location.

The Art of Picking Intel Registers

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

From Dev

ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

From Dev

Does __asm{}; return the value of eax?

From Dev

Gcc inline ASM, set EBX to char array address using LEA

From Dev

INT vs CALL on Asm instructions

From Dev

INT vs CALL on Asm instructions

From Dev

Why does GCC put a no-op push/pop when using int foo asm("ebx") to pin a var to a register?

From Dev

g++ -no optimization- skips asm code after goto

From Dev

g++ inline asm not working as expected with optimization flag

From Dev

Anaconda vs miniconda space

From Dev

SQL Optimization WHERE vs JOIN

From Dev

Optimization Algorithm vs Regression Models

From Dev

Numba vs Cython loop optimization

From Dev

Cache Optimization - Hashmap vs QuickSort?

From Dev

Hard drive space vs Database space

From Dev

What are the benefits of using smaller registers, e.g. al vs eax / rax

From Dev

What are the benefits of using smaller registers, e.g. al vs eax / rax

From Dev

Unused Vs. Unallocated Space

From Dev

HeapSort vs MergeSort space complexity

From Dev

Java Optimization: Local Variable Vs Instance Variable

From Dev

Optimization of raw new[]/delete[] vs std::vector

From Dev

clang vs gcc - optimization including operator new

From Dev

Compiler optimization call-ret vs jmp

From Dev

MySQL optimization : index choice WHERE vs JOIN

From Dev

Optimization of naive matrix multiplication (ICC vs GCC)

From Dev

Compiler code optimization: AST vs. IR

From Dev

Writing JavaScript for minification vs. for optimization

From Dev

Optimization of sprites (quantity vs redundant transparency)

From Dev

Writing JavaScript for minification vs. for optimization

Related Related

  1. 1

    ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

  2. 2

    ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

  3. 3

    Does __asm{}; return the value of eax?

  4. 4

    Gcc inline ASM, set EBX to char array address using LEA

  5. 5

    INT vs CALL on Asm instructions

  6. 6

    INT vs CALL on Asm instructions

  7. 7

    Why does GCC put a no-op push/pop when using int foo asm("ebx") to pin a var to a register?

  8. 8

    g++ -no optimization- skips asm code after goto

  9. 9

    g++ inline asm not working as expected with optimization flag

  10. 10

    Anaconda vs miniconda space

  11. 11

    SQL Optimization WHERE vs JOIN

  12. 12

    Optimization Algorithm vs Regression Models

  13. 13

    Numba vs Cython loop optimization

  14. 14

    Cache Optimization - Hashmap vs QuickSort?

  15. 15

    Hard drive space vs Database space

  16. 16

    What are the benefits of using smaller registers, e.g. al vs eax / rax

  17. 17

    What are the benefits of using smaller registers, e.g. al vs eax / rax

  18. 18

    Unused Vs. Unallocated Space

  19. 19

    HeapSort vs MergeSort space complexity

  20. 20

    Java Optimization: Local Variable Vs Instance Variable

  21. 21

    Optimization of raw new[]/delete[] vs std::vector

  22. 22

    clang vs gcc - optimization including operator new

  23. 23

    Compiler optimization call-ret vs jmp

  24. 24

    MySQL optimization : index choice WHERE vs JOIN

  25. 25

    Optimization of naive matrix multiplication (ICC vs GCC)

  26. 26

    Compiler code optimization: AST vs. IR

  27. 27

    Writing JavaScript for minification vs. for optimization

  28. 28

    Optimization of sprites (quantity vs redundant transparency)

  29. 29

    Writing JavaScript for minification vs. for optimization

HotTag

Archive