Skip to content

Instantly share code, notes, and snippets.

@mfazekas
Created February 15, 2011 13:01
Show Gist options
  • Save mfazekas/827483 to your computer and use it in GitHub Desktop.
Save mfazekas/827483 to your computer and use it in GitHub Desktop.
gdb assemble macro
define assemble
# dont enter routine again if user hits enter
dont-repeat
if ($argc)
if (*$arg0 = *$arg0)
# check if we have a valid address by dereferencing it,
# if we havnt, this will cause the routine to exit.
end
printf "Instructions will be written to %#x.\n", $arg0
else
printf "Instructions will be written to stdout.\n"
end
printf "Type instructions, one per line.\n"
printf "End with a line saying just \"end\".\n"
if ($argc)
# argument specified, assemble instructions into memory
# at address specified.
shell perl -e 'print "BITS 32\n"; while(1) { print STDERR ">"; $| =1 ; $_ = <STDIN> ; if ($_ =~ /^end\s*/){ exit;} print "$_"; }' > ~/.nasm_in.txt \
&& nasm -f bin -o /dev/stdout ~/.nasm_in.txt \
| hexdump -ve \
'1/1 "set *((unsigned char *) $arg0 + %#2_ax) = %#02x\n"' \
> ~/.gdbassemble
# load the file containing set instructions
source ~/.gdbassemble
# all done.
shell rm -f ~/.gdbassemble
else
# no argument, assemble instructions to stdout
shell perl -e 'print "BITS 32\n"; while(1) { print STDERR ">"; $| =1 ; $_ = <STDIN> ; if ($_ =~ /^end\s*/){ exit;} print "$_"; }' > ~/.nasm_in.txt \
&& nasm -f bin -o /dev/stdout ~/.nasm_in.txt \
| ndisasm -i -b32 /dev/stdin
end
end
document assemble
Assemble instructions using nasm.
Type a line containing "end" to indicate the end.
If an address is specified, insert instructions at that address.
If no address is specified, assembled instructions are printed to stdout.
Use the pseudo instruction "org ADDR" to set the base address.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment