Author Topic: Question about offsets in assembly language  (Read 313 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Hero Member
  • *****
  • Posts: 826
Question about offsets in assembly language
« on: January 25, 2019, 05:18:45 AM »
Hi Charles,

I try to run \examples\Asm32\AddrVar.o2bas in 64-bit mode. It seems I cannot replace the number of [ecx+4] with a constant or variable. Is there a way how I could make function f1 or f2 work correctly?

Roland

Code: [Select]
$ filename "AddrVar1.exe"

'uses rtl32
'uses rtl64

uses console

function f(sys*a) as sys
sys p=@a
mov ecx,p
#ifndef mode64bit
  return [ecx+4]
#else 
  return [ecx+8]
#endif 
end function

% ofs=sizeof(sys)

function f1(sys*a) as sys
sys p=@a
mov ecx,p
return [ecx+ofs]
end function

function f2(sys*a) as sys
sys p=@a
int offs=sizeof(sys)
mov ecx,p
return [ecx+offs]
end function

indexbase 1

sys aa[10]={1,2,3,4,5,6,7,8,9,10}

printl "f aa[2] = " f aa[2] 'result: 3
printl "f1 aa[2] = " f1 aa[2] 'result: 2
printl "f2 aa[2] = " f2 aa[2] 'result: 2

printl "Enter..." : waitkey

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: Question about offsets in assembly language
« Reply #1 on: January 25, 2019, 07:20:49 AM »
Hi Roland,

Code: [Select]
function f1(sys*a) as sys
addr rcx,a 'addr is pseudo-asm to resolve any address
add rcx,sizeof(sys)   'rcx is downgraded to ecx in 32bit
return [rcx]
end function


Brian Alvarez

  • Sr. Member
  • ****
  • Posts: 375
    • PluriBASIC
Re: Question about offsets in assembly language
« Reply #2 on: January 25, 2019, 07:42:50 PM »
...'rcx is downgraded to ecx in 32bit...

 Charles, is this a consistent rule to all other registers?

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: Question about offsets in assembly language
« Reply #3 on: January 25, 2019, 08:43:23 PM »
Yes, Brian. It's an o2 feature.

Except for R9..R15 which do not exist in 32bit mode

Arnold

  • Hero Member
  • *****
  • Posts: 826
Re: Question about offsets in assembly language
« Reply #4 on: January 26, 2019, 01:22:55 AM »

Thank you, Charles. Somehow I forgot about addr, although there are many examples with assembler code in the distribution. It is time to become more flexible again.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4097
    • Oxygen Basic
Re: Question about offsets in assembly language
« Reply #5 on: January 27, 2019, 02:35:55 AM »
To further facilitate 32bit/64bit compatibility, there is another similar rule:

When a 32bit register is used for addressing, it will be upgraded to its 64bit equivalent when in 64bit mode

thus:

mov eax,[ecx]

is interpreted as

mov eax,[rcx]