kc5vdj
Posts: 2
Joined: Mon Apr 09, 2018 5:04 am

low cost or free assembler books / tutorials?

Mon Apr 09, 2018 5:15 am

Does anyone know of any?

I found a tutorial video series on youtube, but the dude skips a LOT, and doesn't explain a lot. His gives some basics, but doesn't quite explain the addressing modes, and assembler syntax for such.

The video series I mention can be found starting with this video: https://www.youtube.com/watch?v=ViNnfoE56V8

I have done assembly before, mainly intel (microsoft syntax), 6502 and z80, but i'm just not familiar largely with the syntax and notations for addressing, etc for arm.

jahboater
Posts: 2914
Joined: Wed Feb 04, 2015 6:38 pm

Re: low cost or free assembler books / tutorials?

Mon Apr 09, 2018 6:40 am

This book is quite a good introduction and reasonably priced:
https://www.amazon.co.uk/ARM-A32-Assemb ... dpSrc=srch

This one is more expensive, but is very good indeed:
https://www.amazon.co.uk/ARM-Assembly-L ... b_image_bk

Google finds this overview, which is easy to read:
https://www.element14.com/community/ser ... Manual.pdf

and this which is the full ARMv8 Reference manual - which should be every assembler programmers bible:
https://static.docs.arm.com/ddi0487/ca/ ... v8_arm.pdf

Other than that, just try GCC to get idea's about how to do things:-

gcc -Os -S -fverbose-asm hello.c -o hello.s

and look at the .s file.
(better with gcc 7 as you can see the original C source lines mixed in with the assembler)

kc5vdj
Posts: 2
Joined: Mon Apr 09, 2018 5:04 am

Re: low cost or free assembler books / tutorials?

Mon Apr 09, 2018 8:39 am

I'll take a look at those. As far as the last one there, I currently only have the Zero W in the C4Labs Zebra Zero Plus "case" (the one with the half breadboard), and do have the armv6 manual, as well as the broadcom manual, as well as the arm assembler manual.

I hadn't thought of using assembler listings from gcc, I may give that a try. I currently only have the default gcc / gfortran installed, i'll have to check version.
jahboater wrote:
Mon Apr 09, 2018 6:40 am
This book is quite a good introduction and reasonably priced:
https://www.amazon.co.uk/ARM-A32-Assemb ... dpSrc=srch

This one is more expensive, but is very good indeed:
https://www.amazon.co.uk/ARM-Assembly-L ... b_image_bk

Google finds this overview, which is easy to read:
https://www.element14.com/community/ser ... Manual.pdf

and this which is the full ARMv8 Reference manual - which should be every assembler programmers bible:
https://static.docs.arm.com/ddi0487/ca/ ... v8_arm.pdf

Other than that, just try GCC to get idea's about how to do things:-

gcc -Os -S -fverbose-asm hello.c -o hello.s

and look at the .s file.
(better with gcc 7 as you can see the original C source lines mixed in with the assembler)

jahboater
Posts: 2914
Joined: Wed Feb 04, 2015 6:38 pm

Re: low cost or free assembler books / tutorials?

Mon Apr 09, 2018 9:30 am

The Pi Zero W is fine ...

On my Pi Zero, this C program

Code: Select all

#include <stdio.h>

int
main( void )
{
  printf( "Hello, world\n" );
}

Code: Select all

gcc -Os -S -fverbose-asm hello.c -o hello.s
produces (with the red tape removed) ....

Code: Select all

main:
    push    {r4, lr}    
@ hello.c:6:   printf( "Hello, world\n" );
    ldr r0, .L3 
    bl  puts    
@ hello.c:7: }
    mov r0, #0  
    pop {r4, pc}    
.L3:
    .word   .LC0
    .section    .rodata.str1.1,"aMS",%progbits,1
.LC0:
    .ascii  "Hello, world\000"

dwelch67
Posts: 944
Joined: Sat May 26, 2012 5:32 pm

Re: low cost or free assembler books / tutorials?

Mon Apr 09, 2018 6:12 pm

printf and other C library calls are very uninteresting for disassembling. I recommend compile to object then disassemble rather than compile to assembly. They maximize the ghee whiz features of gnu assembler between the compiler and assembler, but if you disassemble all that fluff is gone you have just real instructions. sure you sometimes lose labels, etc, but you can then figure that out from the arm docs or work backward through the assembly. you can just do -save-temps with gcc rather than -S. Also optimize -O2 to remove a lot of the fluff that gets in the way, granted it removes a lot of dead code, so there is some practice required building test functions to compile then disassemble, basically make simple function with variable inputs and have them either get passed on to an external function or have them combine into a return value, otherwise they will likely get optimized out in some way.

