top | item 12015436

Linux Assembly How-to

99 points| rspivak | 9 years ago |tldp.org | reply

18 comments

order
[+] heinrichhartman|9 years ago|reply
From http://www.tldp.org/HOWTO/Assembly-HOWTO/external.html:

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

[+] groovy2shoes|9 years ago|reply
I'm a fan of TeX2page[1] for converting (La)TeX to HTML. There are also similar TeX/Scribe-inspired markup processors. Scribble[2] comes to mind, and Chibi Scheme[3] even comes with a very lightweight, very configurable "Scribble" processor in the form of `chibi-doc`. Chibi's flavor is a little more similar to TeX (using '\' as the leader rather than '@', for example), if that's important to you.

[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 :)

[+] AdmiralAsshat|9 years ago|reply
Trying to read through "Programming From the Ground Up" (a GNU book on Assembly), and having some trouble keeping track of what I've thrown into what register.

Does anyone have a recommendation for an IDE or debugger that can let me inspect my registers at a given step of the program?

[+] userbinator|9 years ago|reply
having some trouble keeping track of what I've thrown into what register.

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.

[+] groovy2shoes|9 years ago|reply
Honestly, when I'm writing assembly code, my "IDE" is a notebook and pencil. Narrowly ruled paper, so that I can squeeze a lot of lines in, with a line drawn down the middle so that I get two columns: the left column has my code, and the right column holds commentary.

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.
[+] ndesaulniers|9 years ago|reply
While using lldb (LLVM's equivalent of gdb, both of which are debuggers I use to step through my code), I frequently run:

  (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`.
[+] briansteffens|9 years ago|reply
gdb is magic once you get the hang of it, I'd definitely recommend spending some time getting comfortable with it.