操作码到字节的主图表



我正在尝试使用这个简单的hello-world汇编程序,并从零开始用机器语言生成它(最终目的是制作汇编程序,但目前我只想了解这个特定程序所需的十六进制值):

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? 

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

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

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

因此,从理论上讲,在机器语言中,我不需要将变量定义为"coby"(消息变量)来将其插入计数器寄存器(CX);相反,我会直接在代表eCx的任何数字后面插入原始字符串,或者在这种情况下,不是字符串,而是字符代码字符(尽管我也不确定为什么在这个例子中使用计数器寄存器,我想它不仅用于存储递减的东西?)

无论如何,如果我的假设是正确的,那么我需要某种图表,告诉什么数字与什么操作码(在这种情况下,如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:00000000000:367:00000000000:368:00000000000:369:00000000000:370:00000000000:371:00000000000:372:00000000001:373:00000000000:374:00000000000:375:00000000000:376:00000000000:377:00000000000:378:00000000000:379:00000000000:

所以这个文件很大。380字节,这有点令人惊讶,我认为这与NASM和ELF的一堆头有关,从顶部可以明显看出,所以我不知道实际的汇编程序从哪里开始,也不知道是否可以将其简化为这样。

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

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

所以我试着在英特尔文档和一个很长的PDF文件中查找,但我找不到任何关于的内容

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

2.将它们放入编译输出中的确切顺序/格式(无需求助于任何第三方汇编程序,如NASM)

我找到了一份第三方参考资料表,但我更想知道他们从原始文档中哪里得到的,尽管它显示了寄存器的一些代码,但我找不到在生成的版本中使用什么格式。我也看到了类似问题的答案:

x86操作码编码:sib-byte

操作码的十六进制值

如何在没有EXE或ELF等容器的情况下手动编写和执行PURE机器代码?

但是我真正想要的是对所有寄存器和操作码的完整和官方引用

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

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

最新更新