Search code examples
assemblyx8632-bitmachine-codecode-size

What is the method for counting how many bytes your program will be, reading from 32 bit x86 assembly code


I am working on a simple stack smashing exercise. I need to write into my assembly a short jump of a specific offset. I need to calculate the offset by how many bytes the two instructions are separated by.

I have been searching google but I can't seem to get the right search terms.

I know, for example, that the mov opcode is 2 bytes. But how many bytes the whole instruction will be depends on your src and dest ... whether your are using immediate values or offsets, what the addressing mode is, etc.

I know there has to be a chart or something, somewhere, I can use to calculate this.

page 12 of Aleph One's Smashing the Stack for Fun and Profit shows what I'm trying to do (count bytes from assembly code): http://www-inst.eecs.berkeley.edu/~cs161/fa08/papers/stack_smashing.pdf


Solution

  • I need to calculate the offset by how many bytes the two instructions are separated by.

    The easy way is to slap your instructions into an online assembler (e.g. https://defuse.ca/online-x86-assembler.htm ) and let it show you the bytes.

    I know there has to be a chart or something, somewhere, I can use to calculate this.

    Intel's Software Developer Manuals, Volume 2 (from https://software.intel.com/en-us/articles/intel-sdm ) contains all the tables, rules, values and other information needed for the conversion of assembly into machine code (the bytes).

    Be prepared to be daunted; it's not a simple process. Mostly, you use the instruction's name (mnemonic), current operating mode of the CPU and the instruction's operands to determine which addressing mode and then which opcode it is; then use multiple tables to determine how to encode the operands into the machine code.