How to resolve the algorithm Machine code step by step in the Common Lisp programming language
How to resolve the algorithm Machine code step by step in the Common Lisp programming language
Table of Contents
Problem Statement
The task requires poking machine code directly into memory and executing it. The machine code is the architecture-specific opcodes which have the simple task of adding two unsigned bytes together and making the result available to the high-level language. For example, the following assembly language program is given for x86 (32 bit) architectures: This would translate into the following opcode bytes: Or in hexadecimal: If different than 32-bit x86, specify the target architecture of the machine code for your example. It may be helpful to also include an assembly version of the machine code for others to reference and understand what is being executed. Then, implement the following in your favorite programming language:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Machine code step by step in the Common Lisp programming language
Source code in the common programming language
;;Note that by using the 'CFFI' library, one can apply this procedure portably in any lisp implementation;
;; in this code however I chose to demonstrate only the implementation-dependent programs.
;;CCL
;; Allocate a memory pointer and poke the opcode into it
(defparameter ptr (ccl::malloc 9))
(loop for i in '(139 68 36 4 3 68 36 8 195)
for j from 0 do
(setf (ccl::%get-unsigned-byte ptr j) i))
;; Execute with the required arguments and return the result as an unsigned-byte
(ccl::ff-call ptr :UNSIGNED-BYTE 7 :UNSIGNED-BYTE 12 :UNSIGNED-BYTE)
;; Output = 19
;; Free the pointer
(ccl::free ptr)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;SBCL
(defparameter mmap (list 139 68 36 4 3 68 36 8 195))
(defparameter pointer (sb-alien:make-alien sb-alien:unsigned-char (length mmap)))
(defparameter callp (loop for byte in mmap
for i from 0
do
(setf (sb-alien:deref pointer i) byte)
finally
(return (sb-alien:cast pointer (function integer integer integer)))))
(sb-alien:alien-funcall callp 7 12)
(loop for i from 0 below 18 collect (sb-alien:deref ptr i))
(sb-alien:free-alien pointer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;CLISP
(defparameter mmap (list 139 68 36 4 3 68 36 8 195))
(defparameter POINTER (FFI:FOREIGN-ADDRESS (FFI:FOREIGN-ALLOCATE 'FFI:UINT8 :COUNT 9)))
(loop for i in mmap
for j from 0 do
(FUNCALL #'(SETF FFI:MEMORY-AS) i POINTER 'FFI:INT j))
(FUNCALL
(FFI:FOREIGN-FUNCTION POINTER
(LOAD-TIME-VALUE
(FFI:PARSE-C-TYPE
'(FFI:C-FUNCTION (:ARGUMENTS 'FFI:INT 'FFI:INT) (:RETURN-TYPE FFI:INT) (:LANGUAGE :STDC)))))
7 12)
(FFI:FOREIGN-FREE POINTER)
You may also check:How to resolve the algorithm Pinstripe/Printer step by step in the Nim programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the jq programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the J programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Pike programming language
You may also check:How to resolve the algorithm Special variables step by step in the Lua programming language