3 ms·
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.
by AdmiralAsshat 10y ago
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?
- briansteffens 10y agogdb is magic once you get the hang of it, I'd definitely recommend spending some time getting comfortable with it.
- AdmiralAsshat 10y agoI know my way around GDB, but only as a C debugger. Haven't really used it for Assembly.
- sigjuice 10y ago(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 $r11 All of this is applicable when debugging a C program as well.
- AdmiralAsshat 10y ago`layout reg` Did exactly what I wanted. Thank-you so much!
- barrkel 10y agoI've found it very buggy when working at the assembly level on a complex program (e.g. JVM). I think it coredumped on average every 5 minutes as I tried to step through things or set breakpoints.
- BellsOnSunday 10y agoYes, and there is an appendix on gdb in the Bartlett book which will get OP started.
- userbinator 10y agohaving 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 10y agoHonestly, 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 10y agoWhile 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`.