Assembly Programming in Pine64
#1
Is it possible to program the pine64 in pure assembly?
If it is possible in which compiler can i program it and how can i upload my code to the board?
For example: Get 2 64 bit numbers from memory, add them in registers and store them in the memory?
#2
(04-13-2016, 05:29 AM)mustaphos Wrote: Is it possible to program the pine64 in pure assembly?
If it is possible in which compiler can i program it and how can i upload my code to the board?
For example: Get 2 64 bit numbers from memory, add them in registers and store them in the memory?

The pine uses an A64 SOC which incorporates a quad core A53 CPU. The A53 implements the Armv8-A architecture. (so much digging to get to the meat)

http://infocenter.arm.com/help/topic/com...p2_trm.pdf

https://static.docs.arm.com/den0024/a/DEN0024.pdf

Is it possible, I would imagine it is. However you are entering into a land with very few players, I'd be surprised to find more than 5 that bought a pine that have ever coded anything in assembler.  To further complicate matters you are going to have issues with other things connected to the CPU that use binary blobs instead of open source. Getting assembly level programming info for something closed source like the Mali 400, you can probably forget that. Not trying to sound discouraging just dont want you to get surprised. 

http://infocenter.arm.com/help/index.jsp has lots of relvant info, including compilers. Gno binutils should also possibly work. 

As for uploading code on the board, if you are planning on running just your assembled code you are going to need info on the boot system etc and other parts that I dont know if you will get a lot of success with. Good luck!
#3
Well, it depends on what you mean.

Bear in mind, the normal way pine64 is run is you are running an operating system (written in C with some assembly thrown in, perhaps in C++) kernel that runs programs on demand. Could you replace the operating system with something you wrote from scratch? You could, but it might take person-years if not person-decades to get all of the interfaces correct. I suspect by the time you finish, this version of the pine64 will have been replaced by the next generations. If you have not programmed in assembly before, it may be too complex to do anything but simple toy programs.

If you really want to do this, perhaps using a different ARM board that is setup to download code from a host, and has debug support via JTAG or other means built-in would be simpler than using a system setup to run a normal OS. Note, even in this arena it is extremely rare that the whole system is written in assembly. Normally only bits and pieces are written in assembly, and everything else is written in higher level languages like C/C++ (and yeah, I know C is not particularly high level compared to other languages, but it still is a lot higher than assembly language).

Ok, maybe writing a complete OS isn't your cup of tea, could you write a Linux/Android program completely in assembly language? Sure. Not only would you have to learn the ARM assembly language, you would have to replicate the whole interface between the operating system, and the programs that is normally handled by the standard libraries. This is doable, but it is perhaps a bit of work tracking down all of the interface details. This might be 1-2 person-weeks worth of effort if you are already familiar with ARM assembly and just need to figure out the details on the pine64 system, or it might be several person-months of effort if you have not have programmed in assembly before (as it appears from your question).

Now, if you just wanted to learn ARM assembly, and are willing to use the standard libraries, then it is possible, and in fact is probably the simplest method to learn assembly. First write a program in C, and compile it with gcc -O2 -S foo.c, and it will provide a foo.s file. Hack on this to your hearts content, using gcc -o foo.exe foo.s to build it, and then run foo.ex (or whatever name you call it). You can use the debugger (gdb) to place breakpoints at locations, you can single step at the instruction level, and look at registers. You will need to figure out the application binary interface (ABI) to learn what the conventions for the stack layout, what registers are used for passing/return parameters, what registers are preserved across calls, etc.

You can also write assembly code that is a function called from C/C++, rather than the whole program.

You can also use GCC's inline assembly feature (https://gcc.gnu.org/onlinedocs/gcc-5.3.0...age-with-C).
#4
(04-13-2016, 06:14 AM)rahlquist Wrote: To further complicate matters you are going to have issues with other things connected to the CPU that use binary blobs instead of open source. Getting assembly level programming info for something closed source like the Mali 400, you can probably forget that. Not trying to sound discouraging just dont want you to get surprised.

You've hit the nail on the head with that statement. Bare Metal programming is possible on any system, and I've used assembler on ARM since the Archimedes days. But without knowing how the memory controller, video controller and all the other hardware is addressed by the CPU it will be difficult to do anything.

I do disagree about how many people would be interested in doing this though, there are lot of metal heads out there.....
#5
(04-13-2016, 06:14 AM)rahlquist Wrote:
(04-13-2016, 05:29 AM)mustaphos Wrote: Is it possible to program the pine64 in pure assembly?
If it is possible in which compiler can i program it and how can i upload my code to the board?
For example: Get 2 64 bit numbers from memory, add them in registers and store them in the memory?

The pine uses an A64 SOC which incorporates a quad core A53 CPU. The A53 implements the Armv8-A architecture. (so much digging to get to the meat)

http://infocenter.arm.com/help/topic/com...p2_trm.pdf

https://static.docs.arm.com/den0024/a/DEN0024.pdf

Is it possible, I would imagine it is. However you are entering into a land with very few players, I'd be surprised to find more than 5 that bought a pine that have ever coded anything in assembler.  To further complicate matters you are going to have issues with other things connected to the CPU that use binary blobs instead of open source. Getting assembly level programming info for something closed source like the Mali 400, you can probably forget that. Not trying to sound discouraging just dont want you to get surprised. 

http://infocenter.arm.com/help/index.jsp has lots of relvant info, including compilers. Gno binutils should also possibly work. 

As for uploading code on the board, if you are planning on running just your assembled code you are going to need info on the boot system etc and other parts that I dont know if you will get a lot of success with. Good luck!

Yep - very few players and I am one of them that is crazy enough to do some kind of basic Assembly coding daily. However, I generally don't do bare-metal other that the one time on a Tiva-C with a simple boot up just to make me appreciate the real hard working kernel and OS Jedis out there. 

I purchased the Pine64 to run and OS but to specifically lean ARMv8 Assembly. I know ARMv6/7 and have worked on Raspberry Pi 2, Tivia-C, and a rented Cortex-A9 server from Scaleway. I also know Bash, Python, Haskell, Awk, Sed; however, my computer knowledge improved while learning x86, x86-64, and ARM. It's also very therapeutic, imo. Smile

(04-13-2016, 06:54 AM)MichaelMeissner Wrote: Well, it depends on what you mean.

Bear in mind, the normal way pine64 is run is you are running an operating system (written in C with some assembly thrown in, perhaps in C++) kernel that runs programs on demand. Could you replace the operating system with something you wrote from scratch? You could, but it might take person-years if not person-decades to get all of the interfaces correct. I suspect by the time you finish, this version of the pine64 will have been replaced by the next generations. If you have not programmed in assembly before, it may be too complex to do anything but simple toy programs.

If you really want to do this, perhaps using a different ARM board that is setup to download code from a host, and has debug support via JTAG or other means built-in would be simpler than using a system setup to run a normal OS. Note, even in this arena it is extremely rare that the whole system is written in assembly. Normally only bits and pieces are written in assembly, and everything else is written in higher level languages like C/C++ (and yeah, I know C is not particularly high level compared to other languages, but it still is a lot higher than assembly language).

Ok, maybe writing a complete OS isn't your cup of tea, could you write a Linux/Android program completely in assembly language? Sure. Not only would you have to learn the ARM assembly language, you would have to replicate the whole interface between the operating system, and the programs that is normally handled by the standard libraries. This is doable, but it is perhaps a bit of work tracking down all of the interface details. This might be 1-2 person-weeks worth of effort if you are already familiar with ARM assembly and just need to figure out the details on the pine64 system, or it might be several person-months of effort if you have not have programmed in assembly before (as it appears from your question).

Now, if you just wanted to learn ARM assembly, and are willing to use the standard libraries, then it is possible, and in fact is probably the simplest method to learn assembly. First write a program in C, and compile it with gcc -O2 -S foo.c, and it will provide a foo.s file. Hack on this to your hearts content, using gcc -o foo.exe foo.s to build it, and then run foo.ex (or whatever name you call it). You can use the debugger (gdb) to place breakpoints at locations, you can single step at the instruction level, and look at registers. You will need to figure out the application binary interface (ABI) to learn what the conventions for the stack layout, what registers are used for passing/return parameters, what registers are preserved across calls, etc.

You can also write assembly code that is a function called from C/C++, rather than the whole program.

You can also use GCC's inline assembly feature (https://gcc.gnu.org/onlinedocs/gcc-5.3.0...age-with-C).

Good words!
#6
If you want to do bare metal on pine64, start by looking at uboot, as it's the first thing that's run after (still) proprietary boot0 code from allwinner.
#7
Assembly language programming is my favorite! I don't know where to look to find the specs I need to get started.
#8
(05-13-2016, 12:08 PM)dmartin Wrote: Assembly language programming is my favorite!  I don't know where to look to find the specs I need to get started.

You can explore with Sunxi community, https://linux-sunxi.org
#9
Very late reply - but if anybody is still interested - I'm one of the '5'.

the very very basic - get started in assembler on pine 64:

Set up the pine 64 with debian - not going near bare bones here - that's way down the road.

open a terminal session.

at the $ type : (and note the spaces at the beginning of the third and fourth lines.
cat > test1.s
.global main
main:
move x0, #3
blr x30
^D (i.e hold ctrl and hit 'D')
... and you get your $ back
.. and you can use vi or any other editor to create the program, of course.

So we have typed a two instruction 64-bit assembly language program which has an entry point defined (main) and exits back to the calling program (we'll see why in a mo...).

test it by calling the assembler :
as -o test1.o test1.s
(fix any errors that occur - should be just typos)
then link it to the core c libraries for basic calling and return support
gcc -o test1 test1.o
(note simply typing 'make test1' will actually do the same as both of these - but explaining why is a whole new topic - and in this case obscures the two step assemble then link process)
and finally run it :
./test1
if you've done it right you see the $ again.
to verify the program did something type
echo $?
and you should see a '3'
(this is the #3 from our third line.)

OK so what's happenned here ?
we declared a program location called main as global - this means other things can see it - calling it main is key as that is what the linker needs to find to pass control to.
Then we defined where main should start executing.
We then loaded the number 3 into the first of the 31 64-bit registers (called X0) - and we did this because when our program finishes that is where the system expect to find our return code. Then we executed a branch to a location in the 31st 64 bit register (called X30)- and we did this because that is where we expect the OS to have put the address to which we return when finished.
... and it worked because the program completed and we got our $ prompt back and when we checked the return code we saw a three which we had intentionally sent back as the return code in x0.

OK so it does all work - but to progress you need to learn the ARMv8-A assembly language and you need to understand the structure of the processor.
check the two pdf links in rahlquist's post above and also find :
ARMCT_armasm_reference_guide_v6_01_DUI0802B_en.pdf (it's on the Arm site - use google).

Ok so from here on it's just learning the assembly language - and the debugger (gdb) so you can see what's happenning - and you also need to understand that there are rules you have to follow - in the same way that the X0 is where we have to put our return code and X30 is where we have to return to - you need to ensure that don't overwrite your own return address or you can't successfully return control to the OS - there are docs out there which will help. And the slightly good news is that the linker links in some key c routines like printf and scanf which allows our program to get user input and send user output by calling these routines with the approriate values in registers/memory.

Typed into midori while validating it live on my Pine 64.

Only found this post when I was trying my pine 64 with some Arm 7 code I had written for a Raspberry Pi - which failed miserably a few minutes ago - and then I realised duh - 64 bit. But migrating in principal is not a big deal - but in detail the assembly language is quite different.

There is a very good intro to Arm 7 for Raspberry pi at
http://thinkingeek.com/2013/01/09/arm-as...chapter-1/
clearly the assembly language is different - but the key messages in the first few chapters are the same.
(the docs above outline the differences between the 32bit and 64bit instructions sets)

Finally - I don't believe this will work on a RasPi3 - I believe that it runs in the 32-bit mode -and mode switching is a few steps down the road - but I will check now that my curiosity is engaged.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Programming languages support under PINE64 baryluk 6 13,287 09-23-2020, 11:46 PM
Last Post: Phillip Bell
  Manjaro arm on pine64+ roel 2 6,452 10-19-2019, 05:13 AM
Last Post: roel
  Volumio for PINE64 Released mikelangeloz 11 26,209 03-22-2019, 09:25 AM
Last Post: llungster
  I2S Audio patches for Pine64+ uploaded to GitHub ramstadt 1 4,395 01-19-2019, 11:31 AM
Last Post: ramstadt
  Pine64: Minimal SDL config Max11 3 6,556 01-04-2019, 03:47 PM
Last Post: Max11
  Fedora 27 on Pine64 gregjo 18 37,976 01-02-2019, 10:42 PM
Last Post: heatfanjohn
  Official build procedure of ayufan pine64 r.tanaka 0 3,322 08-21-2018, 10:54 PM
Last Post: r.tanaka
  DKMS on kernel 3.10.107-pine64 obrienmd 5 9,471 06-21-2018, 05:45 PM
Last Post: evilbunny
  Gentoo for pine64 - longsleep kernel incoherent 0 3,859 12-09-2017, 08:14 AM
Last Post: incoherent
  Fedora Running On Pine64! cztian 26 40,979 10-26-2017, 05:51 PM
Last Post: gregjo

Forum Jump:


Users browsing this thread: 1 Guest(s)