Good afternoon dear friends!
I found out that NASM is unable to optimize some kind of instruction even with -Ox option.
The problem is that NASM couldn't optimize a small-sized displacement to register if the displacement goven as an address.
Here is the brief sample code with detailed comments, plase have a look:
Code:
cpu 386
bits 16
[section .text]
org 0
Start:
; This instruction uses "Data" label as a displacement.
; But NASM doesn't recognize that the displacement is small enough to fit one byte.
; So even with -Ox option it is compiled to the non-optimal sequence.
cmp [bp+Data], cl ; Coded to 38 8E 16 00
; This instruction actually does the same, because the "Start" label is equal to 0.
; But since the difference between two addresses is non-address, NASM compiles it to the more optimal sequence
; The same result may be achieved if we write [bp+const] and define "const" to 7
cmp [bp+Data-Start], cl ; Coded to 38 4E 16
; The same situation is on the following two instructions. The first will not be optimized.
add dword [Data], Data ; Coded to 66 81 06 16 00 16 00 00 00
add dword [Data], Data-Start ; Coded to 66 83 06 16 00 16
Data: db 55h
Logged