--- tags : 微算機與組合語言 --- ###### 國立臺北大學資工系江宥旻 Assembly Language Programming === Software:The microcomputer program --- Hierarchy of Programming Language + High Level Language ```c int a = 0, b = 0; a = 2; b = 5; a = a * a + 3 * b + 6; printf("The Integer: %d", a); ``` + Low Level Language (Assembly Language) ```asm MOV ax, 2 MOV cx, ax MOV bx, 5 mul ax, cx add ax, bx ``` + Machine Language ```ml 1001000100011110 0111001010010101 1100110101110101 1110001111011010 0101110101010101 ``` + CPU Execution + Fetch Instruction + Decoding + Retrieve Data + Execution + Write Back Native language is machine language + A program written in machine language is often called machine code + Is encoded using 0, 1 + An instruction can be divided into two parts + Operation code (opcode)運算子 + Operands運算元 + Each opcode is assigned a unique letter combination called a `mnemonic(助憶碼)` + MOV AX, BX + Move bx register to ax register + Physically cpu copies the value of BX into AX + BX is source operand and AX is destination operand --- Simple program that can be translated into machine code using MASM 6.0 MASM (將組合語言轉成機器碼) + Macro Assembler + Translating mnemonic code into machine code (object code) ```asm ;***************** ;* SIMPLE.ASM ;***************** .MODEL small .DATA CR EQU 0DH ;EQU等於用來定義常數DB define byte LF EQU 0AH ONE DB 01h MESSAGE DB 'smile’, CR, LF, '$' .CODE start: MOV ax, @data ;DS 移到資料區開始地 MOV ds, ax MOV ah, 02h MOV dl, ONE int 21h MOV ah,09h MOV dx,OFFSET MESSAGE ;DX 移到字串的位址 int 21h MOV ah,4ch int 21h .STACK END start ``` :::info 一個程式會有三個區 + .DATA 資料區 + .CODE 程式區 + .STACK 堆疊區 ::: > 組譯程式把組合語言轉換成機器碼 > OD、OA(CR、LF):carriage return and line feed (游標移到最前並換行) ![app-os.png](https://i.imgur.com/4q3Etmx.png =400x) [INT 21H 指令說明及使用方法](https://www.796t.com/content/1548310330.html) int 21h是一種中斷程式(interrupt)是OS提供給APP使用的,能夠讓APP進到OS,讓OS做APP想做的事。 Advantages of assembly program than high-level program + Small code size `程式小` + Useful for limited memory space + Short execution time `執行快` + Useful for real-time application or time-sensitive application + Low level control `I/O控制,高階語言很難做到` + Device service routines OS的程式 + C:95% + 組合語言:5% Assembly language program development on the PC --- ![PC-asm](https://i.imgur.com/TeXNaz6.jpg =300x) The Instruction set --- `117 basic instructions` for the 8088/8086 + Data transfer + Mov, push, pop, XCHG(exchange), in, out, ... > XCHG:register直接交換,不用多設變數 + Arithmetic + Add, adc, inc, sub, sbb, dec, cmp, ... > inc:++,sbb:-- + Logic + And, or, not, shift, rotate, test, xor, ... + String manipulation + Rep, movs, cmps, ... > Rep:重複 + Control transfer + Call, ret, jmp, conditional jump, loop, conditional loop, int, ... + Processor control + Clc, cmc, stc, cli, (set and reset flag), ... The MOV instruction --- ### MOV D, S > D:(Destination)目的,S:(Source)來源。來源的值`copy`一份到目的 ![D-S](https://i.imgur.com/XZycMUG.jpg =400x) + Memory + `[]`:記憶體,內部的值很像arr[index]的概念。 + Accumulator (AX) + Register + Seg-reg (區段暫存器) + Reg16 (DX) + Immediate (立即值) Example: 1. MOV AL, '0' + AL:register + '0':immediate 2. MOV [400], AL + []:memory + AL:register 3. MOV BL, [400] + BL:register + []:memory 4. MOV [402], 02 + []:memory + 02:immediate 5. ADD AL, BL + AL <- AL + BL 6. SUB AL, CL + AL <- AL - CL ![mov](https://i.imgur.com/9os1asN.jpg =300x)![mov2](https://i.imgur.com/VmGPgu2.jpg =300x) > 把CS的值複製一份到DX Addressing mode 定址模式 --- CPU找到資料的方式,稱作定址模式。 Access different type of operands, the 8088 is provided with various addressing modes. + `Register` addressing mode + `Immediate` addressing mode + `Memory` addressing mode + Direct + Register indirect + Based + Indexed + Based-Indexed ![addressing-mode](https://i.imgur.com/ff207Q5.jpg =300x) --- ### Register operand addressing mode ``` MOV AX, BX ``` ![ram](https://i.imgur.com/VkMyoXo.jpg =300x) ![ram2](https://i.imgur.com/zd7SxKt.jpg) > Data在register裡,不用到memory裡拿,也就是說不用去算實體位址。 --- ### Immediate operand addressing mode ``` MOV AL, 15H ``` ![iam](https://i.imgur.com/9jcsmUs.jpg =300x) ![iam2](https://i.imgur.com/tln6KYX.jpg) > Data在指令中裡,可立即拿到,不在register也不在memory,也就是因為不在memory裡,所以就不用去算實體位址。 --- ### Memory operand addressing mode `Physical address (PA)` is computed from a segment base address (SBA) and an effective address (EA) > 資料在memory裡,所以要去算**實體位址** Five memory operand addressing modes + Displacement:Direct addressing mode + BX, BP, SI, DI:Register indirect + BX or BP + displacement:Based + SI or DI + displacement:Indexed + BX, BP + SI, DI + displacement:Based-Indexed > Based和Indexed,很像,差別在用的register不同 ![mam](https://i.imgur.com/Bxw3kaq.jpg =300x) **Direct Addressing Mode** ``` MOV CX, [1234H] ``` ![dam](https://i.imgur.com/Gu9PTfm.jpg =500x) ![dam2](https://i.imgur.com/ZBEe1nO.jpg =400x) > PA = SBA * 16 + EA > 為何是用DS(Data segment)算呢?因為資料在資料區,所以是用DS算。 **Register Inderict Addressing Mode** ``` MOV AX, [SI] ``` ![riam](https://i.imgur.com/McGEVyU.jpg) ![riam2](https://i.imgur.com/BuwglnU.jpg =400x) **Based Addressing Mode** > 常用在一維陣列 ``` MOV [BX] + 1234H, AL ``` ![bam](https://i.imgur.com/HVhv6fJ.jpg) ![bam2](https://i.imgur.com/f9bRyto.jpg =400x) **Indexed Addressing Mode** ``` MOV AL, [SI] + 1234H ``` ![miam](https://i.imgur.com/CLivgvR.jpg) ![miam2](https://i.imgur.com/gZLGyGy.png =400x) **Based-Indexed Addressing Mode** > 常用在二維陣列 ``` MOV AH, [BX][SI] + 1234H ``` ![biam](https://i.imgur.com/aPniU4w.jpg) ![biam2](https://i.imgur.com/3StVZKX.jpg =400x) 參考資料 + Barry B. Bery, “The Intel Microprocessors,” 8th Edition, 2009, Prentice Hall. + Walter A. Triebel, Avtar Singh, “The 8088 and 8086 Microprocessors – Programming, Interfacing, Software, Hardware, and Applications,” 4th Edition, 2003, Prentice Hall. + 國立臺北大學資工系張玉山教授ppt