unsigned int more_fun ( unsigned int, unsigned int );

unsigned int fun0 ( unsigned int a, unsigned int b )
{
return(a+b+1);
}
unsigned int fun1 ( unsigned int a, unsigned int b )
{
return(more_fun(a+1,b+2));
}


arm-none-eabi-gcc -O2 -save-temps -c fun.c -o fun.o
arm-none-eabi-objdump -D fun.o

fun.o: file format elf32-littlearm


Disassembly of section .text:

00000000 <fun0>:
0: e2811001 add r1, r1, #1
4: e0810000 add r0, r1, r0
8: e12fff1e bx lr

0000000c <fun1>:
c: e92d4010 push {r4, lr}
10: e2811002 add r1, r1, #2
14: e2800001 add r0, r0, #1
18: ebfffffe bl 0 <more_fun>
1c: e8bd4010 pop {r4, lr}
20: e12fff1e bx lr

fun0 shows the return value is passed in r0
fun1 shows that the first passed in parameter is in r0, the second in r1
these match the arm abi which you can read, but the compiler conforms to it so if you prototype the function you want to call, you can look at
the disassembly and see what registers are used.

my tools default to armv4t most likely so specify an arm11 the same or close to the one we are using

arm-none-eabi-gcc -O2 -save-temps -mcpu=arm1176jzf-s -c fun.c -o fun.o
arm-none-eabi-objdump -D fun.o

fun.o: file format elf32-littlearm


Disassembly of section .text:

00000000 <fun0>:
0: e2811001 add r1, r1, #1
4: e0810000 add r0, r1, r0
8: e12fff1e bx lr

0000000c <fun1>:
c: e2811002 add r1, r1, #2
10: e2800001 add r0, r0, #1
14: eafffffe b 0 <more_fun>

the tail optimization is in there, why they couldnt do that in the armv4 would have to think about it for a minute. but still shows a few things


unsigned int fun2 ( void )
{
return(0x12345678);
}

assume fixed length instructions so

00000000 <fun2>:
0: e59f0000 ldr r0, [pc] ; 8 <fun2+0x8>
4: e12fff1e bx lr
8: 12345678 eorsne r5, r4, #120, 12 ; 0x7800000

big immediates have to be or are generally loaded with pc relative addressing

which looks like this without tricks

ldr r0, .L3
...
.L3:
.word 305419896 (can use 0x12345678 here, the compiler generated decimal for some reason)

or the arm assembly languages (defined by the assembler of course, not arm) have a trick they support

ldr r0,=0x12345678

which is derived from letting the assembler load the address of a label

ldr r0,=my_label_name

which the assembler fills in later, finds a pool to place the constant in and generates the pc relative load for you...if it can find a pool near enough, sometimes you need/want to specify .pool in the gnu assembler code to help it. Usually dont need to specify the pool area(s).


BUT....if you already know one or more other assembly languages then the arm documentation should cover over 90% of what you need, a little bit of minimal gnu syntax, and you are there...once you learn one the others are mostly learning the syntax.

The pi-zero is definitely where you want to start, I would even argue thumb first then arm, but YMMV, you have to get into thumb mode which I imagine I have examples for if not can easily demonstrate. Save the armv8 (pi-3) 64 bit stuff for later, there is so much more complication that taking on the basics and the massive amount of complication due to modes, protection, etc greatly increases the chances of failure.

User avatar
DavidS
Posts: 3096
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: low cost or free assembler books / tutorials?

Tue Apr 10, 2018 1:25 pm

There are many good resources for learning ARM Assembly. Most target RISC OS, though it is easy enough to learn the API for what ever target you desire, then use asasm to assemble for your target (because it gives you the quite nicer syntax familiar to RISC OS assembly language, not the odd syntax of gas).

The ARM Wiki (Heyrick):
https://www.heyrick.co.uk/armwiki/Main_Page

For a good and very very verbose book that seems to assume that you have never touched a computer in your life, so covers ALL the details: Check out The book:
Raspberry Pi Assembly Language RISC OS By Bruce Smith.

Once you have those, take a look at information on the difference between 26 bit PC addressing and 32 bit PC Addressing here:
https://www.heyrick.co.uk/assembler/32rules.html
Here:
https://www.heyrick.co.uk/assembler/psr.html#32bit
And Here:
http://www.riscos.info/index.php/32bit

Then you can look at all the great older tutorials, that work in 26-bit PC modes, and know how to convert to work in 32-bit modes (the RPi 1/2 only have 32-bit, the RPi 3 has 32 or 64 bit). then you can look at:
https://www.heyrick.co.uk/assembler/

http://www.riscos.com/support/developer ... /index.htm

And many good tutorial articles in the mag Foundation RISC World:
http://www.apdl.org.uk/riscworld/

And many many more books, web sites, and articles.
26-Bit R15 to 32-bit. 16-bit addressing to 24-bit. ARM and 65xx two CPU's that continue on, and are better than ever. Assembly Language forever :) .

jahboater
Posts: 2914
Joined: Wed Feb 04, 2015 6:38 pm

Re: low cost or free assembler books / tutorials?

Wed Apr 11, 2018 7:00 am

DavidS wrote:
Tue Apr 10, 2018 1:25 pm
then use asasm to assemble for your target (because it gives you the quite nicer syntax familiar to RISC OS assembly language, not the odd syntax of gas).
The assembler on the Pi is "as" (aka gas).
"as" does not have any special syntax of its own.
It supports all variants of the official standard ARM assembly language, obviously including UAL, A32, T32, A64 to give them their proper names, and the old divided/thumb stuff. As specified by ARM holdings.

David, as (gas) on x86 does have the choice of ATT syntax (which might well be described as odd), or the normal Intel syntax. On ARM though, as only supports the ARM standard language.

User avatar
DavidS
Posts: 3096
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: low cost or free assembler books / tutorials?

Wed Apr 11, 2018 5:22 pm

jahboater wrote:
Wed Apr 11, 2018 7:00 am
DavidS wrote:
Tue Apr 10, 2018 1:25 pm
then use asasm to assemble for your target (because it gives you the quite nicer syntax familiar to RISC OS assembly language, not the odd syntax of gas).
The assembler on the Pi is "as" (aka gas).
"as" does not have any special syntax of its own.
It supports all variants of the official standard ARM assembly language, obviously including UAL, A32, T32, A64 to give them their proper names, and the old divided/thumb stuff. As specified by ARM holdings.

David, as (gas) on x86 does have the choice of ATT syntax (which might well be described as odd), or the normal Intel syntax. On ARM though, as only supports the ARM standard language.
How do you get as (Gnu as aka gas) to assemble the common syntax of !objasm, and the many other common RISC OS assemblers (where the arm began)?

Or at least something close there to, close enough that we only have to modify a couple of lines, not nearly every line in the source file. It is one thing to have to redo the lables, constants, and directives, it is another altogether to have to redo nearly all the instructions.

Since Objasm syntax predates what is now known as the ARM standard assembly, and more people are familiar with objasm syntax (and the very similar other RISC OS assemblers) because of RISC OS (the original ARM OS), I would consider it the standard, as it came from Acorn, and Acorn created the ARM.

I would like to know, as I am sure most RISC OS programmers would, there is a reason that RISC OS gcc comes with asasm as an alternative to as, because we need something that can assemble the objasm syntax sources, which as will not.
26-Bit R15 to 32-bit. 16-bit addressing to 24-bit. ARM and 65xx two CPU's that continue on, and are better than ever. Assembly Language forever :) .

jahboater
Posts: 2914
Joined: Wed Feb 04, 2015 6:38 pm

Re: low cost or free assembler books / tutorials?

Wed Apr 11, 2018 6:19 pm

DavidS wrote:
Wed Apr 11, 2018 5:22 pm
How do you get as (Gnu as aka gas) to assemble the common syntax of !objasm, and the many other common RISC OS assemblers (where the arm began)?
You cannot of course, there is no reason for as to support even more versions of ARM assembler syntax, especially something decades out of date.

But here is an idea. Why don't you write a simple pre-processor that takes !objasm and translates it to a modern assembler syntax, and then silently, in the background, calls as to produce the object code.
Its a simple textual transformation and should be fairly easy to write I would have thought, and fast.
Then you would have these RISC OS assemblers on Raspbian.

User avatar
DavidS
Posts: 3096
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: low cost or free assembler books / tutorials?

