Linux Assembly How-to
tldp.org
tldp.org
AT&T syntax is this weird awful syntax only used by GNU as. The syntax is some leftover from an assembler from the early UNIX days which was clearly written by a very lazy person. Whoever came up with it apparently thought to themselves, "If we make source files put a % character in front of registers and a $ character in front of constants, then I don't have to think too hard about how to write a parser. If we make source files specify their width in a suffix, then we don't have to think too hard about how to get this information out of the register name. And let's put our operands in (source, destination) order instead of (destination, source) order just to be evil and confuse people!"
Aaagggghh. If gas wasn't used as the backend of gcc, I guarantee exactly zero people would use it, because AT&T syntax is so awful.
You should consider posting an update using NASM / YASM (or if you feel you must use GAS, use its directive for Intel syntax). Your readers will thank you.
About m4 macro-processor: "However, its disfunctional quoting and unquoting semantics force you to use explicit continuation-passing tail-recursive macro style if you want to do advanced macro programming (which is remindful of TeX -- BTW, has anyone tried to use TeX as a macroprocessor for anything else than typesetting ?)."
This is a great point! The TeX Macro language is so much better than M4 quoting syntax. I'd love to have it available for e.g. writing blog-posts in HTML or coding.
It seems that the tex macroprocessor can not be used independently of latex, which is a pitty. There are m4 clones with TeX-like syntax (e.g. http://www.nongnu.org/t4/) - but none of those seems to be used much.
To see what's possible with just assembly and macros see Tannenbaums paper: https://www.computer.org/csdl/trans/ts/1976/02/01702350.pdf
[1]: https://ds26gte.github.io/tex2page/index.html
[2]: https://docs.racket-lang.org/scribble/index.html
[3]: http://synthcode.com/wiki/chibi-scheme
And here's Chibi's own manual written in it: https://github.com/ashinn/chibi-scheme/blob/master/doc/chibi...
I think it would be freakishly easy to adapt chibi-doc into a general text preprocessor rather than an HTML generator, and honestly I've thought about it before. I may just do it yet :)
Also I can see a LISP style macro processor being super awesome. Let me know if you are going to adapt chibi-doc that way.
Does anyone have a recommendation for an IDE or debugger that can let me inspect my registers at a given step of the program?
As someone who has been writing in Asm for a few decades and taught it briefly at the undergrad level, here are a few advices that might help:
Don't move values between registers unless absolutely necessary.
Try to visualise the formation of an expression in a register as instructions operate on it. (E.g. eax holds x+y and ebx holds z; after 'add eax, ebx', eax holds x+y+z.)
If you're doing x86, there's only 8 and one of them (stack pointer) you almost never have to change. That leaves 7, but if you use them according to how they were originally envisioned, then it gets a lot simpler keeping track of the values since their names are mnemonic: EAX is the Accumulator and should be used most of the time. ECX is Count and should be your first choice for a loop counter. EDX is Data and can be treated like a second accumulator. EBX is Base and normally thought of as holding a pointer, same for ESI, EDI, and EBP. The last one is usually used for local variables/parameters on the stack, but for 32/64-bit you can use ESP for stack accesses and put any other "long-lived" address (like a "this" pointer of an object) in EBP.
There are basically no cushy IDEs or other hand-holding things --- Asm is very different from the typical HLL, and mentally executing your code in the editor before assembling and running/debugging is the norm. There is far less "instant gratification", or any "shortcuts". It's just something that you have to practice to become good at.
I first started writing code in notebooks due to necessity (I grew up in a poor family, and though I had access to many outdated programming books at my local library, I rarely had access to a computer), but I keep doing it nowadays because it's utterly distraction-free, and being able to scribble notes and diagrams in the code is often convenient.
For assembly language especially, I find the two-column format indispensable. Even when I got to university and took an assembly language class, I would write all my assignments in vim with soft tabstops set to the middle of the screen[1], so that after each instruction, I could hit tab and my cursor would instantly be at the second column for commentary. I know emacs can do something similar, because I managed to get it stuck on in lisp-mode before, and couldn't figure out how to turn it off! :)
[1]:
au FileType asm setlocal sts=38 et " GNU as
au FileType nasm setlocal sts=38 et " nasm
Vim has syntax files for a whole bunch of assembly instruction sets and syntaxes, which I've found handy. (lldb) register read
General Purpose Registers:
rax = 0x0000000000000001
rbx = 0x0000000000000000
rcx = 0x00007fff5fbff8c8
rdx = 0x00007fff5fbff780
rdi = 0x0000000100000f2a "sizeof(char): %zu\n"
rsi = 0x0000000000000001
rbp = 0x00007fff5fbff750
rsp = 0x00007fff5fbff730
r8 = 0x0000000000000000
r9 = 0x00007fff790620c8 atexit_mutex + 24
r10 = 0x00000000ffffffff
r11 = 0xffffffff00000000
r12 = 0x0000000000000000
r13 = 0x0000000000000000
r14 = 0x0000000000000000
r15 = 0x0000000000000000
rip = 0x0000000100000e66 a.out`main + 22 at sizeof.c:4
rflags = 0x0000000000000206
cs = 0x000000000000002b
fs = 0x0000000000000000
gs = 0x0000000000000000
gdb's equivalent should be `info registers`. (gdb) help si
Step one instruction exactly.
(gdb) help ni
Step one instruction, but proceed through subroutine calls.
(gdb) help info registers
List of integer registers and their contents, for selected stack frame.
Register name as argument means describe only that register.
(gdb) layout reg # to enable view of registers in split screen
Also, you can use register names in expressions. e.g. p $r11All of this is applicable when debugging a C program as well.