NASM Forum > Programming with NASM

Accessing pushed value from a label

<< < (2/3) > >>

Frank Kotler:
Dunno. Does it do what you want?

I think you might be better off to use si and quit fighting it... maybe...

Best,
Frank

fredericopissarra:
My point: Instead trying to remember if you should add or subtract N from the stack pointer to access an argument pushed on stack, use structures... And more: Using BP to access the stack was wildely used on 8086 and 80286 because there were no other way to do it... Since 386 you can use ESP to access the stack directly.

The prologue/epilogue:


--- Code: ---  push bp
  mov  bp,sp    ; prologue
  ...
  pop   bp        ; epilogue

  ret
--- End code ---
Aren't necessary anymore, if you use ESP to access the stack.

Yep... ESP is 32 bits long, but in 16 bits mode the upper 16 bits os E?? will be discarded in an effective address calculation.

Of course, using ESP will add a prefix (0x67 maybe) and even a longer opcode, but this is faster then saving/retrieving BP to/from the stack.

The same argument can be made for using 32 bits registers as base/index on an effective address calculation, instead of the fixed [BX+(SI/DI)+offset]... Since we are free to use any registers (including repeating them) as base and index, the code tends to be shorter. And remember that 386+ has a scaling of indexes as well (not available on 80(2)86).

Simple example: Multiplying a register by 10:

--- Code: ---; Multiply EAX by 10;
  lea eax,[eax+eax*4]  ; multiply by 5
  shl eax,1 ; multiply by 2
--- End code ---
This LEA isn't possible on pre-386 80x86 family processors. To do the same with AX on 8086 code:

--- Code: ---  mov cl,2
  mov bx,ax
  shl ax,cl          ; There is no SHL AX,imm8 instruction on 80(2)86
  add ax,bx
  shl ax,1           ; except this one!
--- End code ---

In general, using 32 bits registers on 16 bits code leads to better code.

[]s
Fred

Frank Kotler:
Hi Fred,

Golly, I'm losing it! I didn't realize that it was you I was addressing last time.

I agree that 386 registers are way more convenient. Tyson says he wants to stick with 16-bit registers so it'll run on older hardware. In a museum? In the basement? Whatever.

But that's a slightly different question than accessing the stack through a label/ >aybr ovrtlaying the stack with a structure name as you show is what Tyson's looking for.

Best,
Frank

fredericopissarra:

--- Quote from: Frank Kotler on July 27, 2022, 04:42:57 PM ---Hi Fred,

Golly, I'm losing it! I didn't realize that it was you I was addressing last time.

I agree that 386 registers are way more convenient. Tyson says he wants to stick with 16-bit registers so it'll run on older hardware. In a museum? In the basement? Whatever.

--- End quote ---

Hehehehehehehe... Maybe Tyson have a time machine and want to go back to the 1980's... I've seen "stranger things"... :)

--- Quote ---But that's a slightly different question than accessing the stack through a label/ >aybr ovrtlaying the stack with a structure name as you show is what Tyson's looking for.
--- End quote ---
Maybe that helps...

[]s, Frank!
Fred

Frank Kotler:

--- Quote from: fredericopissarra on July 27, 2022, 06:58:58 PM ---
Hehehehehehehe... Maybe Tyson have a time machine and want to go back to the 1980's... I've seen "stranger things"... :)

--- End quote ---

Maybe I can hitch a ride with  him. I was a young fellow in the 1980's. Not like now with my
memory all shot. :(

Best,
Frank


.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version