addybist
addybist

Reputation: 699

How to use pseudo-op for an instruction?

In an assembly language, how can I use pseudo-ops for an instruction? From what I know, an assembly file is converted to machine code using an assembler. Is there a way to directly send the op-code from the assembly file itself using pseudo-ops?

For example if there's some instruction, say ADD which has an op-code of 0101. Is there a way to use our that knowledge of ADD's op code being 0101 to just ADD two registers using 0101 from some memory location and not using the ADD instruction explicitly? I was looking for a hack and was wondering this.

Upvotes: 0

Views: 948

Answers (3)

Michael Karcher
Michael Karcher

Reputation: 4111

Using an indirect opcode (retrieve the instruction byte from a data location specified in the instruction) is not supported in any machine language I know. But you have the following possibilities:

  • Self-modifying code (You get write access to your own code segment and patch the code bytes on the fly)
  • Create a (short) fragment of code in a data region, mark that region executable and jump into it (likely you will create a subroutine there, ending in 0xC3 if it is x86 assembly), and jump to it using the CALL instruction (or whatever it is called on your machine).

Upvotes: 1

Andras
Andras

Reputation: 3055

Yes, this is possible, and is in fact how buffer overflow or stack overrun exploits work to attack system security. If binary values are stored into executable memory, they will be loaded and executed as machine language instructions, and 0101 will ADD (in your example).

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137467

You haven't specified what assembler you're using, but GAS for example has the .byte pseudo-op to simply emit a byte. Note that 0101 is in binary notation which is probably not suitable for the assembler. You should specify opcodes in hexadecimal.

Upvotes: 0

Related Questions