AArch64, load a constant to register lazarus / 21-General ---------------------------------------------------------------------- LemonParty · 2025-07-02 17:32 Hello. I have a constant: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  CArray: array [0..15] of Byte = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);How do I load this constant into q0? ---------------------------------------------------------------------- Thaddy · 2025-07-03 06:41 Usually what I do is write it in pascal and compile with -a, the examine the  xx.s file to give me some idea on how the compiler handles this. Specifically for AARCH64 in which I am kind of a beginner. This leads often to two options: - compiler is fine. leave it alone and keep the pascal code - (mainly double loads) I see room for improvement and optimize and test it. Untested example from DeepSeek:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program LoadQ0Example;{$mode objfpc}{$asmmode advanced} // Align to 16 bytes for optimal performance{$ALIGN 16}const  CArray: array [0..15] of Byte = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16); // Another aligned array for verification{$ALIGN 16}var  VerifyArray: array [0..15] of Byte; procedure LoadToQ0; assembler;asm  // Load array address into x0  ldr x0, =CArray                      //<---------    // Load 16 bytes into q0  ldr q0, [x0]                          //<----------    // Optional: Store back to VerifyArray for testing  ldr x1, =VerifyArray  str q0, [x1]end; var  i: Integer;begin  LoadToQ0;    // Verify the transfer  WriteLn('Contents of q0:');  for i := 0 to 15 do    Write(VerifyArray[i], ' ');end.Looks OK. Let me know if it works. Will test later on a Raspberry Pi. I added arrows to the 2 instructions you need and I know that part is correct. ---------------------------------------------------------------------- H₂SO₄ · 2025-07-03 07:37 AArch64 supports PC-relative addressing (analogously called RIP- relative addressing in x86-64) through the  adr  and  adrp  instructions. There's a catch, however: adr  has a range of only +/- 1 MiBadrp  has a range of +/- 4 GiB, but only operates on page granularity (4 KiB)This means that loading an arbitrary address generally requires at least two instructions: one for the page's base and one for the offset within that page. For this purpose FPC's assembler provides two operand suffixes,  @PAGE  and  @PAGEOFF,  which split the operand's address into the relevant parts. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$codealign constmin=16} const  CArray: array [0..15] of Byte = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16); asm  adrp  x0, CArray@PAGE  ldr   q0, [x0, CArray@PAGEOFF]  // ...end; Note that  q0  and the other vector registers can only be used in FPC versions that incorporate this commit from October 15, 2020 since prior to it some register definitions in  compiler/aarch64/ra64num.inc  were wrong (e.g.  q0's  subtype was declared as  R_SUBQ  when it should be  R_SUBMMWHOLE), leading to an internal error. Unfortunately this commit didn't make it into any official release tag yet, so you'll have to use trunk if you want to use the 128-bit vector registers. ---------------------------------------------------------------------- Thaddy · 2025-07-03 08:21 Luckily OP is using trunk/main according to his signature. ---------------------------------------------------------------------- LemonParty · 2025-07-03 12:34 @Thaddy, your code gives this error: QuoteFatal: Illegal character "'='" @Khrys Your code work. Thank you. What @PAGE and @PAGEOFF in your code mean? ---------------------------------------------------------------------- Thaddy · 2025-07-03 13:59 Yes, that failed on the pseudo instruction =. Corrected:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program LoadQ0Example;{$mode objfpc}{$asmmode advanced} // Align to 16 bytes for optimal performance{$ALIGN 16}const  CArray: array [0..15] of Byte = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16); // Another aligned array for verification{$ALIGN 16}var  VerifyArray: array [0..15] of Byte; procedure LoadToQ0; assembler; nostackframe;asm  // Load array address using adrp+add (position-independent)  adrp x0, CArray  add x0, x0, :lo12:CArray    // Load 16 bytes into q0  ldr q0, [x0]    // Optional: Store back to VerifyArray for testing  adrp x1, VerifyArray  add x1, x1, :lo12:VerifyArray  str q0, [x1]end; var  i: Integer;begin  LoadToQ0;    // Verify the transfer  WriteLn('Contents of q0:');  for i := 0 to 15 do    Write(VerifyArray[i], ' ');  WriteLn;end.@Khrys is right. ---------------------------------------------------------------------- LemonParty · 2026-08-13 10:31 What if I have this constant:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  CArray: array [0..31] of Byte = (1,2,3,4,5,6,7,8,9,10,11 ,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32); And I want to load the content of the constant starting from element 16 into q0.