Rissun木鴨
    • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 目錄(contents) ### [1.1](#1.1) - [**Context of Assembly Language**](#Context-of-Assembly-Language) - [**what is assembly**](#1.1.1-What-is-Assembly-Language?) - [**Assembly Language Application**](#1.1.2-Assembly-Language-Applications) - [**Machine Language**](#1.1.3-Machine-Language) ### [1.2](#1.2) - [**Data Representation**](#Data-Representation) - [**Binary Numbers**](#1.2.1-Binary-Number) - [**Hexadecimal Numbers**](#1.2.3-Hexadecimal-Numbers) - [**Signed Numbers**](#1.2.4-Signed-Numbers) - [**Character Storage**](#1.2.5-Character-Sotrage) ### [1.3 Introducing Assembly Language](#1.3-Introducing-Assembly-Language) - [**Assembly Language Instructions**](#Assembly-Language-Instructions) - [**A Sample Debug Program**](#1.3.2-A-Sample-Debug-Program) # 1.1 ### Context of Assembly Language ![image](https://hackmd.io/_uploads/Hy-a5Ox-kg.png) &emsp;&emsp;**組合語言解釋了電腦運作的原理,它告訴了我們硬體和軟體如何藉由程式語言相互溝通和運作,這也會帶給我們在使用高階語言時有更深入的運用, 在高階語言每個單獨的語句會被展開為多條機器指令處理(就像圖1-1,),在組合語言和機器層面上,使用者創建的指令是直接由計算機的處理器解讀的,組合語言是一種低階語言。(這意味著它不需要中間的編譯步驟!)**<br> &emsp;&emsp;**組合語言經常被運用在和作業系統通信的過程或是直接訪問電腦硬體,其次是最佳化程序的執行效率和時間,組合語言最好同時和作業系統以及計算機組織一同學習。**<br> ### 1.1.1 What is Assembly Language? &emsp;&emsp;**組合語言是一個機器特定的程式語言,其語句與計算機的本機機器語言之間存在一對一的對應關係<span style="color:red">(意味著每個架構甚至每個機型的組合語言都會可能存在差異)</span>,有許多不同類型的組合語言,每個都特定為了某個處理器或是處理器系列,這是因為組合語言被設計來一一對應機器指令,IBM-PC 組合語言指的是被多個不同的 Intel 8086 微處理器識別的指令。**<br> &emsp;&emsp;**What Is an Assembler? 一個組譯器作用是轉換源代碼從組合語言到機器語言,組譯器可以選擇性地生成一個源代碼清單文件,其中包含行號、內存地址、源代碼語句,以及程序中使用的符號和變數的交叉引用清單, 一個配套程式叫做<span style="color:red">連結器(linker)</span>,由組譯器創建為單一可執行程序會被連結器給結合起來,第三個程序叫做偵錯工具,提供編程人員一個方法去追蹤程式的執行。我主要在Ubuntu + nasm結合的練習環境。**<br> &emsp;&emsp;**組合語言被稱作低階語言,因為它在結構和運作上都更接近機器語言,每一個組合語言都和機器指令一一對應,相比之下高階語言單一的語句可以在多種機器上運作** **為何要學習組合語言?我個人是因為我想更了解電腦。**![image](https://hackmd.io/_uploads/S1uo9_xbkg.png) **你可能也想學習組合語言(asm),因為它的實用性,舉例來說,直接與作業系統溝通是必須得,就像是,一個遊戲程式必須直接訪問顯示裝置和音效卡,Visual Basic 程式可以呼叫用組合語言(asm)編寫的 DLL 程序,以加速程式在關鍵代碼區域的執行。**<br> **也許更重要的是,組合語言是強大的工具當我們學習計算機組織,作業系統時,數據表示以及硬體裝置時,近年越來越多的大學也開設這些主題的課程,這些知識都將可以幫助我們在處理異常情況時具有明顯優勢。** ### 1.1.2 Assembly Language Applications &emsp;&emsp;**這本書第一個呈現的 asm_programs可能看起來很微不足道,你不能相信需要龐大的工作量才能實現一個相對簡單的任務,組合語言在細節上需要非常留意,在早期程式設計,大多數的應用程序都被寫運用組合語言編寫,因為必須要儲存在很小的記憶體中,同時電腦跑得非常慢,當電腦變得更快更強大,程序可以更長更複雜了,這時需要使用高階語言,他們包含了很多的結構,容器去幫助程式設計師,更近期的,物件導向語言像是C++使得編寫包含數百萬行原始程式碼的複雜程式成為可能。**<br> &emsp;&emsp;**今天我們很難看到一個應用程式是完全以組合語言編寫的,因為編寫和維護它太花時間了,相反地,組合語言被運用在最佳化部份應用程式的程式區段以及訪問電腦硬體,組合語言還被用來編寫嵌入式系統程式,它們被儲存在硬體裝置的程式唯讀記憶體(PROM)之中**<br> &emsp;&emsp;**C語言具備獨特的能力提供折衷於HLL的結構和ASM的細節,允許直接訪問硬體,但完全地不能移植,大多數的C編譯器有能力產出組合語言源代碼,程式設計師可以在組裝成可執行程式碼之前進行客製化和改進。**<br> &emsp;&emsp;**大多數的程式設計師會使用C語言結合組合語言在嵌入式系統應用, 像是電腦卡帶遊戲, 微控制器, 工業機器人, 安全系統。**<br> &emsp;&emsp;**組合語言子程式,程式設計師常常會把組合語言嵌入在高級語言的子程式中,有時候會被叫做端口子程式,因為它直接提供了一個方便的方式去訪問電腦硬體**<br> &emsp;&emsp;**多平台編程,一個組合語言的弱點就是不能輕易的被重新編譯和執行在不同地電腦上,為了改善這個情況,交叉組譯器被誕生了,它可以將組合語言轉換為另一個處理器系列的組合語言源代碼,當然,所有語句在原始程式關於特定裝置和作業系統的程式碼仍然需要被重寫。**<br> ### 1.1.3 Machine Language &emsp;&emsp;**電腦不能直接解釋組合語言,但是它了解機器語言,機器語言被組成由數字,它可以被處理器給處理,通常處理器內建了稱為微程式的解釋器,它可以解釋和轉換機器指令為硬體訊號。**<br> &emsp;&emsp;**機器語言內建原始的語句,去表現普通的任務像是,資料傳輸,以8086舉例:** ``` asm=1 //push the AX register on the stack push ax ; //push all general registers on the stack pusha ; ``` # 1.2 ### Data Representation &emsp;&emsp;**因為我們處理電腦在機器層面,它必須要檢查記憶體和暫存器的內容,電腦被以數位電路建置所以它每一個單元有兩個狀態,on and off 當資料儲存在揮發性記憶體,需要電荷來維持記憶狀態。**<br> ### 1.2.1 Binary Number &emsp;&emsp;**一個電腦的指令和資料被儲存在記憶體藉由收集電荷,要表達這些實體行為我們需要運用數字去表示on and off, true and false 基於二進位數,通常 1代表5伏特,0代表0.5伏特,同時,電腦事實上充滿了開關進行複雜的邏輯轉換**<br> ![image](https://hackmd.io/_uploads/Syc7UPeZ1e.png) &emsp;&emsp;**Bits, Bytes, Words, and Doublewords,每一個位元為bits, 8個為byte, 一個word為16bits, Doubleword為32bits,事實上這也是高級程式語言最底層的本質,回顧和運用過去知道的位元運算也很重要。**<br> ### 1.2.3 Hexadecimal Numbers &emsp;&emsp;**16進位的運算。** ![image](https://hackmd.io/_uploads/Bkt6PDeZkl.png) ![image](https://hackmd.io/_uploads/BkF_wDebkg.png) ### 1.2.4 Signed Numbers ![image](https://hackmd.io/_uploads/H1TlYDx-1x.png) ![image](https://hackmd.io/_uploads/Hyx7YweZ1e.png) &emsp;&emsp;**2的補數運算,一個數字取反後加上1**<br> ![image](https://hackmd.io/_uploads/SkZk5veWJg.png) &emsp;&emsp;**在組合語言中,我們要實現+16和-23相加,可以這樣做。** ``` asm=1 mov ax, 16; add ax,-23; ``` &emsp;&emsp;**在2進位中,結果為-7,但是它會被直譯為無號數249。** ### 1.2.5 Character Sotrage &emsp;&emsp;**電腦只儲存二進位數,那如何表示字元?電腦必須使用字元編碼方法轉換數字要信件,反之亦然,最廣為人知的微電腦系統是ASCII code在ASCII每個數字都會對應一個字元,標準的 ASCII只有7位元範圍0-127,0-31為控制碼給印表機,他們不會產生可見的字元,之後陸續被個公司擴展。**<br> &emsp;&emsp;**一個字串字元被代表在記憶體中作為一連串包含字元的位元組,例如:ABC123被表示為41h, 42h, 43h, 30h, 31h, 32h**<br> # 1.3 Introducing Assembly Language ### Assembly Language Instructions &emsp;&emsp;**雖然我們已經知道直接使用機器碼使用數字,但是組合語言使的這些變得更簡單,因為組合語言是一個以符號來表示術訊號的機器指令, 在它最間單的形式中,它由助記符組成,一個簡短的字母代碼,字面意思是「幫助記憶」記住 CPU 指令, 助記符號還能跟隨一系列的操作符這裡有一些範例:** ``` asm=1 clc ; just a mnemonic inc ax ; single operand mov ax, bx; two operands 10 (immediate operand) count (variable or memory operand) AX (register oprand) [0200] (memory ;ocation) ``` ### 1.3.2 A Sample Debug Program &emsp;&emsp;**讓我們開始寫一個短短組合語言程式,我們組合和運行程式使用偵錯。** ```asm=1 mov ax, 5 add ax, 10 add ax, 20 mov [0120],ax int 20 ``` &emsp;&emsp;**mov指令告訴CPU移動並複製資料,上面的程式做的事情也是,移動5到ax,加上10到ax,加上20到ax,寫入記憶體中位置 0120 的變數, 如果你訪問偵錯器,你也會看到類似的資訊。** ### 接續下一個章節[硬體和軟體組織 Assembly Language_Hardware and Software Architecture](/hjSxOgh6Rq6KDUEuEvg3DA)

    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