NiuKaLi
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # MASM開發環境設置 - 使用 Visual Studio 與 Irvine 範例 ###### tags: `教學` `筆記` `開發環境` `112-1` `112-1 組合語言與系統程式` ![24.png](https://hackmd.io/_uploads/ByUq089Xp.png) ## 目錄 [TOC] ## 簡介 本教學配合 *Assembly Language for x86 Processors* 使用MASM, 配合該書作者 Irvine 的範例與函式庫,使用作者提供的專案設定, 介紹 Windows 平台上使用 Visual Studio 簡單快速開始編寫MSAM的方法。 :::success 本文基本設置內容大致與作者自行編寫的教學大同小異,也可直接參考: [*Getting Started with MASM and Visual Studio 2022*](https://www.asmirvine.com/gettingStartedVS2022/index.htm) 也推薦參考[作者網站](https://www.asmirvine.com/)有更多資源 *(作者也有Visual Studio 2019版本的教學)* ::: :::info 本文整理自修習組合語言課程時,為了複習與寫作業自行摸索的步驟 如果有任何錯誤或建議,請隨時留言提出 ::: :::warning 請注意,本文撰寫於2023/11,內容未來可能不再適用 ::: ## 開發環境基本設置 ### 系統需求 * Windows 10/11 64bit * 4 GB 記憶體 * 20GB儲存空間 (建議) ### 下載與安裝 Visual Studio 2022 1. 進入 [**Visual Studio 官網**](https://visualstudio.microsoft.com/zh-hant/vs/whatsnew/) 2. 選擇下載 **Community 2022** 版本 *(Community為免費版)* 3. 下載完畢後開啟 `VisualStudioSetup.exe` 4. 僅勾選 **`使用C++的桌面開發`** 後點選安裝 ![01.png](https://hackmd.io/_uploads/ry_7w79Qa.png) 5. 等待下載與安裝完畢後重新開機 ### 下載與放置 Irvine 範例、函式庫與專案檔案 1. 前往 [**Irvine 的專案 GitHub**](https://github.com/surferkip/asmbook) 2. 下載 [`Irvine.zip`](https://github.com/surferkip/asmbook/blob/main/Irvine.zip)、[`Project32_VS2022.zip`](https://github.com/surferkip/asmbook/blob/main/Project32_VS2022.zip)與[`Project64_VS2022.zip`](https://github.com/surferkip/asmbook/blob/main/Project64_VS2022.zip) 三個壓縮檔 :::info 可以分別點選上述檔案連結後點選右上角`Download raw file`下載 **(建議)** 或在專案頁面點選右上角`Code > Download ZIP`下載整個專案再取出這三個檔案 ::: 3. 將三個壓縮檔**解壓縮至此** *(壓縮檔內已經是資料夾,外面不用再包一層)* 4. 將`Irvine`資料夾移到`C:\`下,看起來像是這樣 ![02.png](https://hackmd.io/_uploads/ryRHvX5Xa.png) :::info `Irvine`資料夾內的`Examples`包含 *Assembly Language for x86 Processors* 書中範例 ::: :::warning 除非會自行更改專案設定,否則請不要改變路徑 ::: 5. 將`Project32_VS2022`、`Project64_VS2022`資料夾移到自己喜歡的目錄,例如`C:\Code\` ### 設置 Visual Studio 組譯與除錯 1. 開啟 Visual Studio 2022 2. 若提示登入帳號,可以選擇暫時略過 *(登入帳號未來可在多裝置上同步設定)* 3. 選擇佈景主題後啟動 4. 選擇 **`開啟專案或解決方案`** 5. 選擇先前`Project32_VS2022`資料夾內的`Project.sln`,例如`C:\Code\Project32_VS2022\Project.sln` * **(重要)** 不是`Project.vcxproj`是`Project.sln`(有可能沒顯示副檔名`Project`) 6. 點開右側方案總管下的`AddTwo.asm` ![03.png](https://hackmd.io/_uploads/BytNR7c76.png) 畫面應該會如下 ![04.png](https://hackmd.io/_uploads/S1ztAQ5Xa.png) 其中`AddTwo.asm`程式碼如下 ```= ; AddTwo.asm - adds two 32-bit integers. ; Chapter 3 example .386 .model flat,stdcall .stack 4096 ExitProcess proto,dwExitCode:dword .code main proc mov eax,5 add eax,6 invoke ExitProcess,0 main endp end main ``` 7. 在 ```=11 mov eax, 5 ``` 該行行號左側點一下,設置一個中斷點 ![05.png](https://hackmd.io/_uploads/HydHxE97a.png) 8. 按下上方中間的 **`本機Windows偵錯工具`** 或快捷鍵 **`F5`** 開始偵錯 此時應該會跳出一個小黑窗,而程式此時暫停於中斷點如下 ![06.png](https://hackmd.io/_uploads/H1jVGVcma.png) 9. 點選 **`偵錯 > 視窗 > 暫存器`** ![07.png](https://hackmd.io/_uploads/B1QnfV9mT.png) 會出現一個暫存器小窗格,可以窗格上側將其放置至喜歡的位置 ![08.png](https://hackmd.io/_uploads/r1HwXN9Qa.png) 例如在右下方 ![09.png](https://hackmd.io/_uploads/HJt1VV576.png) 10. 在暫存器小窗格上方按右鍵,勾選 **`旗標`** ![10.png](https://hackmd.io/_uploads/rk8DEVqXT.png) 暫存器窗格將多顯示旗標 ![11.png](https://hackmd.io/_uploads/ByAOVV5Qa.png) :::info 如何透過 **`EFL`***(EFLAGS)* 暫存器觀察常見旗標,請參考[EFLAGS對應位元](https://en.wikipedia.org/wiki/FLAGS_register) ::: 11. 按一下上方中間的 **`逐步執行(不進入函式)`** 或快捷鍵 **`F10`** 使程式執行至下一行 ![12.png](https://hackmd.io/_uploads/HyVXIVcm6.png) 此時執行了這行程式 ```=11 mov eax, 5 ``` 觀察程式碼處,箭頭變為指向下一行開頭 ![13.png](https://hackmd.io/_uploads/By9m8NcXT.png) 而暫存器窗格的`EAX`暫存器變為紅色,值變為`00000005` *(`EIP`暫存器也會改變)* ![14.png](https://hackmd.io/_uploads/HkTQUE9Xp.png) 12. 重複上個步驟,再按一下逐步執行 此時執行了這行程式 ```=+ add eax,6 ``` 箭頭往下移動,而暫存器窗格的`EAX`暫存器值變為`0000000B` *(11)* ![15.png](https://hackmd.io/_uploads/S1cVTBqQ6.png) 13. 重複上個步驟,再按一下逐步執行,退出偵錯模式 至此即完成開發環境的基本設置以及偵錯示範 14. 若要編寫程式,可以直接開始修改`AddTwo.asm` :::info Visual Studio 編譯、組譯與偵錯是**以方案而非單一檔案**為單位 若要在 Visual Studio 中執行或偵錯其他檔案,在右側方案總管的`AddTwo.asm`上點右鍵,選擇 **`從專案移除`** *(這不會將檔案刪除,只是從專案中移除)* ![16.png](https://hackmd.io/_uploads/B1EpCS5mp.png) 在`Project`上 *(**不是**`方案'Project'(1/1個專案)`)*點右鍵, * 若要建立新檔案,選擇 **`加入 > 新建項目`**,輸入檔名並將副檔名改為 **`.asm`** * 若要加入已存在的檔案 *(例如:Irvine的課本範例檔)* ,選擇 **`加入 > 現有項目`**,選擇檔案 ::: ## Visual Studio 進階設置 (選擇性) 以下步驟皆非必要,但可以改善開發體驗,可自行考量是否操作 ### 調整編輯器字體與大小 1. 點選上方 **`工具 > 選項`** 2. 選擇 **`環境 > 字型和色彩`** 3. 將文字編輯器的字型換為其他合適字體 * 最好選擇等寬字體 *(以粗體表示)* * 推薦選擇內建的 **`Consolas`** 4. 將大小調整為合適大小 * 也可以在編寫時按住 **`Ctrl + 滾輪`** 調整大小 5. 按確定完成調整 ![19.png](https://hackmd.io/_uploads/BkINDIqmT.png) |**調整前**| |:-:| |![17.png](https://hackmd.io/_uploads/ByxsL85Qp.png)| |**調整後**| |![18.png](https://hackmd.io/_uploads/BJ7oUI9Qp.png)| :::info Visual Studio 大多數文字的字體與顏色大小等都能修改,可自行摸索 ::: ### 加入語法突顯(Syntax highlighting) 使用 **AsmDude2** 延伸模組,其[GitHub頁面](https://github.com/HJLebbink/asm-dude)有詳細說明 1. 點選上方 **`延伸模組 > 管理延伸模組`** 2. 選擇 **`線上`** 後在右上方搜尋輸入 `AsmDude2` 3. 點選下載 ![20.png](https://hackmd.io/_uploads/HkMowU5Xp.png) 4. 關閉 Visual Studio 5. 點選 Modify ![21.png](https://hackmd.io/_uploads/BytrqIcQp.png) 6. 安裝完畢後重新開啟 Visual Studio 2022 |**調整前**| |:-:| |![18.png](https://hackmd.io/_uploads/BJ7oUI9Qp.png)| |**調整後**| |![22.png](https://hackmd.io/_uploads/BJhrq89Xp.png)| :::info 可以在 **`工具 > 選項`** 中的 **`AsmDude2`** 對顏色等進行進一步修改 ::: :::warning 目前 **AsmDude2** 還在開發中,有時會有小錯誤,例如這個錯誤訊息 ![23.png](https://hackmd.io/_uploads/HyuM985Xp.png) :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully