操作码到字节的主要图表

Primary chart of opcodes to bytes

我正在尝试使用这个简单的 hello world assembly program 并从头开始将其生成为机器语言(最终目的是制作汇编程序,但现在我只想了解此操作所需的十六进制值仅限特定程序):

section .text
    global _start //I'm guessing not needed for the machine language, only assembler or linker

_start: ;label, I don't know if this is somehow decoded to something in machine language or not
    mov edx,myLength ;"DX" register: the data register, apparently moving a new empty variable "myLength" into t, since it wasn't defined before
    mov ecx,coby ;move a new variable "coby" into the CX or "count register", not sure why this register was used for holding text, as it's usually used as a decrementer, but ok
    mov ebx,1 "BX" is the base register, used for indexing things, like system commands I'm guessing, although I don't know what else. In this case though "1" corresponds to the stdout command
    mov eax,4 ;"AX" the primary register, used to execute a certain command, in this case "4" or "sys_write", which I'm guessing simply is the equivalent of writing to the terminal, somehow, but I don't know what exactly it's writing
    int 0x80 ;interrupt at "20" which means call whatever is stored in eax, in this case sys_write 

    mov eax,1 ;"1" is exit, moving to primary register to perform next
    int 0x80 ;executes the command stored in the primary register "AX", in this case, exit

segment .data ;pretty sure this stuff is just for the assembler or linker, but I could be wrong
coby db 'Yo, world just testing!?!!! 123, BTW', 0xa ;somehow sets the value of the message variable "coby", although I don't know what "db" and 0xa (10) are for
myLength equ $ - coby ;somehow sets the value of myLength, I know in general "-" represents an input, I'm guessing "$" is the length of, but I have absolutely no idea how to translate that to machine langauge, or if the assembler automatically reads the length and hard-codes it in instead? 

所以我知道一般的想法是机器语言只是input/output操作中“_start”中提到的部分。它还与 AX 寄存器和 DX 一起用于涉及大值的乘法和除法运算。但不是用文本写出来,每个命令都用数字(或二进制等)表示。

因此,例如"mov" 将是一个数字,"edx" 数据寄存器将由另一个寄存器表示,而"myLength" 将由另一个寄存器表示。命令 "int 0x80" -- 在“128”处中断,这意味着(我认为)调用存储在主寄存器中的任何命令,即 AX,将对应于表示命令 "int" 的某个数字,加上 0x80 (128),我猜它只是二进制的 128。

我还猜测所有额外的东西,比如 .text 段、.data 段,只是为了让汇编程序查找和替换上面使用的变量,但我猜是机器语言本身不保留任何类型的变量或常量,但如果我错了请纠正我。

所以理论上在机器语言中,我不需要为 "coby"(消息变量)定义一个变量来将它插入计数器寄存器(CX);相反,我只是插入原始字符串,或者在这种情况下,不是字符串,而是 char-code 字符,直接在代表 eCx 的任何数字之后(尽管我也不确定为什么在 this example,我猜它不仅用于存储递减的东西?)

无论如何,如果我的假设是正确的,那么我需要某种图表来告诉我哪些数字与哪些操作码完全对应(比如 movint 在这种情况下),以及什么寄存器(如 edx、eax 等),我还需要知道这些字节命令究竟以什么顺序存储在二进制文件中。我尝试使用 NASM(使用命令 nasm -f elf yo.asm && ld -m elf_i386 -s -o yoman yo.o)并查看 ANSI 中的内容,这是我从 JavaScript 控制台打印到 HTML 页面时得到的内容,每个数字在自己的行上标记,连同它的二进制值、字符代码和实际的 unicode 文本(在 HTML 中并不总是可见):

