diff options
| -rw-r--r-- | instructions.h | 16 | ||||
| -rw-r--r-- | main.c | 50 |
2 files changed, 59 insertions, 7 deletions
diff --git a/instructions.h b/instructions.h index c94c04b..36a69f8 100644 --- a/instructions.h +++ b/instructions.h @@ -190,8 +190,20 @@ #define JNZ #define JMP #define LEA -#define POP -#define PUSH + +// Push immediate byte/dword +#define PUSHBI(imm) (0x6a), imm +#define PUSHDI(imm) \ + (0x68), (imm & 0xff), ((imm >> 8) & 0xff), ((imm >> 16) & 0xff), \ + ((imm >> 24) & 0xff) +// Push register word/qword +#define PUSHW(reg) PREFIX_OVERRIDE_OPSIZE, (0x50 + reg) +#define PUSHQ(reg) (0x50 + reg) + +// Pop register word/qword +#define POPW(reg) PREFIX_OVERRIDE_OPSIZE, (0x58 + reg) +#define POPQ(reg) (0x58 + reg) + #define RET 0xc3 #define RETN #define SUB @@ -7,7 +7,8 @@ static const unsigned char exit_2[] = { // Exit syscall MOVWI(REG_EAX, SYS_exit), // Exit code - MOVWI(REG_EDI, 0x02), SYSCALL + MOVWI(REG_EDI, 0x02), + SYSCALL, }; static const unsigned char some_opcodes[] = { @@ -26,14 +27,53 @@ static const unsigned char some_opcodes[] = { // rdi 00000006 MOVD(REG_ECX, REG_EDI), // Exit syscall - MOVWI(REG_EAX, SYS_exit), SYSCALL + MOVWI(REG_EAX, SYS_exit), + SYSCALL, +}; + +// (x86-64) syscall argument order, rax=syscall: +// rdi rsi rdx r10 r8 r9 +static const unsigned char hello_world[] = { + // Write + + MOVWI(REG_EDI, 0x1), // stdout + MOVDI(REG_ESI, 0x40101d), // buffer + MOVWI(REG_EDX, 0xc), // count/buffer len + MOVWI(REG_EAX, SYS_write), + SYSCALL, + + // Exit 0 + MOVWI(REG_EDI, 0x00), + MOVWI(REG_EAX, SYS_exit), + SYSCALL, + + // 'hello world\n' + 0x68, + 0x65, + 0x6c, + 0x6c, + 0x6f, + 0x20, + 0x77, + 0x6f, + 0x72, + 0x6c, + 0x64, + 0x0a, }; -static const unsigned int some_opcodes_len = - sizeof(some_opcodes) / sizeof(some_opcodes[0]); +static const unsigned char inc_dec_test[] = { + INCB(REG_BL), INCW(REG_BX), INCD(REG_EBX), INCQ(REG_RBX), + DECB(REG_BL), DECW(REG_BX), DECD(REG_EBX), DECQ(REG_RBX), +}; + +static const unsigned char pushpop[] = { + PUSHBI(0x42), PUSHDI(0x42431337), PUSHW(REG_BX), + PUSHQ(REG_RBX), POPW(REG_BX), POPQ(REG_RBX), +}; int main(int argc, char* argv[]) { - write_elf("test.out", some_opcodes_len, (char*)some_opcodes); + write_elf("test.out", sizeof(pushpop) / sizeof(pushpop[0]), (char*)pushpop); return 0; } |
