From 791e52dafd396bb3aa0c223200131821965329fc Mon Sep 17 00:00:00 2001 From: julianb0 Date: Sun, 16 Jun 2019 19:42:10 +0200 Subject: [PATCH] att style for the lol --- ka/main.k | 9 ++- vm/in/string.c | 12 +-- vm/pc/dump.c | 202 +++++++++++++++++++++++++++++++++++-------------- vm/pc/main.c | 5 +- 4 files changed, 160 insertions(+), 68 deletions(-) diff --git a/ka/main.k b/ka/main.k index e210ee2..139a59e 100644 --- a/ka/main.k +++ b/ka/main.k @@ -5,7 +5,10 @@ ; Main function ; main: - jmp bswap_test + call bswap_test + call str_test + call itoa_test + call movzx_test bswap_test: mov rdx, 0x1122334455667788 @@ -20,10 +23,10 @@ bswap_test: ret ramdev_test: - call DevGetMemOff + call MEM.GetMemOff mov rdx, rax - call DevGetMemSize + call MEM.GetMemSize ret stosb_test: diff --git a/vm/in/string.c b/vm/in/string.c index 233e519..51b682f 100644 --- a/vm/in/string.c +++ b/vm/in/string.c @@ -13,7 +13,7 @@ //----------------------------------------------------------------------------// -void stos_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) +static void stos_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) { ulong reg, val; @@ -65,7 +65,7 @@ IMPL_END; //----------------------------------------------------------------------------// -void lods_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) +static void lods_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) { ulong reg1, reg2; @@ -117,7 +117,7 @@ IMPL_END; //----------------------------------------------------------------------------// -void scas_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) +static void scas_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) { ulong reg, val; @@ -170,7 +170,7 @@ IMPL_END; //----------------------------------------------------------------------------// -void cmps_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) +static void cmps_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) { ulong reg1, reg2; @@ -224,7 +224,7 @@ IMPL_END; //----------------------------------------------------------------------------// -void cmpzs_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) +static void cmpzs_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) { ulong reg1, reg2; @@ -281,7 +281,7 @@ IMPL_END; //----------------------------------------------------------------------------// -void movs_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) +static void movs_impl(ctx_t *ctx, acc_t *p1, acc_t *p2, uint len) { ulong reg1, reg2; diff --git a/vm/pc/dump.c b/vm/pc/dump.c index 98a354b..f3e3822 100644 --- a/vm/pc/dump.c +++ b/vm/pc/dump.c @@ -13,62 +13,10 @@ char *cond_suffixes[] = "?" }; -static void dump_acc(ctx_t *ctx, acc_t *p) -{ - uint mfmt; +// Comment the following line to get back intel syntax +#define _ATT_STYLE 1 - if (p->type == A_REG) - log("%s", ctx->r[p->reg].name); - - else if (p->type == A_IMM64) - { - if (p->val < 0xA) - log("%lu", p->val); - - else - log("0x%lX", p->val); - } - - else - { - log("%c[", getmempref(p->mlen)); - - mfmt = p->type & AM_MFMT_MASK; - - if (mfmt == AM_IMM64) - log("0x%lX]", p->addr); - - else if (mfmt == AM_RR) - { - if (p->reg1 && p->reg2) - log("%s+%s]", ctx->r[p->reg1].name, ctx->r[p->reg2].name); - - else log("%s]", ctx->r[p->reg1 ? p->reg1 : p->reg2].name); - } - - else if (mfmt == AM_RRI) - { - if (p->reg1 && p->reg2) - log("%s+%s+%hd]", ctx->r[p->reg1].name, - ctx->r[p->reg2].name, p->imm2); - - else log("%s+%d]", ctx->r[p->reg1 ? p->reg1 : p->reg2].name, p->imm2); - } - - else if (mfmt == AM_RRII) - { - if (p->reg1) - log("%s+%s*%u+%hd]", - ctx->r[p->reg1].name, - ctx->r[p->reg2].name, - p->imm1, p->imm2); - else - log("%s*%u+%hd]", - ctx->r[p->reg2].name, - p->imm1, p->imm2); - } - } -} +static void dump_acc(ctx_t *ctx, acc_t *p); void dump_instr(ctx_t *ctx, instr_t *in, @@ -108,8 +56,9 @@ void dump_instr(ctx_t *ctx, else log("\t"); +#ifndef _ATT_STYLE log("%s\t", in->name); - + if (p1) { dump_acc(ctx, p1); @@ -119,6 +68,147 @@ void dump_instr(ctx_t *ctx, } } +#else + char s1[2] = {0, 0}, s2[2] = {0, 0}; + + if (p1 && ACC_IS_MEM(p1)) + s1[0] = getmempref(p1->mlen); + + if (p2 && ACC_IS_MEM(p2)) + s2[0] = getmempref(p2->mlen); + + log("%s%s%s\t", in->name, s1, s2); + + if (p1) { + if (p2) { + dump_acc(ctx, p2); + log(", "); + } + + dump_acc(ctx, p1); + } +#endif + log("\n"); } +#ifndef _ATT_STYLE + +void dump_acc(ctx_t *ctx, acc_t *p) +{ + uint mfmt; + + if (p->type == A_REG) + log("%s", ctx->r[p->reg].name); + + else if (p->type == A_IMM64) + { + if (p->val < 0xA) + log("%lu", p->val); + + else + log("0x%lX", p->val); + } + + else + { + log("%c[", getmempref(p->mlen)); + + mfmt = p->type & AM_MFMT_MASK; + + if (mfmt == AM_IMM64) + log("0x%lX]", p->addr); + + else if (mfmt == AM_RR) + { + if (p->reg1 && p->reg2) + log("%s+%s]", ctx->r[p->reg1].name, ctx->r[p->reg2].name); + + else log("%s]", ctx->r[p->reg1 ? p->reg1 : p->reg2].name); + } + + else if (mfmt == AM_RRI) + { + if (p->reg1 && p->reg2) + log("%s+%s+%hd]", ctx->r[p->reg1].name, + ctx->r[p->reg2].name, p->imm2); + + else log("%s+%hd]", + ctx->r[p->reg1 ? p->reg1 : p->reg2].name, p->imm2); + } + + else if (mfmt == AM_RRII) + { + if (p->reg1) + log("%s+%s*%u+%hd]", + ctx->r[p->reg1].name, + ctx->r[p->reg2].name, + p->imm1, p->imm2); + else + log("%s*%u+%hd]", + ctx->r[p->reg2].name, + p->imm1, p->imm2); + } + } +} + +#else + +void dump_acc(ctx_t *ctx, acc_t *p) +{ + uint mfmt; + + if (p->type == A_REG) + log("%%%s", ctx->r[p->reg].name); + + else if (p->type == A_IMM64) + { + if (p->val < 0xA) + log("$%lu", p->val); + + else + log("$0x%lX", p->val); + } + + else + { + mfmt = p->type & AM_MFMT_MASK; + + if (mfmt == AM_IMM64) + log("($0x%lX)", p->addr); + + else if (mfmt == AM_RR) + { + if (p->reg1 && p->reg2) + log("(%%%s,%%%s)", ctx->r[p->reg1].name, ctx->r[p->reg2].name); + + else log("(%%%s)", ctx->r[p->reg1 ? p->reg1 : p->reg2].name); + } + + else if (mfmt == AM_RRI) + { + if (p->reg1 && p->reg2) + log("%hd(%%%s,%%%s)", p->imm2, ctx->r[p->reg1].name, + ctx->r[p->reg2].name); + + else log("%hd(%%%s)", p->imm2, + ctx->r[p->reg1 ? p->reg1 : p->reg2].name); + } + + else if (mfmt == AM_RRII) + { + if (p->reg1) + log("%hd(%s,%s,$%u)", p->imm2, + ctx->r[p->reg1].name, + ctx->r[p->reg2].name, + p->imm1); + else + log("%hd(*,%s,$%u)", p->imm2, + ctx->r[p->reg2].name, + p->imm1); + } + } +} + +#endif + diff --git a/vm/pc/main.c b/vm/pc/main.c index b3e393a..818cb94 100644 --- a/vm/pc/main.c +++ b/vm/pc/main.c @@ -68,8 +68,6 @@ int main(int argc, char **argv) main_ctx.r = arch_r; main_ctx.i = arch_i; - - disable_stdin_echoing(); // // srand @@ -87,7 +85,6 @@ int main(int argc, char **argv) // // Load program // - if (argc < 2) { log("Not enough arguments\n"); exit(-3); @@ -133,6 +130,8 @@ int main(int argc, char **argv) exit(-10); } + disable_stdin_echoing(); + while (1) { decode(&main_ctx);