NASM addressing problem
Posted: Mon May 12, 2008 3:36 am
With addressing with bp+x (x-128<x<x+128) you can generate smaller code in x86 asm language.
ErrorValue is defined in the very beginning of the file. Moving of this definition to the end of the file doesn't matter.
ErrorValuePTR is inside the code:
Addresses: bp_size = 20h (defined constant), ErrorValuePTR=7C59h (calculated address)
Zo result of the calculation of ErrorValue is 79h
This value is small enough, to generate one byte less code:
inc byte [bp+79h] is 3 bytes of code.
inc byte [bp+bigvalue] or inc byte [value] is 4 bytes of code.
The problem is Nasm generates 4 bytes of code. When I define ErrorValue manually with value 79h, everything is fine, I get the shorter code.
When I replace (ErrorValuePTR - 7C00h) with 59h, everything is also fine.
Generates for inc byte [ErrorValue]: fe 86 79 00
Generates for inc byte [ErrorValue]: fe 46 79
I prefer to use ErrorValue and definition with ErrorValuePTR because the address of the address of ErrorValuePTR can change a little bit (but within reach of bp-7fh and bp+7fh)
How can Nasm understand that my definition can use the smaller code?
Code: Select all
inc byte [ErrorValue]
Code: Select all
%define ErrorValue bp + bp_size + (ErrorValuePTR - 7C00h)
Code: Select all
show_error_LE: call Print
db 'Error '
ErrorValuePTR: db '1',0
Zo result of the calculation of ErrorValue is 79h
This value is small enough, to generate one byte less code:
inc byte [bp+79h] is 3 bytes of code.
inc byte [bp+bigvalue] or inc byte [value] is 4 bytes of code.
The problem is Nasm generates 4 bytes of code. When I define ErrorValue manually with value 79h, everything is fine, I get the shorter code.
When I replace (ErrorValuePTR - 7C00h) with 59h, everything is also fine.
Code: Select all
%define ErrorValue bp + bp_size + (ErrorValuePTR - 7C00h)
Code: Select all
%define ErrorValue bp + bp_size + 59h ; (ErrorValuePTR - 7C00h) replaced
I prefer to use ErrorValue and definition with ErrorValuePTR because the address of the address of ErrorValuePTR can change a little bit (but within reach of bp-7fh and bp+7fh)
How can Nasm understand that my definition can use the smaller code?