Author Topic: call dword PTR[randomptr]  (Read 15422 times)

Offline bastl

  • Jr. Member
  • *
  • Posts: 17
call dword PTR[randomptr]
« on: December 21, 2020, 09:48:39 AM »
I can not find a clear answer, how to use the PTR-bit of an instruction in NASM.

For OOP you need to store addresses at addresses:
In NASM you have a address "randomptr" and there is stored a second address.
So randomptr points to dword on a 32bit system and qword on a 64-bit system.

Now you have to set the PTR bit of the CALL instruction to call the address that is stored at the given address.

I would do it like this:

call dword ptr[randomptr]    32-bit-sys
call qword ptr[randomptr]    64-bit-sys

So CALL calls the member that is stored at randomptr!
And if NASM knows the system, mybe with USE 32 directive  you could write

call ptr[randomptr]    ,all is deffined !

Tasm can, but how does NASM handle the PTR-bit ?

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: call dword PTR[randomptr]
« Reply #1 on: December 21, 2020, 03:11:15 PM »
There is no PTR reserved word in NASM. These calls must be:
Code: [Select]
  call dword [address] ; 32 bits.
  call qword [address] ; 64 bits.

If you are using 'bits 64' or 'bits 32', "call [address]" works accordingly.
« Last Edit: December 21, 2020, 03:13:17 PM by fredericopissarra »

Offline bastl

  • Jr. Member
  • *
  • Posts: 17
Re: call dword PTR[randomptr]
« Reply #2 on: December 27, 2020, 09:16:45 PM »
Yes, I can not find anything how to set the ptr-bit of an instruction in nasm.
Are the developer of nasm are sleeping or way don't they support it??
it is so frustrating, loading the value of the address in a register to access the address that is pointing to the value I need.

And sorry, fredericopissarra, your code does not do what ptr does !!!
read exactly what the problem is.
Here again:
Code: [Select]
mov   ebx,[ptr_address]
mov   eax,[ebx]
is the same as:
Code: [Select]
mov   eax,dword ptr[ptr_address]
With a assembler directive (USE 32) you don't need the "dword", eax only defines the length of the last value!
You waste CPU zycles without ptr. And if you have a special task you get extremely slow.

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: call dword PTR[randomptr]
« Reply #3 on: December 28, 2020, 12:31:09 PM »
Yes, I can not find anything how to set the ptr-bit of an instruction in nasm.
Because there are none. What is a 'ptr-bit'?

Quote from: bastl
it is so frustrating, loading the value of the address in a register to access the address that is pointing to the value I need.
That how it works.

Quote from: bastl
And sorry, fredericopissarra, your code does not do what ptr does !!!
read exactly what the problem is.
It helps if you were able to explain the problem coherently. As I undestand now, what you are trying to do is to call a virtual function through an entry in a virtual table which pointer is at the begining of an instance of a object, pointed by 'ptr_address'. But even now, I'm not sure...

If I understand correctly, you have something like this:
Code: [Select]
class X {
public:
  virtual void f( void );
  virtual void g( void );
};
...
X *p = new X;
p->g();
delete p;
...
Assuming the pointer to the virtual table is at the begining of the object structure and then you must do:
Code: [Select]
  ...
  call   newX
  mov  ebx,eax   ; EBX is preserved between calls.
  ...
  mov  eax,[ebx]   ; Get the pointer of the vtable.

  push ebx            ; push the 'this' pointer.
  call   [eax+4]     ; Call the function g() via its pointer.
  ...
  push ebx
  call   delete
  ...

There is no way to do a double indirection in x86 assembly.

Quote from: bastl
With a assembler directive (USE 32) you don't need the "dword", eax only defines the length of the last value!
You waste CPU zycles without ptr. And if you have a special task you get extremely slow.
I know about the default size of addresses accordingly to the model in use. And there is no way you can spare this cycles due to the double indirection AND, possibly, a penalty from the dynamic branch prediction algorithm.
« Last Edit: December 28, 2020, 01:30:08 PM by fredericopissarra »

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: call dword PTR[randomptr]
« Reply #4 on: December 28, 2020, 08:28:47 PM »
Yes, I can not find anything how to set the ptr-bit of an instruction in nasm.
Are the developer of nasm are sleeping or way don't they support it??
it is so frustrating, loading the value of the address in a register to access the address that is pointing to the value I need.

And sorry, fredericopissarra, your code does not do what ptr does !!!
read exactly what the problem is.
Here again:
Code: [Select]
mov   ebx,[ptr_address]
mov   eax,[ebx]
is the same as:
Code: [Select]
mov   eax,dword ptr[ptr_address]
With a assembler directive (USE 32) you don't need the "dword", eax only defines the length of the last value!
You waste CPU zycles without ptr. And if you have a special task you get extremely slow.

nasm uses instructions known to the CPU. It does not use instructions, directives, etc that are known to most higher level compilers. You may be able to determine something called a "ptr-bit" in your chosen high level compiler, but that is just a directive that is internal to that compiler, and is calculated when needed. It is not defined by or for the CPU.

Nasm does allow the use of pointers, you just need to read up on how the instructions you want to use them with implement the pointer function you want. That is not the job of an assembler, whose sole job is to convert strings of code into code the CPU understands. It's the programmer's job to know how to implement things. Otherwise you are simply looking for a high level compiler.

If you read the nasm manual, you might be surprised just how powerful it really is when you know how top write appropriate code or describe what you need.
My graphics card database: www.gpuzoo.com

Offline bastl

  • Jr. Member
  • *
  • Posts: 17
Re: call dword PTR[randomptr]
« Reply #5 on: December 30, 2020, 09:53:45 PM »
Oh, I'm so sorry. I'm gone though my 25 years old tasm code and I did something different there.
X86 instructions can not do, what I expected - Sorry for this.
NASM is the worlds best assembler...

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: call dword PTR[randomptr]
« Reply #6 on: December 31, 2020, 12:34:22 AM »
Oh, I'm so sorry. I'm gone though my 25 years old tasm code and I did something different there.
X86 instructions can not do, what I expected - Sorry for this.
NASM is the worlds best assembler...

If you can show a sample of what you would write in tasm, one of us might better understand and be able to show you how to write the same for nasm.

The main thing I like about nasm is that it is up to date (in as much as it support all x86 and x64 instructions as understood by any and all x86 compatible CPUs - not sure about Itanium though, I've never looked at that) and is regularly updated with new features (such as structure support, macros, etc). It's not the only modern assembler though, just the one I learned most of my coding on.
My graphics card database: www.gpuzoo.com