Miku
    • 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
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • 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

      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.
      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
    • Engagement control
    • 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
Engagement control 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
Engagement control Commenting, Suggest edit, Emoji Reply
  • 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

    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.
    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
    # Chap 19 ## Microprocessor * Bộ vi xử lí (microprocessor) là bộ phận hợp nhất tất cả các thành phần của bộ xử lí trung tâm (CPU) * Bộ vi xử lí 8-bit có thể gửi đi 256 lệnh khác nhau (instructions) --- ## Bộ vi xử lí Intel 8080 --- ### Lệnh - Instructions * Một bộ vi xử lí có thể có các lệnh như Load, Store, Add, Subtract, ... * Theo chap 17, mỗi lệnh thường có 3 bytes, byte đầu tiên là opcode (mã lệnh), 2 bytes tiếp theo là địa chỉ của ô nhớ ![](https://i.imgur.com/DuUfY4z.png) * Trong trường hợp bộ vi xử lí, các lệnh có thể được biểu diễn : ![](https://i.imgur.com/u8IeU6E.png) Trong đó A là thanh tổng (accumulator), và [aaaa] là địa chỉ ô chứa thông tin. Lệnh ````STA [aaaa] , A```` có nghĩa lấy từ ô chứa [aaaa] và nhập vào thanh tổng (accumulator) ### Thanh ghi - Register * Ngoài thanh tổng, bộ vi xử lí có thêm 6 thanh ghi phụ, tên B, C, D, E, H, L * Cả 7 thanh đều cho phép chứa các giá trị 8-bit, cho phép lưu byte từ bộ nhớ, hay xuất ra bộ nhớ. * Nhưng thanh tổng A đặc biệt hơn, ví dụ khi thực hiện phép tính giữa 2 số 8-bit, kết quả chỉ hiển thị duy nhất trên thanh A. * Có thể ghép 2 thanh ghi với nhau để tạo địa chỉ 16-bit, ví dụ ghép thanh H và L để tạo thanh địa chỉ 16-bit [HL] ### Lệnh với các thanh ghi - Có nhiều lệnh là lệnh gán, ví dụ LDA, MOV, MVI nhưng cách thực hiện gán lại khác nhau - **direct addressing**: gán trực tiếp. Ví dụ ````LDA A, [aaaa]```` - **indexed addressing**: lệnh gán địa chỉ. Ví dụ ````MOV A, [HL]```` . - **immediate addressing**: lệnh gán lập tức. Ví dụ sau lệnh ````MVI E, 37h````, thanh ghi E lập tức mang giá trị 37h - Ví dụ về lệnh ````MOV```` 8-bit : ````01dddsss````. Trong đó ````01```` là mã của lệnh ````MOV````, ````ddd```` là mã 3 bits chỉ địa chỉ ô chứa đích, và ````sss```` chỉ địa chỉ ô lấy giá trị. - ![](https://i.imgur.com/f1BzeIO.png) - Lệnh ````MOV L, E```` sẽ có mã opcode là````01101011````, hay ````6Bh```` ### Flag Ngoài thanh ghi, ta có các Flags để rõ hơn về tính trạng của kết quả, ví dụ: - Sign flag (dấu) là 1 nếu kết quả của phép tính cuối cùng là âm - Zero flag (không) được về 1 nếu kết quả phép tính cuối cùng bằng 0 - Parity flag đạt giá trị 1 khi số số bit ````1```` trong kết quả là chẵn, 0 nếu ngược lại. Được dùng để kiểm tra độ chính xác kết quả, không phổ biến trong Intel 8080. - Carry flag (nhớ) tăng 1 khi phép cộng ````ADD```` ,````ADC```` có nhớ; khi phép trừ ````SUB```` hoặc ````SBB```` không có nhớ - Axiliary flag (phụ trợ) bằng 1 nếu phép tính từ hàng đơn vị lên hàng trăm/chục có nhớ - Một số lệnh ảnh hưởng đến Flag, một số lệnh không. Ở đây sẽ chỉ đề cập một số loại lệnh: * ````CMP A, B```` : lệnh trừ giá trị ở hai thanh ghi A, B, nhưng kết quả bị hủy ngay sau đó. Thế có điều gì xảy ra? Zero Flag sẽ thành 1 nếu ````A = B````, Carry Flag nhận giá trị tương tự nếu ````A < B```` * Lệnh tăng 1(````INR````) và giảm 1(````DCR````) ảnh hưởng tất cả các flag trừ carry flag ![](https://i.imgur.com/J7U8YWU.png) * Lệnh dịch bit trái phải ````RLC```` và ````RRC```` ![](https://i.imgur.com/prAuhXw.png) Giả sử thanh tổng đang chứa giá trị ````110100111````, lệnh ````RLC```` dịch chuyển các bit từ phải sang trái, được kết quả ````01001111````, lưu 1 vào Carry Flag. Trong khi đó, ````RRC```` cho kết quả ````11010011````, kết thúc với giá trị Carry Flag được đẩy lên 1. ### Ngăn xếp (stack) * Khi các thanh ghi cần làm việc với giá trị khác nhưng cần giữ lại giá trị hiện tại để xử lí trong tương lai, ta cần chứa nó ở đâu đó, đó chính là stack. ##### - Nguyên lí của stack * Như chồng sách: vào đầu ra cuối. Quyển sách được chồng vào sớm nhất được lấy cuối cùng. * Hai thao tác chính: * PUSH : Đẩy một giá trị vào stack * POP : Lấy giá trị đầu tiên ra khỏi stack * Ví dụ : ```` PUSH A PUSH B PUSH C ```` để lấy các giá trị ra, lấy theo giá trị ngược lại: ```` POP C POP B POP A ```` ##### - Lập trình stack * Cấu trúc : Trên bộ vi xử lí có 1 thanh ghi 16-bit tên Stack Pointer, mang địa chỉ của một đoạn trống trên RAM. Đoạn trống trên RAM này là stack ta đang cần. * Ví dụ về hoạt động : Giả sử ````Stack Pointer```` đang có giá trị 8000h. Và chúng ta cần ````PUSH BC````, máy tính thực hiện các bước: * Giảm ````Stack Pointer```` còn 7FFFh * Lưu giá trị hiện tại của thanh B vào địa chỉ của ````Stack Pointer````, hay ````7FFFh```` * Giảm ````Stack Pointer```` xuống ````7FFEh```` * Lưu giá trị hiện tại của thanh C vào địa chỉ ````7FFEh```` Tương tự với ````POP CB```` * Lấy giá trị ở ô ````Stack Pointer 7FFEh```` * Tăng ````Stack Pointer```` lên ````7FFFh```` * Lấy giá trị ở ô ````7FFFh```` * Tăng ````Stack Pointer```` lên ````8000h```` #### Các vấn đề của stack * overflow : tràn ngăn xếp, xảy ra khi ngăn xếp hết không gian chứa các giá trị và phải nhận thêm quá nhiều lệnh ````PUSH```` * underflow : khi ngăn xếp đã trống và phải nhận thêm quá nhiều lệnh ````POP```` ## Little-edian và Big-edian Cùng xét ví dụ để hiểu hơn về sự khác biệt của 2 loại bộ vi xử lí Intel 8080 (little-edian) và Motorola 6800 (big-edian). Giả sử có một lệnh gồm 3 bytes như sau cho Intel 8080: ![](https://i.imgur.com/oKXORB4.png) và lệnh tương tự cho Motorola 6800 : ![](https://i.imgur.com/bWGwKlZ.png) Với ````3Ah```` là opcode của ````LAD```` cho Intel 8080, ````B6h```` là opcode của ````LAD```` cho Motorola 6080. Ở trường hợp Intel 8080, bộ vi này sẽ đọc vào thanh tổng giá trị ở địa chỉ ````347Bh```` . Ngược lại, bộ vi 6800 đọc giá trị ở địa chỉ ````7B34h````. Đây chính là điểm khác biệt lớn nhất giữa little-edian và big-edian. Little-edian đọc byte ở vị trí thấp nhất và nhỏ nhất đầu tiên. Trái lại, big-edian đọc byte ở vị trí cao nhất, lớn nhất trước và sau đó tới byte ở vị trí nhỏ hơn. Điểm khác nhau rõ rệt trên đã tạo ra một vài khó khăn trong việc trao đổi thông tin giữa hai hệ thống có hai bộ vi xử lí khác nhau này.

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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