Skip to content

Commit d66e486

Browse files
committed
py: Add store r8 and store r16 ops to asm_x86 and asm_x64.
1 parent 851f15f commit d66e486

4 files changed

Lines changed: 43 additions & 8 deletions

File tree

py/asmx64.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
5050
#define OPCODE_MOV_I64_TO_R64 (0xb8) /* +rq */
5151
#define OPCODE_MOV_I32_TO_RM32 (0xc7)
52+
#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
5253
#define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */
5354
#define OPCODE_MOV_RM64_TO_R64 (0x8b)
5455
#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
@@ -85,6 +86,8 @@
8586
#define MODRM_RM_REG (0xc0)
8687
#define MODRM_RM_R64(x) ((x) & 0x7)
8788

89+
#define OP_SIZE_PREFIX (0x66)
90+
8891
#define REX_PREFIX (0x40)
8992
#define REX_W (0x08) // width
9093
#define REX_R (0x04) // register
@@ -298,18 +301,28 @@ STATIC void asm_x64_ret(asm_x64_t *as) {
298301
asm_x64_write_byte_1(as, OPCODE_RET);
299302
}
300303

301-
void asm_x64_mov_r32_to_r32(asm_x64_t *as, int src_r32, int dest_r32) {
302-
// defaults to 32 bit operation
303-
assert(src_r32 < 8);
304-
assert(dest_r32 < 8);
305-
asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
306-
}
307-
308304
void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
309305
// use REX prefix for 64 bit operation
310306
asm_x64_write_byte_3(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R) | (dest_r64 < 8 ? 0 : REX_B), OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
311307
}
312308

309+
void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
310+
assert(dest_r64 < 8);
311+
if (src_r64 < 8) {
312+
asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
313+
} else {
314+
asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_R8_TO_RM8);
315+
}
316+
asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
317+
}
318+
319+
void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
320+
assert(src_r64 < 8);
321+
assert(dest_r64 < 8);
322+
asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
323+
asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
324+
}
325+
313326
void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
314327
// use REX prefix for 64 bit operation
315328
assert(dest_r64 < 8);
@@ -356,6 +369,7 @@ void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
356369
}
357370

358371
void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
372+
// TODO use movzx, movsx if possible
359373
if (UNSIGNED_FIT32(src_i64)) {
360374
// 5 bytes
361375
asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);

py/asmx64.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#define ASM_X64_CC_JNZ (0x5)
5959
#define ASM_X64_CC_JNE (0x5)
6060
#define ASM_X64_CC_JL (0xc) // less, signed
61+
#define ASM_X64_CC_JG (0xf) // greater, signed
6162

6263
typedef struct _asm_x64_t asm_x64_t;
6364

@@ -75,6 +76,9 @@ void asm_x64_mov_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
7576
void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
7677
void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64);
7778
void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
79+
void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
80+
void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
81+
void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
7882
void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64);
7983
void asm_x64_add_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
8084
void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b);

py/asmx86.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
//#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
5050
#define OPCODE_MOV_I32_TO_R32 (0xb8)
5151
//#define OPCODE_MOV_I32_TO_RM32 (0xc7)
52+
#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
5253
#define OPCODE_MOV_R32_TO_RM32 (0x89)
5354
#define OPCODE_MOV_RM32_TO_R32 (0x8b)
5455
#define OPCODE_LEA_MEM_TO_R32 (0x8d) /* /r */
@@ -85,6 +86,8 @@
8586
#define MODRM_RM_REG (0xc0)
8687
#define MODRM_RM_R32(x) (x)
8788

89+
#define OP_SIZE_PREFIX (0x66)
90+
8891
#define IMM32_L0(x) ((x) & 0xff)
8992
#define IMM32_L1(x) (((x) >> 8) & 0xff)
9093
#define IMM32_L2(x) (((x) >> 16) & 0xff)
@@ -232,7 +235,17 @@ void asm_x86_mov_r32_to_r32(asm_x86_t *as, int src_r32, int dest_r32) {
232235
asm_x86_write_byte_2(as, OPCODE_MOV_R32_TO_RM32, MODRM_R32(src_r32) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
233236
}
234237

235-
STATIC void asm_x86_mov_r32_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
238+
void asm_x86_mov_r8_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
239+
asm_x86_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
240+
asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
241+
}
242+
243+
void asm_x86_mov_r16_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
244+
asm_x86_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R32_TO_RM32);
245+
asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
246+
}
247+
248+
void asm_x86_mov_r32_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
236249
asm_x86_write_byte_1(as, OPCODE_MOV_R32_TO_RM32);
237250
asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
238251
}

py/asmx86.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define ASM_X86_CC_JNZ (0x5)
6060
#define ASM_X86_CC_JNE (0x5)
6161
#define ASM_X86_CC_JL (0xc) // less, signed
62+
#define ASM_X86_CC_JG (0xf) // greater, signed
6263

6364
typedef struct _asm_x86_t asm_x86_t;
6465

@@ -72,6 +73,9 @@ void* asm_x86_get_code(asm_x86_t* as);
7273
void asm_x86_mov_r32_to_r32(asm_x86_t* as, int src_r32, int dest_r32);
7374
void asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32);
7475
void asm_x86_mov_i32_to_r32_aligned(asm_x86_t *as, int32_t src_i32, int dest_r32);
76+
void asm_x86_mov_r8_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
77+
void asm_x86_mov_r16_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
78+
void asm_x86_mov_r32_to_disp(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
7579
void asm_x86_xor_r32_to_r32(asm_x86_t *as, int src_r32, int dest_r32);
7680
void asm_x86_add_r32_to_r32(asm_x86_t* as, int src_r32, int dest_r32);
7781
void asm_x86_cmp_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);

0 commit comments

Comments
 (0)