Skip to content

Commit

Permalink
Implement movs
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed May 24, 2018
1 parent eb29bef commit 210f2c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
3 changes: 3 additions & 0 deletions emu/gadgets-x86/gadgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@

.endr

#define ifin(thing, ...) irp da_op, __VA_ARGS__; .ifc thing,\da_op
#define endifin endif; .endr

# sync with enum reg
#define REG_LIST reg_a,reg_c,reg_d,reg_b,reg_sp,reg_bp,reg_si,reg_di
# sync with enum arg
Expand Down
16 changes: 5 additions & 11 deletions emu/gadgets-x86/math.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,30 @@

# this would have been just a few nice compact nested loops, but gas said "nuh uh"

#define ifin(thing, ...) irp da_op, __VA_ARGS__; .ifc thing,\da_op
#define endifin endif; .endr
#undef _tmp
#define _tmp r10
.macro _do_op op, arg, size, s, ss
.ifc \op,load
mov\ss \arg, %_tmp\s
mov\ss \arg, %tmp\s
.else; .ifc \op,store
mov\ss %_tmp\s, \arg
mov\ss %tmp\s, \arg
.else
.ifin(\op, add,sub)
mov\ss \arg, %r14\s
setf_a src=%r14\s, dst=%_tmp\s, ss=\ss
setf_a src=%r14\s, dst=%tmp\s, ss=\ss
.endifin
.ifin(\op, and,or,xor)
clearf_a
clearf_oc
.endifin
\op\ss \arg, %_tmp\s
\op\ss \arg, %tmp\s
.ifin(\op, add,sub)
setf_oc
.endifin
setf_zsp %_tmp\s, \ss
setf_zsp %tmp\s, \ss
.endif; .endif
.endm
.macro do_op op, size, arg
ss \size, _do_op, \op, \arg
.endm
#undef _tmp
#define _tmp r10d

.macro do_reg_op op, size, reg
.gadget \op\size\()_reg_\reg
Expand Down
36 changes: 24 additions & 12 deletions emu/gadgets-x86/string.S
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
andl $~DF_FLAG, CPU_eflags(%_cpu)
gret

.macro do_strop op, size, rep
.macro do_strop op, size, rep, s, ss, a
# repnz is only a thing for cmps and scas
.ifc \rep,repnz
.ifnc \rep,cmps; .ifnc \rep,scas
Expand All @@ -19,32 +19,44 @@
.ifc \op,stos
movl %edi, %_addr
write_prep
.if \size == 8
movb %al, (%_addrq)
.elseif \size == 16
movw %ax, (%_addrq)
.elseif \size == 32
movl %eax, (%_addrq)
.endif
mov\ss %\a, (%_addrq)

.else; .ifc \op,movs
movl %esi, %_addr
read_prep
mov\ss (%_addrq), %tmp\s
movl %edi, %_addr
write_prep
mov\ss %tmp\s, (%_addrq)
.endif; .endif
.ifin(\op, lods,movs,cmps,scas)
addl $(\size/8), %esi
.endifin
.ifin(\op, movs,stos,cmps)
addl $(\size/8), %edi
.endif
.endifin
.ifc \rep,rep
decl %ecx
jnz 1b
.endif
gret
.endm

.irp op, stos
.irp op, movs,stos
.irp size, 8,16,32
.irp rep, REP_LIST
do_strop \op, \size, \rep
.if \size == 8
do_strop \op, \size, \rep, b, b, al
.elseif \size == 16
do_strop \op, \size, \rep, w, w, ax
.elseif \size == 32
do_strop \op, \size, \rep, d, l, eax
.endif
.endr
.endr
.gadget_list_size \op, REP_LIST
.endr
# temporary
.gadget_list_size movs, REP_LIST
.gadget_list_size lods, REP_LIST
.gadget_list_size scas, REP_LIST
.gadget_list_size cmps, REP_LIST

0 comments on commit 210f2c5

Please sign in to comment.