Wed Apr 11, 2018 6:47 pm

jahboater wrote:
Wed Apr 11, 2018 6:19 pm
DavidS wrote:
Wed Apr 11, 2018 5:22 pm
How do you get as (Gnu as aka gas) to assemble the common syntax of !objasm, and the many other common RISC OS assemblers (where the arm began)?
You cannot of course, there is no reason for as to support even more versions of ARM assembler syntax, especially something decades out of date.
Not as far out of date as the whole n*x concept on which Linux is built.

And I did not know that 2017 was decades ago. These assemblers are still being kept up to date, in many cases more up to date than the n*x counterparts.
But here is an idea. Why don't you write a simple pre-processor that takes !objasm and translates it to a modern assembler syntax, and then silently, in the background, calls as to produce the object code.
Its a simple textual transformation and should be fairly easy to write I would have thought, and fast.
Then you would have these RISC OS assemblers on Raspbian.
Good thought. Though we already have asasm available to Linux (including Raspbian), so redundant.
26-Bit R15 to 32-bit. 16-bit addressing to 24-bit. ARM and 65xx two CPU's that continue on, and are better than ever. Assembly Language forever :) .

dwelch67
Posts: 944
Joined: Sat May 26, 2012 5:32 pm

Re: low cost or free assembler books / tutorials?

Wed Apr 11, 2018 9:41 pm

assembly language is defined by the assembler the program that reads it. GNU has a bad habit of mangling languages created by the chip vendors, but that is fair since the assembler defines the assembly language. GNU assembler most definitely is not compatible with ARMs assembly language from ARMs tools, dont know a time when it ever has been. their unified syntax might be close, but still a long way from compatibility.

gnu still supports arm2/3 as I use it periodically, I assume arm1 as well then along with the armv4t and all the later stuff. not syntax certainly but can generate the instructions from the gnu assembler arm syntax. so long as you patch up the syntax to match gnu you might have a chance, but some of the arm1/2/3 instructions dont directly translate to arm4 and later, will have to replace that code if you hit it.

I recommend thumb first, probably already said that but you need some code to get into thumb mode. the unified syntax has made a huge mess of things, done more damage than good, I avoid it but there are thumb nuances in gnu assembler with and without unified you have to deal with.

depending on the version you can use .inst or .word or .hword or whatever a halfword is to insert machine code, assemble then disassemble and get a rough idea of what the gnu syntax is from machine code you created using the documentation. gnus disassembler for thumb can/will produce syntax that is not compatible with gnus own assembler so you have to deal with that. highly recommend you go back and forth between assembly and then disassemble the object/binary and examine the machine code.

I used to recommend learning a new instruction set by writing a quick and dirty disassembler, still a very good way to really learn an instruction set. with the armv6 not having thumb2 extensions you can go either way, all thumb and disassemble linearly 16 bits at a time or arm disassembling 32 bits at a time. write the assembly assemble it to object, maybe objcopy i fyou dont want to read the elf directly, disassemble the loadable sections, repeat. you get to understand the reason why certain instructions are limited in reach or what th erules are for constants/immediates, etc, plus you get to learn how to create the syntax for the assembler in question to generate the exact instruction you wanted which is the entire point of assembly language.

you already have assembly language experience, just get the arm architectural reference manual for the armv5, look at the programmers model area for a bit as t how they boot, the exception table, then the arm and thumb instruction sets are there. being RISC they are easier to understand than the CISC you are used to. Likewise arm does a good job with pseudo code, the newer manauls are not as good as that old manual, the armv5 ARM ARM is derived directly from the print books that go back to the ARMv4t in the ARMTDMI. basically when the A changed from acorn to advanced. The blue book is still my favorite compared to the thin white book, the thick white book is pretty close to the one on arms site. they couldnt keep all the new cores in a single big book obviously so they did a do over for each new thing after that. armv6 is the red headed step child that is a little in the armv5(/armv4) book and a little in the armv7 book.

another good exercise is to write an instruction set simulator, the thumbulator next to my pi stuff and mcu stuff on github I think I banged that out one saturday, debugged on sunday, was mostly functional by then. had a bug or two, but so far good enough. you are of course welcome to use it to learn thumb (pre-thumb2 extensions). that thumb being the one instruction set supported across all the arm products, thus a good reason to learn it.

Return to “Bare metal, Assembly language”

Who is online

Users browsing this forum: No registered users and 3 guests