簡體   English   中英

操作碼到字節的主要圖表

[英]Primary chart of opcodes to bytes

我正在嘗試使用這個簡單的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 控制台打印到 Z4C4AD5FCA2E7A3F74DBB1CED0038AA1 頁面時得到的將每個數字標記在自己的行上,連同其二進制值、字符代碼和實際 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:00000000,000:371:00000000、000:372:372:372:00000001、001:373:373:373:373:373:000000000000000000000000000000000000000000000000000000個00000000, 000: 375: 00000000, 000: 376: 00000000, 000: 377: 00000000, 000: 378: 00000000, 000: 379: 00000000, 000:

所以文件很大。 380 字節,有點令人驚訝,我認為這與來自 NASM 的一堆標頭和 ELF 的事情有關,從頂部可以看出,所以我不知道實際的匯編程序從哪里開始,如果可能的話把它減少到那個程度。

所以一些立即突出的東西顯然是打印到控制台的字符串值,它出現在上述 output 的第160行(AKA byte#)處。 字符串的長度恰好是 36,它應該對應於存儲在數據寄存器中的第一個值(在匯編程序中稱為“myLength”),盡管我找不到任何對應於“36”的字節所以我不確定該長度是如何准確存儲的,是否被分解等。

其他一些值得注意的字節是數字 128,它對應於內核調用,或上面的中斷號 (0x80),它出現在 output 的第 (/byte#) 156 行以及第 149 行。它也出現在其他地方,但是在那個特定的地方,它出現在另一個字符串附近,所以我猜它可能對應於傳遞給“int”中斷命令的值,並且這些位置中的每個字節前面也都有字節“205”,作為 ANSI 字符看起來像這個 Í,它可能以某種方式暗示它對應於命令“int”,特別是因為它緊接在數字 0x80 之前,但我不確定。

所以我嘗試查看英特爾文檔以及一個非常長的 PDF 文件,但我找不到任何談論的地方:

1. 哪些字節碼與哪些操作碼和命令一一對應

2. 究竟是什么順序/格式,將它們放入已編譯的 output 中(無需求助於任何 NASM 等第三方匯編程序)

我找到了第 3 方參考表,但我更想知道他們從原始文檔中得到它的位置,雖然它顯示了寄存器的一些代碼,但我找不到將它們放入的格式生成的版本。 我還看到了類似問題的這些答案:

x86 操作碼編碼:sib 字節

操作碼的十六進制值

如何在沒有 EXE 或 ELF 等容器的情況下手動編寫和執行 PURE 機器代碼?

我真正要尋找的是對所有寄存器和操作碼的完整和官方參考

此類信息的規范來源是 Intel 和 AMD 參考手冊。

英特爾手冊位於https://software.intel.com/en-us/articles/intel-sdm 有關指令編碼的信息在第 2 卷第 2 章。操作碼表在第 2 卷附錄 A。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM