一个简单的汇编代码高亮JS逻辑

效果:

汇编代码高亮

lea         rax,[b]  
lea         rcx,[a]  
mov         rdi,rax  
mov         rsi,rcx  
mov         ecx,80h  
rep movs    byte ptr [rdi],byte ptr [rsi]  
mov         eax,0Ah  
imul        rax,rax,0  
lea         rax,[rbp+rax+0C8h]  
mov         rdx,rax  
lea         rcx,[string "b.strs[0]=%s\n" (07FF74AF79D38h)]  
call        printf (07FF74AF711D1h)  
lea         rax,[rbp+210h]  
lea         rcx,[b]  
mov         rdi,rax  
mov         rsi,rcx  
mov         ecx,80h  
rep movs    byte ptr [rdi],byte ptr [rsi]  
lea         rdx,[rbp+210h]  
lea         rcx,[string "hello  world.  b = %d" (07FF74AF79D48h)]  
call        printf (07FF74AF711D1h)
 
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
<body>
 
<h2>汇编代码高亮</h2>
 
<p id="demo"></p>
 
<script>
function highlightAssemblyCode(assemblyCode) {
    // 匹配字符串
    var highlighted = assemblyCode.replace(/(".*?"|\'.*?\')/g, '<span style="color: #7f007f;">$1</span>');
 
    // 匹配指令
    var highlighted = highlighted.replace(/(lea|mov|rep movs|imul|call)/g, '<span style="color: #00007f; font-weight: bold;">$1</span>');
 
    // 匹配寄存器
    highlighted = highlighted.replace(/(rax|rcx|rdi|rsi|rdx|rbp|eax|ecx)/g, '<span style="color: #46aa03; font-weight: bold;">$1</span>');
 
    // 匹配十六进制数
    highlighted = highlighted.replace(/(0x[a-fA-F0-9]+|\b[0-9A-Fa-f]+h\b)/g, '<span style="color: #ff0000;">$1</span>');   
 
    // 匹配方括号和括号
    // highlighted = highlighted.replace(/(\[|\]|\(|\))/g, '<span style="color: #009900; font-weight: bold;">$1</span>');
     
    // 匹配byte ptr
    highlighted = highlighted.replace(/(byte ptr)/g, '<span style="color: #0000ff; font-weight: bold;">$1</span>');
     
    // 添加换行
    highlighted = highlighted.replace(/\n/g, '<br>\n');
     
    return highlighted;
}
 
var assemblyCode = `lea         rax,[b] 
lea         rcx,[a] 
mov         rdi,rax 
mov         rsi,rcx 
mov         ecx,80h 
rep movs    byte ptr [rdi],byte ptr [rsi] 
mov         eax,0Ah 
imul        rax,rax,0 
lea         rax,[rbp+rax+0C8h] 
mov         rdx,rax 
lea         rcx,[string "b.strs[0]=%s" (07FF74AF79D38h)] 
call        printf (07FF74AF711D1h) 
lea         rax,[rbp+210h] 
lea         rcx,[b] 
mov         rdi,rax 
mov         rsi,rcx 
mov         ecx,80h 
rep movs    byte ptr [rdi],byte ptr [rsi] 
lea         rdx,[rbp+210h] 
lea         rcx,[string "hello  world.  b = %d" (07FF74AF79D48h)] 
call        printf (07FF74AF711D1h)`;
 
document.getElementById("demo").innerHTML = highlightAssemblyCode(assemblyCode);
</script>
 
</body>
</html>

  

参考:

https://highlight.hohli.com/index.php

posted @   bonelee  阅读(61)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2016-12-18 LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路
2016-12-18 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2
2016-12-18 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
2016-12-18 319. Bulb Switcher——本质:迭代观察,然后找规律
2016-12-18 436. Find Right Interval ——本质:查找题目,因此二分!
点击右上角即可分享
微信分享提示