000: 01111111, 127: 
001: 01000101, 069: E
002: 01001100, 076: L
003: 01000110, 070: F
004: 00000001, 001: 
005: 00000001, 001: 
006: 00000001, 001: 
007: 00000000, 000:
008: 00000000, 000:
009: 00000000, 000:
010: 00000000, 000:
011: 00000000, 000:
012: 00000000, 000:
013: 00000000, 000:
014: 00000000, 000:
015: 00000000, 000:
016: 00000010, 002: 
017: 00000000, 000:
018: 00000011, 003: 
019: 00000000, 000:
020: 00000001, 001: 
021: 00000000, 000:
022: 00000000, 000:
023: 00000000, 000:
024: 10000000, 128: €
025: 10000000, 128: €
026: 00000100, 004: 
027: 00001000, 008: 
028: 00110100, 052: 4
029: 00000000, 000:
030: 00000000, 000:
031: 00000000, 000:
032: 11011100, 220: Ü
033: 00000000, 000:
034: 00000000, 000:
035: 00000000, 000:
036: 00000000, 000:
037: 00000000, 000:
038: 00000000, 000:
039: 00000000, 000:
040: 00110100, 052: 4
041: 00000000, 000:
042: 00100000, 032:
043: 00000000, 000:
044: 00000010, 002: 
045: 00000000, 000:
046: 00101000, 040: (
047: 00000000, 000:
048: 00000100, 004: 
049: 00000000, 000:
050: 00000011, 003: 
051: 00000000, 000:
052: 00000001, 001: 
053: 00000000, 000:
054: 00000000, 000:
055: 00000000, 000:
056: 00000000, 000:
057: 00000000, 000:
058: 00000000, 000:
059: 00000000, 000:
060: 00000000, 000:
061: 10000000, 128: €
062: 00000100, 004: 
063: 00001000, 008: 
064: 00000000, 000:
065: 10000000, 128: €
066: 00000100, 004: 
067: 00001000, 008: 
068: 10011101, 157: 
069: 00000000, 000:
070: 00000000, 000:
071: 00000000, 000:
072: 10011101, 157: 
073: 00000000, 000:
074: 00000000, 000:
075: 00000000, 000:
076: 00000101, 005: 
077: 00000000, 000:
078: 00000000, 000:
079: 00000000, 000:
080: 00000000, 000:
081: 00010000, 016: 
082: 00000000, 000:
083: 00000000, 000:
084: 00000001, 001: 
085: 00000000, 000:
086: 00000000, 000:
087: 00000000, 000:
088: 10100000, 160:  
089: 00000000, 000:
090: 00000000, 000:
091: 00000000, 000:
092: 10100000, 160:  
093: 10010000, 144: 
094: 00000100, 004: 
095: 00001000, 008: 
096: 10100000, 160:  
097: 10010000, 144: 
098: 00000100, 004: 
099: 00001000, 008: 
100: 00100101, 037: %
101: 00000000, 000:
102: 00000000, 000:
103: 00000000, 000:
104: 00100101, 037: %
105: 00000000, 000:
106: 00000000, 000:
107: 00000000, 000:
108: 00000110, 006: 
109: 00000000, 000:
110: 00000000, 000:
111: 00000000, 000:
112: 00000000, 000:
113: 00010000, 016: 
114: 00000000, 000:
115: 00000000, 000:
116: 00000000, 000:
117: 00000000, 000:
118: 00000000, 000:
119: 00000000, 000:
120: 00000000, 000:
121: 00000000, 000:
122: 00000000, 000:
123: 00000000, 000:
124: 00000000, 000:
125: 00000000, 000:
126: 00000000, 000:
127: 00000000, 000:
128: 10111010, 186: º
129: 00100101, 037: %
130: 00000000, 000:
131: 00000000, 000:
132: 00000000, 000:
133: 10111001, 185: ¹
134: 10100000, 160:  
135: 10010000, 144: 
136: 00000100, 004: 
137: 00001000, 008: 
138: 10111011, 187: »
139: 00000001, 001: 
140: 00000000, 000:
141: 00000000, 000:
142: 00000000, 000:
143: 10111000, 184: ¸
144: 00000100, 004: 
145: 00000000, 000:
146: 00000000, 000:
147: 00000000, 000:
148: 11001101, 205: Í
149: 10000000, 128: €
150: 10111000, 184: ¸
151: 00000001, 001: 
152: 00000000, 000:
153: 00000000, 000:
154: 00000000, 000:
155: 11001101, 205: Í
156: 10000000, 128: €
157: 00000000, 000:
158: 00000000, 000:
159: 00000000, 000:
160: 01011001, 089: Y
161: 01101111, 111: o
162: 00101100, 044: ,
163: 00100000, 032:
164: 01110111, 119: w
165: 01101111, 111: o
166: 01110010, 114: r
167: 01101100, 108: l
168: 01100100, 100: d
169: 00100000, 032:
170: 01101010, 106: j
171: 01110101, 117: u
172: 01110011, 115: s
173: 01110100, 116: t
174: 00100000, 032:
175: 01110100, 116: t
176: 01100101, 101: e
177: 01110011, 115: s
178: 01110100, 116: t
179: 01101001, 105: i
180: 01101110, 110: n
181: 01100111, 103: g
182: 00100001, 033: !
183: 00111111, 063: ?
184: 00100001, 033: !
185: 00100001, 033: !
186: 00100001, 033: !
187: 00100000, 032:
188: 00110001, 049: 1
189: 00110010, 050: 2
190: 00110011, 051: 3
191: 00101100, 044: ,
192: 00100000, 032:
193: 01000010, 066: B
194: 01010100, 084: T
195: 01010111, 087: W
196: 00001010, 010:

197: 00000000, 000:
198: 00101110, 046: .
199: 01110011, 115: s
200: 01101000, 104: h
201: 01110011, 115: s
202: 01110100, 116: t
203: 01110010, 114: r
204: 01110100, 116: t
205: 01100001, 097: a
206: 01100010, 098: b
207: 00000000, 000:
208: 00101110, 046: .
209: 01110100, 116: t
210: 01100101, 101: e
211: 01111000, 120: x
212: 01110100, 116: t
213: 00000000, 000:
214: 00101110, 046: .
215: 01100100, 100: d
216: 01100001, 097: a
217: 01110100, 116: t
218: 01100001, 097: a
219: 00000000, 000:
220: 00000000, 000:
221: 00000000, 000:
222: 00000000, 000:
223: 00000000, 000:
224: 00000000, 000:
225: 00000000, 000:
226: 00000000, 000:
227: 00000000, 000:
228: 00000000, 000:
229: 00000000, 000:
230: 00000000, 000:
231: 00000000, 000:
232: 00000000, 000:
233: 00000000, 000:
234: 00000000, 000:
235: 00000000, 000:
236: 00000000, 000:
237: 00000000, 000:
238: 00000000, 000:
239: 00000000, 000:
240: 00000000, 000:
241: 00000000, 000:
242: 00000000, 000:
243: 00000000, 000:
244: 00000000, 000:
245: 00000000, 000:
246: 00000000, 000:
247: 00000000, 000:
248: 00000000, 000:
249: 00000000, 000:
250: 00000000, 000:
251: 00000000, 000:
252: 00000000, 000:
253: 00000000, 000:
254: 00000000, 000:
255: 00000000, 000:
256: 00000000, 000:
257: 00000000, 000:
258: 00000000, 000:
259: 00000000, 000:
260: 00001011, 011: 
261: 00000000, 000:
262: 00000000, 000:
263: 00000000, 000:
264: 00000001, 001: 
265: 00000000, 000:
266: 00000000, 000:
267: 00000000, 000:
268: 00000110, 006: 
269: 00000000, 000:
270: 00000000, 000:
271: 00000000, 000:
272: 10000000, 128: €
273: 10000000, 128: €
274: 00000100, 004: 
275: 00001000, 008: 
276: 10000000, 128: €
277: 00000000, 000:
278: 00000000, 000:
279: 00000000, 000:
280: 00011101, 029: 
281: 00000000, 000:
282: 00000000, 000:
283: 00000000, 000:
284: 00000000, 000:
285: 00000000, 000:
286: 00000000, 000:
287: 00000000, 000:
288: 00000000, 000:
289: 00000000, 000:
290: 00000000, 000:
291: 00000000, 000:
292: 00010000, 016: 
293: 00000000, 000:
294: 00000000, 000:
295: 00000000, 000:
296: 00000000, 000:
297: 00000000, 000:
298: 00000000, 000:
299: 00000000, 000:
300: 00010001, 017: 
301: 00000000, 000:
302: 00000000, 000:
303: 00000000, 000:
304: 00000001, 001: 
305: 00000000, 000:
306: 00000000, 000:
307: 00000000, 000:
308: 00000011, 003: 
309: 00000000, 000:
310: 00000000, 000:
311: 00000000, 000:
312: 10100000, 160:  
313: 10010000, 144: 
314: 00000100, 004: 
315: 00001000, 008: 
316: 10100000, 160:  
317: 00000000, 000:
318: 00000000, 000:
319: 00000000, 000:
320: 00100101, 037: %
321: 00000000, 000:
322: 00000000, 000:
323: 00000000, 000:
324: 00000000, 000:
325: 00000000, 000:
326: 00000000, 000:
327: 00000000, 000:
328: 00000000, 000:
329: 00000000, 000:
330: 00000000, 000:
331: 00000000, 000:
332: 00000100, 004: 
333: 00000000, 000:
334: 00000000, 000:
335: 00000000, 000:
336: 00000000, 000:
337: 00000000, 000:
338: 00000000, 000:
339: 00000000, 000:
340: 00000001, 001: 
341: 00000000, 000:
342: 00000000, 000:
343: 00000000, 000:
344: 00000011, 003: 
345: 00000000, 000:
346: 00000000, 000:
347: 00000000, 000:
348: 00000000, 000:
349: 00000000, 000:
350: 00000000, 000:
351: 00000000, 000:
352: 00000000, 000:
353: 00000000, 000:
354: 00000000, 000:
355: 00000000, 000:
356: 11000101, 197: Å
357: 00000000, 000:
358: 00000000, 000:
359: 00000000, 000:
360: 00010111, 023: 
361: 00000000, 000:
362: 00000000, 000:
363: 00000000, 000:
364: 00000000, 000:
365: 00000000, 000:

366: 00000000, 000: 367:00000000,000: 368:00000000,000: 369:00000000,000: 370:00000000,000: 371:00000000,000: 372:00000001,001: 373:00000000,000: 374:00000000,000: 375:00000000,000: 376:00000000,000: 377:00000000,000: 378:00000000,000: 379:00000000,000:

所以文件很大。 380 字节,这有点令人惊讶,我认为它与 NASM 中的一堆 headers 和那个 ELF 东西有关,从顶部可以看出,所以我不知道实际在哪里汇编程序开始,如果可能的话,将它减少到那个。

所以一些立即突出的东西显然是打印到控制台的字符串值,它出现在上面输出的行 (AKA byte#) 160 处。字符串的长度恰好是36,应该对应于存储在数据寄存器中的第一个值(在汇编程序中称为"myLength"),虽然我找不到任何对应于“36”的字节" 所以我不确定该长度是如何准确存储的,是否被分解等等

其他一些值得注意的字节是数字 128,它对应于内核调用,或上面的中断号 (0x80),它出现在 (/byte#) 156 行的输出中,以及在第 149 行。它也出现在其他地方,但在那个特定的地方它出现在另一个字符串附近所以我猜它可能对应于传递给 "int" 中断命令的值,并且每个字节都在这些位置前面还有字节“205”,它作为一个 ANSI 字符看起来像这样的 Í 这可能以某种方式暗示它对应于命令 "int",特别是因为它紧接在数字 0x80 之前,但我不不确定。

所以我试着查看 intel docs as well as a really long PDF file,但找不到任何地方谈论:

1.什么字节码与什么操作码和命令完全对应并且

2。确切地说,将它们放入编译输出中的顺序/格式(不求助于任何第 3 方汇编程序,如 NASM)

我找到了一个 3rd party reference sheet 但我更想知道他们从原始文档中找到它的地方,虽然它显示了寄存器的一些代码,但我无法找到什么格式将它们放入生成的版本中。我也看到了类似问题的这些答案:

x86 opcode encoding: sib byte

hexadecimal value of opcodes

How to write and execute PURE machine code manually without containers like EXE or ELF?

但是 我真正想要的是对所有 regis 的完整官方参考错误和操作码

此类信息的规范来源是 Intel 和 AMD 参考手册。

英特尔手册位于 https://software.intel.com/en-us/articles/intel-sdm。有关指令编码的信息在第 2 卷第 2 章中。操作码表在第 2 卷附录 A 中。