Try   HackMD

微算機 Chaper 4 Programming Languages

tags: microcontroller

Computer Programming Languages

  • 有分三種:
    1. Machine language
      • 可能是binary或hexadecimal op-codes。
      • 基本上不太可能直接使用這種語言來編程,因為全部都是數字。
    2. Assembly language
    3. High-level language
  • assembly和high-level languaue被稱為source code,分別會被assemblercompiler/interpreter翻譯成machine language program,而machine language programs are known as object codes

Machine Languaue

  • 每個microcontroller有自己獨特的machine language instruction。當然也有不同的標準去遵循。PIC18F使用算了

Assembly Language

  • 每一行包含4種fields(?):
    1. Label field
    2. Instruction, mnemonic,or op-code field
    3. Operand field
    4. Comment field
Label Mnemonic Operand Comment
init: MOVLW 0x01 ;Move 1 into accumulator

請自行腦補去掉直線

  • 藉由assembler轉為machine code,透過每一行轉成ASCII,然後再翻譯成binary op-codes。
  • 優點:如果直接寫machine languaue,你要自己去算data storage address或targets for jumps or calls address,但用assembly language就叫電腦幫你算就好。

Assembler

  • Assembler分類:
    1. One-Pass Assembler:在assemble的過程中只scan一次完整的程式碼,所以所有的JUMP instruction都只能往程式碼的後跳,不能往程式碼的前面跳。
    2. Two-Pass Assembler:會scan兩次code,在第一次,assembler做出一個symbol table,第二次才把assembly language轉成machine code。
    3. Macroassembler:讓programmer可以使用macro,效果等同於C語言的 ( function ? ) 。
  • assembler對於field的劃分定義:
    • Space are used between fields.
    • Commas(,) are used between address in an operand field.
    • Semicolon(;) is used before a comment.
    • colon(:) or no delimiter is used after a label.

Assembler Derectives or Pseudoinstructions

  • ORIGIN(ORG):告訴microcontroller你要把這個program放在memory的哪裡。ORG讓assembler把新的值賦予address counter

address counter功能類似program counter

  • Equate(EQU):把operand field的值assign到label field。

我不太懂這啥意思,把值丟到label field幹嘛?

好像就只是一個類似macro的感覺,可以把label用數字代替??(還沒實驗過)

  • Define Byte(DB):會把 8-bit 數值存進原本ORG的記憶體位址裡面。
  • Define Word(DW):會把 16-bit 數值存進原本ORG的記憶體位址裡面(也可以說是在 address counter 裡面的地址)

pic18f 是使用 little endian 所以會把比較低的 byte 先存。
例如:

ORG 0x100 DW 0x4C8F

這樣 4C 會存在 0x101 而 8F 會存在 0x100。

Typical Instruction Set

  • 加減法
    1. 做無號數的加法,如果是 0xFF + 0xFF ,結果會是 0xFE 加上 1bit 的 carry。
    2. 做有號數的加法,如果是 0xFF (-1) + 0xFF (-1) ,結果會是 0xFE (-2),而會多的那個 carry 必須要被捨棄掉。
  • 乘法
    1. 無號數的乘法就直接乘就好了。
    2. 有號數的乘法順序如下:
      1. 把兩個數字的 MSB (signed bit) 拿出來,分別為 P Q 。
      2. 如果該數字的 MSB 是 1 ,要做 2's complement。
      3. 把兩修改過後的數字相乘。
      4. 把 signed bit 加上
        PQ
        的結果。
  • 除法
    1. 無號數的除法也直接做就好了。
    2. 有號數的除法就跟有號數的乘法一樣,自己看。
    3. 如果除下來有餘數,要注意這些事:
      • remainder = dividend - quotient x divisor 。
      • 餘數的符號會與 dividend 一樣。
  • Logic Instruction
    1. 就你知道的那樣(AND OR XOR NOT之類的),沒啥好說的。
  • Shift and Rotate Instructions
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

注意如果是負數做 arithmetic shift 左邊會補 1 。

  • 當做 arighmetic shift left 時,如果做之前的 MSB 和做之後的不一樣, overflow 的 bit 會設為 1 。

Typical Addressing Modes

  • 當 instruction/operand 有數字,稱為 immdiate mode instruction 。
    ex : ADDLW 3

  • 當 instruction/operand 都是記憶體位址或 registers 時,稱為 absolute/direct addressing mode 。
    ex : MOVWF 0x20

  • 有些 branch 是 16-bit ,前 8 是 op-code 而後 8 是當符合狀況時,要加到 program memory 的值,後 8 bit 是 signed 的 range from -128 ~ 127。
    ex : MOVWF INDF0