
Introduction to ARM Assembly Basics - ingve
https://azeria-labs.com/writing-arm-assembly-part-1/
======
wyc
In case you're interested in seeing a project that does something, I wrote an
IRC bot in ARM: [https://github.com/wyc/armbot](https://github.com/wyc/armbot)

------
partycoder
You can also compile assembly with gcc rather than as + ld.

And you can output assembly from C programs

    
    
        gcc -S <source file>
    

You can disassemble a binary to see how it actually looks. The resulting
binary is much larger than your assembly code.

    
    
        objdump -d <binary file>
    

Many disassemblers will show you friendlier output than objdump. I use ht
editor (packaged in Debian based distros as ht), an open source clone of Hiew.
In ht, press F6 -> select image, and you will have an easy to follow
disassembled version of a binary that you can edit, if you happen to know
opcodes.

------
CraigJPerry
Some of the arm instruction set manuals seem harder to come by than others.
Recently I reverse engineered one of the firmwares of the ImmersionRC Vortex
150 racing quadcopter (it uses an stm32f3 chip, arm cortex instruction set).
It was pretty hard to come by a copy of the full instruction set manual - i
just assumed that kind of thing would be a quick google away. Eventually i got
there and made my changes but it was harder to get going than i expected but
for different reasons than i was anticipating.

~~~
bobsam
What are you talking about???

[https://static.docs.arm.com/ddi0403/e/DDI0403E_B_armv7m_arm....](https://static.docs.arm.com/ddi0403/e/DDI0403E_B_armv7m_arm.pdf)

[http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/BA...](http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/BABECGJD.html)

There is also the old cheat sheet:

[http://users.ece.utexas.edu/~valvano/Volume1/QuickReferenceC...](http://users.ece.utexas.edu/~valvano/Volume1/QuickReferenceCard.pdf)

~~~
CraigJPerry
None of these are the correct document?

I think you've inadvertently just helped make my case for me.

I don't have a link to share unfortunately, the only way i could get access
was by going to ST directly.

~~~
bobsam
First link, page 99 says: " armv7m instruction set"

The chapter after that even includes instruction encoding.

Maybe you were looking for something else?

------
big_spammer
What's with the branch instructions "Branch with Link" and "Branch with
Exchange"?

What do they do? I haven't seen anything like this before.

I see some explanation in [https://azeria-labs.com/arm-conditional-execution-
and-branch...](https://azeria-labs.com/arm-conditional-execution-and-
branching-part-6/) but the reason why branching must work this way isn't
explained.

edit: I see "Branch with Exchange" switches the processor from ARM to Thumb in
[http://www.embedded.com/electronics-blogs/beginner-s-
corner/...](http://www.embedded.com/electronics-blogs/beginner-s-
corner/4024632/Introduction-to-ARM-thumb). But I don't see why switching
processor modes must happen on branch instructions.

~~~
repiret
It must be possible to change instruction sets when branching, in order to
call or return from a function in the opposite instruction set.

There are branching instructions that can't change instruction sets for two
reasons: 1\. A direct branch can have more range if you can assume it's going
to a 4-byte aligned address. 2\. For compatibility with code written for
ARM7DI and other really old pre-thumb processors. Since the original direct
branch instructions ignored the bottom two bits of the target address, new
direct branch instructions were added rather than change the behavior of the
existing ones.

------
mishurov
Why don't people use AT&T syntax for ARM?

~~~
theresistor
AT&T syntax is a uniquely X86 thing.

~~~
Annatar
No it isn't, as AT&T syntax is used on SPARC and Motorola 68000, for example.
It has nothing whatsoever to do with any processor in particular.

~~~
danellis
What do you mean by "AT&T syntax"? Neither 68000 nor SPARC use it, because
it's an x86 thing. Do you really mean "operand order"? There's more to an
assembly language syntax than just the order of the operands.

~~~
Annatar
AT&T syntax is mainly about operand order, but there are assembler directives
and constructs specific to AT&T as found in as(1) on any traditional UNIX,
including illumos based ones. That every assembler has his own syntax is
nothing new, compare and contrast Master SEKA with ASM-One on Amiga, or MASM,
TASM or nasm, for example. AT&T as(1) syntax has nothing to do with x86. Any
code written for SVR4 as(1) will be using AT&T syntax, irrespective of
processor architecture. ISA used with AT&T as(1) will still be that of the
processor, of course, but instead of things like a0 or d0, it'll be %a0 and
%d0, for example.

------
lacampbell
Does any one here use a lot of assembly in their job? If so, what do you do?

I started getting fascinated about computer architecture a while back, but
then I saw how dead embedded programming was in my area.

~~~
Stenzel
Since you ask, I use lots of assembly level programming for digital audio
signal processing. Some ARM instructions offer special DSP specific features
like saturation and fractional arithmetic that have no equivalent int C, so it
justifies using assembly. Smaller ARM cores without NEON offer some kind of
miniature SIMD by operating on two 16-bit or four 8bit numbers, these can
speed up things as well. To take full advantage of these it is best to write
some portion of code in assembly. I usually wrap a processing sequence into a
gcc-style inline assembly macro, this way I can easily compare with a C-only
macro and maintain portability, and it saves me from having to deal with the
complete instruction set and calling conventions.

~~~
oriolid
I've also been working with DSP processors in audio processing. The only time
I've used actual inline assembly was storing stack pointer to measure stack
usage.

For saturating arithmetic and other stuff we used compiler intrinsics, which
freed us from handling register allocation, stack management etc by hand. On
that processor there weren't special instructions for saturating arithmetic
but a flag was used instead, the compiler also kept track of that one too.

We did read the assembly result and tweaked C code until assembly looked like
what was expected, though.

~~~
dmytrish
Technically, assembly is not required for measuring stack: just use regular C
address of any local variable/function argument (minus an address of some
local variable in main) to have some approximation of stack usage.

~~~
oriolid
True. But I was young, stupid and had just completed a course on assembler on
that processor. Money well spent.

------
sjtgraham
I'm only a couple of pages in but already a lot of this guide is incorrect for
ARMV8-A (A64). Much is different, e.g. no thumb mode, no directly accessible
program counter, no load multiple, no PUSH/POP, different stack pointer, etc.
Looks good if you're interested in older ARM ISAs, which is probably more
applicable for IoT etc.

~~~
morisGue
Lol, that's because it's about ARMv6. She even points it out in Part 1 or so
(not sure where but I saw it somewhere). If you wanna include all differences
of ARM that would be a long tutorial.

~~~
sjtgraham
I think you'll find the only reference to v6 is where the register names are
explained, e.g. <= v7: r0, r1, etc. This is another thing it gets wrong, i.e.
A64 registers can be x0, X1, etc, but also the lower 32 bits can be addressed
via w0, w1, etc.
[http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc....](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0024a/BABHDEEJ.html)

~~~
mordnis
You do realise you are comparing 32 bit ARM and 64 bit ARM, which are very
different ISAs?

~~~
sjtgraham
Yes, that is exactly my point. This is something I think the guide should
mention.

------
unixhero
Nice

I enjoyed the read!

