DOS debug like program for 32-bit x86 assembly [closed]
Asked Answered
C

4

7

Many of you may recall the old DOS program--debug. Though outdated in many respects, one of the nice things about it was that one could easily find the byte-sequence for a given instruction without having to go through the steps of writing a program, compiling, disassembling, examining the file contents, .... Enter the instruction, then dump the instruction address. 'debug' regrettably does not do 32 bit instructions.

Does anyone know of a tool that does something similar for 32-bit x86 instructions? I don't want to go through the whole compile process; I just need to be able to enter a couple of instructions and have it spew out the length of the instruction and its byte sequence.

Chloe answered 8/7, 2010 at 17:56 Comment(2)
Interesting question!. Why not tagging "debugging"?Inconsolable
Hadn't thought to add the 'debugging' tag until you mentioned it. Thank you.Chloe
H
6

DOS debug was an interactive assembler as well as a debugger, entering assembly code resulted in that line being converted immediately to machine code - which is what you dumped out.

So all you need is to automate your favourite assembler with a script or batch-file.

Here's a bash function I came up with in a minute or two using the popular nasm assembler:

opcode() {
  echo $* > tmp.S && nasm tmp.S -o tmp.o && od -x tmp.o
  rm -f tmp.o tmp.S
}

Takes less than a second. Invocation looks like this:

$ opcode mov eax, [ebx]
0000000 6667 038b
0000004
$ opcode fadd st0,st1
0000000 c1d8
0000002

Not brilliant, but you can tweak od command-line for better output. This idea should work with any command-line assembler as long as you tell it to use a simple binary output format.

Heartsome answered 10/7, 2010 at 11:24 Comment(0)
E
2

FreeDOS Debug/X is a line-oriented debugger originally developed as a clone for MS-DOS Debug. However, it has gained support for 32-bit instructions. Both its assembler and disassembler fully support at least 486-level instructions. Access to and display of the 386+ registers is also included. This debugger, for one, supports 32-bit code (using a32 or o32 prefixes) in Real/Virtual 86 Mode. For another, the DebugX variant allows running the debugger as a DPMI client to load and debug another DPMI client. The client to debug can run in 16-bit or 32-bit Protected Mode. This is true of all of the following members of the FreeDOS Debug family.

The original Debug/X is still being developed from time to time. It can be found on github: https://github.com/Baron-von-Riedesel/DOS-debug/releases

There is also the fork Enhanced Debug, which is nonfree. It's available at https://pcdosretro.github.io/enhdebug.htm

Finally, I forked Debug/X starting in 2008. Based on this codebase I created lDebug, which is still line-oriented but is more advanced than either of the others. Manual and releases are linked on my website: https://pushbx.org/ecm/web/#projects-ldebug

Episiotomy answered 17/2 at 22:17 Comment(2)
The accepted answer is a bash script using Unix shell commands. Probably this answer is useful for some people, but I'm not sure the [dos] tag belongs on the question. I think the OP was looking for tools on a modern OS (which they forgot to specify!) which can do some of the things they did with debug on DOS. You're answering the question of a 32-bit-capable / 386-aware version of debug for actual DOS, which is probably fine: people looking for that could easily end up here from Google based on the question title. I guess the DOS tag isn't hurting anything; I marked my answer as [linux]Wessling
@PeterCordes "people looking for that could easily end up here from Google based on the question title" -- Yes, I did actually find this question from a web search so it's certainly possible to come here from a search for "debug dos".Episiotomy
W
2

On , I use a shell script I call asm-link, which has a -d option to assemble + link and then disassemble with objdump -drwC -Mintel. It's part of my answer on another question.

The script could be ported to Windows using nasm -f win64 or OS X using -f macho64, perhaps even to DOS if you wanted that.

NASM defaults to the .text section at the top of the file, and GNU Binutils ld defaults to putting the ELF entry point at the start of the .text section if it can't find a _start symbol, so even linking works with absolutely minimal .asm source files, and you can even run them in GDB and single-step. cat > foo.asm ... type some and hit CtrlD is one way to create a file, or of course your favourite light-weight editor.

Example use:

$ cat > foo.asm
mov eax, ecx
lea rdi, [rel $]
$ asm-link -dn foo.asm
+ nasm -felf64 -Worphan-labels foo.asm
+ ld -o foo foo.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000

foo:     file format elf64-x86-64


Disassembly of section .text:

0000000000401000 <__bss_start-0x1000>:
  401000:       89 c8                   mov    eax,ecx
  401002:       48 8d 3d f9 ff ff ff    lea    rdi,[rip+0xfffffffffffffff9]        # 401002 <__bss_start-0xffe>

What I actually type on the second command line is asm<tab> -dn <alt-.>.
Alt. recalls the last token from previous commands so I don't have to retype the filename. And tab-completion for the command works since my system doesn't have any other commands that start with asm.

nasm -felf64 (passed by the script, or -f elf32 if you use asm-link -m32) implies BITS 64 (or BITS 32) for assembling.

GNU Binutils objdump's output format is fairly nice for showing the machine code, with addresses that make it easy to see length, and spaces that make it easy to see byte boundaries. (Unlike nasm -l /dev/stdout -f elf64 foo.asm listings which cram hex digits together.)

The + nasm ... and + ld output lines are from bash set -x. The -n option to the script uses NASM instead of YASM; back when I originally wrote it, I usually used YASM. I should probably update the default to be NASM.

Wessling answered 18/2 at 1:39 Comment(2)
I didn't get asm<tab> -dn <alt+.>. alt-. recalls ... Does pressing the TAB key make the text "-link" appear? I suppose <alt+.> in essence is the same as alt-. but I think it could be a tad confusing, the + vs the -.Giglio
@SepRoland: yes, via tab-completion, since my system doesn't have any other command that start with asm in my $PATH. Good point about <alt+.> vs. alt-. in other places.Wessling
B
1

There are a few simple, 32-bit command line debuggers to be found. Based on your description, OllyDbg might fit your needs well. At least some versions of Microsoft's Debugging Tools for Windows include one named CDB, which stands for Commandline DeBugger (though I haven't verified that the linked version includes it...)

Barye answered 8/7, 2010 at 18:19 Comment(1)
Interesting. Not yet sure if it suits my purposes, but I am still playing around with it. Thank you for the suggestion.Chloe

© 2022 - 2024 — McMap. All rights reserved.