Array Value into a Register

Bare metal programming in PureBasic, for experienced users
interfind
User
User
Posts: 22
Joined: Thu Apr 22, 2021 1:41 pm

Array Value into a Register

Post by interfind »

Hello,

how can i mov a Array Element into a Register?

Dim Array(10)
Array(0)=12
Array(1)=15
Array(2)=50
!mov r8, [a_myArray]
!movd xmm0, [a_myArray]
r8 or xmm0 should be 12 but it isn't.
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: Array Value into a Register

Post by AZJIO »

Code: Select all

Dim Array.l(10)
Array(0)=2
Array(1)=5
Array(2)=8
ShowMemoryViewer(@Array(0), 12)
CallDebugger
Jeff8888
User
User
Posts: 38
Joined: Fri Jan 31, 2020 6:48 pm

Re: Array Value into a Register

Post by Jeff8888 »

Here is how PB does it. Remember array indexes start at zero. The relevant assembly generated by PB for the 5 line program:

Dim a(10)
a(0)=0
a(5)=5
b=a(0)
c=a(5)

is as follows:

; a(0)=0
MOV rbp,qword [a_a]
MOV qword [rbp+0],0
; a(5)=5
MOV qword [rbp+40],5
; b=a(0)
MOV rax,qword [rbp+0]
MOV qword [v_b],rax
; c=a(5)
MOV rax,qword [rbp+40]
MOV qword [v_c],rax

Since you are using mixed PB and assembler PB will put the address of the array for you, here for example it is qword[a_a].
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Array Value into a Register

Post by juergenkulow »

Code: Select all

; register xmm0 = (double)Array[0] with register r8
Dim Array(10)
Array(0)=12
!  MOV    r8,qword [a_Array]
!  MOV    rax,qword [r8+0]
!  cvtsi2sd xmm0,rax 

; 0000000140001094 | 4C:8B05 05410000         | mov r8,qword ptr ds:[1400051A0]                               |
; 000000014000109B | 49:8B00                  | mov rax,qword ptr ds:[r8]                                     |
; rax=000000000000000C
; 000000014000109E | F248:0F2AC0              | cvtsi2sd xmm0,rax                                             |
; xmm0=00000000000000004028000000000000
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Array Value into a Register

Post by juergenkulow »

Code: Select all

; x64 DoubleArray Value into Register xmm0
Dim DoubleArray.d(10)
DoubleArray(0)=12.125
! mov r8,qword [a_DoubleArray] ;Store address in register r8.
! movsd xmm0,[r8]              ;Store the 8 byte double content of the address in register xmm0.
! movsd [r8+8],xmm0            ;Store the 8 byte double content of register xmm0 in DoubleArray(1).
Debug DoubleArray(1)
MOVSD — Move or Merge Scalar Double-Precision Floating-Point Value
Post Reply