天天看點

VS2015中運作32位彙程式設計式

【前言】

  這學期開彙編語言課。作為培養方案中的一門選修課,與課組中教微機原理、嵌入式的同僚商量,決定這門課就以8086為載體,帶大家入門即可。不過,在課程結束之前,也向同學們做了拓展。

  本文的版權屬于我的2015級學生小魯同學。他在課程設計階段,費了不少周折,配置環境,完成了32位彙程式設計式的開發。他将整個過程做了記錄,并且完成本文。

  我說:“你将這些文字發成部落格做個分享。”他說:“不發了。老師,您的部落格點選量大,給您吧。”

  這是本文的由來。感謝這位自主、慷慨的弟子!

【正文】

  Visual Studio 2015隻需要14步就可以運作32位彙編語言。你必須建立一個32位的控制台應用(原本32位控制台應用用于C++編寫)并且修改幾個地方的設定。

Step1:選擇左上角的建立項目,選擇建立Win32控制台應用程式。(Depending on your Visual Studio configuration, you might have to find Visual C++ under the “Other Languages” category in the left panel.)當然你的Visual Studio 2015的win32控制選項可能需要你自己找找了,但是Visual Studio 2015必須可以編寫并運作C++程式。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

Step2:都搞好之後點确定,進行下一步的選擇,點選下一步,之後選擇空項目。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

Step3:點完成儲存項目的設定。

Step4:此時方案已經生成,在右邊顯示了目前的項目名稱,接下來需要點選滑鼠右鍵選擇生成依賴項,選擇生成自定義,此時會出現一個生成自定義檔案的選擇對話框,選擇masm,然後點選确定。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

Step5:此時就可以進行基本的80386彙程式設計式了,在右上方的項目的源檔案中添加建立項,建立C++檔案,将名稱改為*.asm作為彙程式設計式,之後點選添加。添加一個測試檔案試試是否可行。

将此段彙程式設計式寫入建立好的mytest.asm檔案中,點選上方的生成,生成解決方案,之後觀察目前控制台的提示,成功之後就可以點選本地Windows調試器運作了。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

下載下傳好之後安裝即可,我安裝在C槽的預設目錄下,即:C:\Irvine。之後進行配置。

Step7:右擊項目,選擇屬性,在彈出的對話框中選擇Microsoft Macro Assembly,就會看到如下配置。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

Step8:選擇General,然後點選Include Paths,點選編輯,将剛剛安裝的Irvine的目錄寫入編輯框中,This tells the assembler where to find files having a filename extension of “.inc”.告訴編輯器去哪裡尋找字尾名位inc的檔案,輸入好點選确定。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

Step9:接下來我們想讓VS2015生成連結檔案,友善我們檢視彙編代碼對應的機器代碼,點選Listing File,點選Assembled Code Listing File點選編輯,輸入$(ProjectName).lst,确定。

VS2015中運作32位彙程式設計式

Step10: 找到連接配接器,選擇輸入,在附加依賴庫中添加irvine32.lib,注意每個依賴庫以分号結尾!

VS2015中運作32位彙程式設計式

Step11:選擇連接配接器的正常,選擇附加庫目錄,将安裝的位址寫入,使得連接配接器可以找到Irvine32.lib這個庫檔案,之後點選确定。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

Step12:選擇連接配接器的調試,将生成調試資訊選擇為“優化以便于調試(/DEBUG)”

VS2015中運作32位彙程式設計式

Step13:選擇連接配接器的進階,選擇映像具有安全異常處理程式,選擇否。

VS2015中運作32位彙程式設計式

Step 14:Ok,配置完成,試試調用Irvine32頭檔案是否可行。下面給出一個在Github上托管的代碼的執行個體:

以同樣的方式生成解決方案,這個時候輸出正确!可以進行愉快的32位彙編喽!

VS2015中運作32位彙程式設計式

注意調試斷點的使用!紅點就是斷點。

VS2015中運作32位彙程式設計式

調試時調出記憶體管理,利用反彙編進行記憶體内容的檢視,當然也可以把寄存器也調出來:

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

這個是個人調整之後的調試界面,左下角的寄存器部分也已進行多種選項,顯示标志寄存器,左上角有個反彙編,

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

将裡邊的記憶體位址指派,在記憶體檢視部分可以看到記憶體的資訊。

VS2015中運作32位彙程式設計式
VS2015中運作32位彙程式設計式

點選逐漸運作,可以看到寄存器的變化,以及标志寄存器,紅色代表改變。

VS2015中運作32位彙程式設計式

程式編譯成功之後生成的清單檔案和編寫的檔案在同一目錄下。

VS2015中運作32位彙程式設計式

對清單檔案的部分截屏:

VS2015中運作32位彙程式設計式

最後還差一個變色包,讓彙編環境關鍵字變色。

MASM syntax highlighting

When a text editor uses syntax highlighting, language keywords, strings, and other elements appear in different colors. Visual Studio highlights MASM reserved words and strings, as shown in the following example:

VS2015中運作32位彙程式設計式

This won’t happen automatically, but you can create a syntax definition file named Usertype.dat that contains MASM keywords. Then when Visual Studio starts, it reads the syntax file and highlights MASM keywords.

There is an interesting third party Visual Studio 2015 extension named Asm-Dude, featuring a rich set of syntax highlighting and code completion features. You can download it from GitHub.

If you decide to use Visual Studio’s built-in MASM syntax highlighter, here are the required steps to set it up:

1) Download the Usertype.dat file (enclosed in a ZIP file) given here to a folder in which you have read/write permissions. If you are using Windows 7, download to My Documents, or C:\temp, or any folder that doesn’t have security restrictions.

2) Copy Usertype.dat to the C:\Program Files\Microsoft Visual Studio 12.x\Common7\IDE folder. In 64-bit windows, Program Files will be named Program Files(x86).

Windows will display a verification dialog before copying the file.

3) Open Visual Studio, select Options from the Tools menu, select Text Editor, and select File Extension. On the right side of the dialog (shown below), enter asm as the extension, select Microsoft Visual C++ from the Editor list, and click the Add button. Click the OK button to save your changes.

VS2015中運作32位彙程式設計式

Close Visual Studio and restart it. Open your project and display an ASM file. You should see syntax highlighting in the editor. There is a glitch in the highlighting–assembly language comment line starts start with a semicolon, which C++ doesn’t recognize. But this is a simple workaround: add an extra // right after the semicolon, like this, which will cause the comments to appear in their usual green color。

或者給出VS2015變色安裝包,不過安裝完之後需要在VS2015中更新一下,重新開機才有效果。