简体   繁体   English

Poke操作码进入内存

[英]Poke opcodes into memory

Hi I am trying to understand whether it is possible to take instruction opcodes and 'poke' them into memory or smehow convert them to a binary program. 嗨,我试图了解是否可以采取指令操作码并将它们“戳”到内存中或将它们转换为二进制程序。 I have found an abandoned lisp project here: http://common-lisp.net/viewvc/cl-x86-asm/cl-x86-asm/ which takes x86 asm instructions and converts them into opcodes (please see example below). 我在这里找到了一个废弃的lisp项目: http//common-lisp.net/viewvc/cl-x86-asm/cl-x86-asm/ ,它接受x86 asm指令并将它们转换为操作码(请参见下面的示例)。 The project does not go further to actually complete the creation of the binary executable. 该项目没有进一步实际完成二进制可执行文件的创建。 Hence I would need to do that 'manually' Any ideas can help me. 因此我需要“手动”做到这一点任何想法都可以帮助我。 Thanks. 谢谢。

 ;; assemble some code in it
(cl-x86-asm::assemble-forms 
  '((.Entry :PUSH :EAX)
    (:SUB :EAX #XFFFEA)
    (:MOV :EAX :EBX)
    (:POP :EAX)
    (:PUSH :EAX)
    (.Exit :RET))

Processing... 处理...

;; print the assembled segment
(cl-x86-asm::print-segment)

* Segment type DATA-SEGMENT
Segment size 0000000C bytes
50 81 05 00 0F FF EA 89
03 58 50 C3

Clozure Common Lisp for example has this built-in. 例如Clozure Common Lisp就有这个内置功能。 This is usually called LAP , Lisp Assembly Program . 这通常称为LAPLisp汇编程序

See defx86lapfunction . defx86lapfunction

Example: 例:

(defx86lapfunction fast-mod ((number arg_y) (divisor arg_z))
  (xorq (% imm1) (% imm1))
  (mov (% number) (% imm0))
  (div (% divisor))
  (mov (% imm1) (% arg_z))
  (single-value-return))

SBCL can do some similar with VOP (Virtual Operations). SBCL可以与VOP (虚拟操作)做类似的事情。

http://g000001.cddddr.org/2011-12-08 http://g000001.cddddr.org/2011-12-08

I learned that it can be done using CFFI/FFI for example the very simple asm code: 我了解到它可以使用CFFI / FFI完成,例如非常简单的asm代码:

(:movl 12 :eax)
(:ret)

This will be converted to the following sequence of octets: #(184 12 0 0 0 195) which in hex it is: #(B8 C 0 0 0 C3). 这将转换为以下八位字节序列:#(184 12 0 0 0 195),以十六进制表示:#(B8 C 0 0 0 C3)。 The next step is to send it to a location in memory as such: 下一步是将其发送到内存中的位置:

(defparameter pointer (cffi:foreign-alloc :unsigned-char :initial-contents #(184 12 0 0 0 195)))
;; and then execute it as such to return the integer 12:
(cffi:foreign-funcall-pointer pointer () :int)
=> result: 12

Thanks to the experts in #lisp (freenode irc channel) for helping out with this solution. 感谢#lisp(freenode irc频道)的专家帮助解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM