Skip to content

Commit 067e76d

Browse files
committed
add assembly code tutorial
1 parent 487b308 commit 067e76d

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
8181
-[How to Get the Size of Directories in Python](https://www.thepythoncode.com/article/get-directory-size-in-bytes-using-python). ([code](general/calculate-directory-size))
8282
-[How to Play and Record Audio in Python](https://www.thepythoncode.com/article/play-and-record-audio-sound-in-python). ([code](general/recording-and-playing-audio))
8383
-[How to Get Geographic Locations in Python](https://www.thepythoncode.com/article/get-geolocation-in-python). ([code](general/geolocation))
84+
-[How to Assembly, Disassembly and Emulate Machine Code using Python](https://www.thepythoncode.com/article/arm-x86-64-assembly-disassembly-and-emulation-in-python). ([code](general/assembly-code))
8485

8586

8687
-### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)

‎general/assembly-code/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Assembly, Disassembly and Emulate Machine Code using Python](https://www.thepythoncode.com/article/arm-x86-64-assembly-disassembly-and-emulation-in-python)
2+
To run this:
3+
-`pip3 install -r requirements.txt`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# We need to emulate ARM
2+
fromunicornimportUc, UC_ARCH_ARM, UC_MODE_ARM, UcError
3+
# for accessing the R0 and R1 registers
4+
fromunicorn.arm_constimportUC_ARM_REG_R0, UC_ARM_REG_R1
5+
# We need to assemble ARM code
6+
fromkeystoneimportKs, KS_ARCH_ARM, KS_MODE_ARM, KsError
7+
8+
9+
ARM_CODE="""
10+
// n is r0, we will pass it from python, ans is r1
11+
mov r1, 1 // ans = 1
12+
loop:
13+
cmp r0, 0 // while n >= 0:
14+
mulgt r1, r1, r0 // ans *= n
15+
subgt r0, r0, 1 // n = n - 1
16+
bgt loop //
17+
// answer is in r1
18+
"""
19+
20+
print("Assembling the ARM code")
21+
try:
22+
# initialize the keystone object with the ARM architecture
23+
ks=Ks(KS_ARCH_ARM, KS_MODE_ARM)
24+
# Assemble the ARM code
25+
ARM_BYTECODE, _=ks.asm(ARM_CODE)
26+
# convert the array of integers into bytes
27+
ARM_BYTECODE=bytes(ARM_BYTECODE)
28+
print(f"Code successfully assembled (length = {len(ARM_BYTECODE)})")
29+
print("ARM bytecode:", ARM_BYTECODE)
30+
exceptKsErrorase:
31+
print("Keystone Error: %s"%e)
32+
exit(1)
33+
34+
35+
# memory address where emulation starts
36+
ADDRESS=0x1000000
37+
38+
print("Emulating the ARM code")
39+
try:
40+
# Initialize emulator in ARM mode
41+
mu=Uc(UC_ARCH_ARM, UC_MODE_ARM)
42+
# map 2MB memory for this emulation
43+
mu.mem_map(ADDRESS, 2*1024*1024)
44+
# write machine code to be emulated to memory
45+
mu.mem_write(ADDRESS, ARM_BYTECODE)
46+
# Set the r0 register in the code, let's calculate factorial(5)
47+
mu.reg_write(UC_ARM_REG_R0, 5)
48+
# emulate code in infinite time and unlimited instructions
49+
mu.emu_start(ADDRESS, ADDRESS+len(ARM_BYTECODE))
50+
# now print out the R0 register
51+
print("Emulation done. Below is the result")
52+
# retrieve the result from the R1 register
53+
r1=mu.reg_read(UC_ARM_REG_R1)
54+
print(">> R1 = %u"%r1)
55+
exceptUcErrorase:
56+
print("Unicorn Error: %s"%e)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# We need to emulate ARM and x86 code
2+
fromunicornimportUc, UC_ARCH_X86, UC_MODE_64, UcError
3+
# for accessing the RAX and RDI registers
4+
fromunicorn.x86_constimportUC_X86_REG_RDI, UC_X86_REG_RAX
5+
# We need to disassemble x86_64 code
6+
fromcapstoneimportCs, CS_ARCH_X86, CS_MODE_64, CsError
7+
8+
9+
X86_MACHINE_CODE=b"\x48\x31\xc0\x48\xff\xc0\x48\x85\xff\x0f\x84\x0d\x00\x00\x00\x48\x99\x48\xf7\xe7\x48\xff\xcf\xe9\xea\xff\xff\xff"
10+
11+
# memory address where emulation starts
12+
ADDRESS=0x1000000
13+
14+
try:
15+
# Initialize the disassembler in x86 mode
16+
md=Cs(CS_ARCH_X86, CS_MODE_64)
17+
# iterate over each instruction and print it
18+
forinstructioninmd.disasm(X86_MACHINE_CODE, 0x1000):
19+
print("0x%x:\t%s\t%s"% (instruction.address, instruction.mnemonic, instruction.op_str))
20+
exceptCsErrorase:
21+
print("Capstone Error: %s"%e)
22+
23+
try:
24+
# Initialize emulator in x86_64 mode
25+
mu=Uc(UC_ARCH_X86, UC_MODE_64)
26+
# map 2MB memory for this emulation
27+
mu.mem_map(ADDRESS, 2*1024*1024)
28+
# write machine code to be emulated to memory
29+
mu.mem_write(ADDRESS, X86_MACHINE_CODE)
30+
# Set the r0 register in the code to the number of 7
31+
mu.reg_write(UC_X86_REG_RDI, 7)
32+
# emulate code in infinite time & unlimited instructions
33+
mu.emu_start(ADDRESS, ADDRESS+len(X86_MACHINE_CODE))
34+
# now print out the R0 register
35+
print("Emulation done. Below is the result")
36+
rax=mu.reg_read(UC_X86_REG_RAX)
37+
print(">>> RAX = %u"%rax)
38+
exceptUcErrorase:
39+
print("Unicorn Error: %s"%e)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
keystone-engine
2+
capstone
3+
unicorn

0 commit comments

Comments
(0)