




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1 微機原理與接口技術Theory and Interface Technology of puter北京化工大學信息科學與技術學院主講教師: 郭青 Chapter 3Assembly Language Programming233.5 Assembly Language Programming匯編語言程序的基本格式匯編語言程序設計步驟:分析問題,建立數學模型確定算法(解決問題的方法、步驟)繪制程序流程圖合理分配存儲空間和寄存器編制程序調試程序4上機調試的過程用編輯程序編寫源程序,如 edit,存盤為文件名為abc.asm的文件使用宏匯編程序masm將擴展名問.asm的源程序匯編成目標程序,在
2、盤上生成一個擴展名為.obj 的文件 masm abc.asm使用連接程序link將擴展名為.obj的目標程序連接裝配成可執行文件.exe并存盤 link abc.obj使用debug調試程序,調試.exe可執行文件 debug abc.exe 3.5.1 Assembly Program Structure5DATA SEGMENT ORG 2100hZBCD DB 38HFBCD DB ?DATA ENDSSTACK SEGMENT DW 256 DUP (?)STACK ENDSCODE SEGMENT ASSUME CS:CODE, DS: DATA, SS:STACKMAIN PRO
3、C FAR; Define the main procedure FARSTR: PUSH DS XOR AX, AX PUSH AX; used with RET for exiting to DOS例、在內存數據區2100H單元存有2位組合BCD碼,將其變成分離BCD碼,低位存于2100H,高位存于2101H。Example6 MOV AX, DATA MOV DS, AX MOV AX, STACK MOV SS, AX ; Load DS and SS LEA BX, ZBCD MOV AL, BX AND ZBCD, 0FH;mask most digit 屏蔽高4位 MOV CL,
4、 4 SHR AL, CL; move most digit 將原高4位移至低4位 MOV BX+1, AL;store most digit 存BCD碼高位 RETMAIN ENDPCODE ENDS END STRKey point7The system must return to DOS operation after the program is terminated. Two ways:MOV AH, 4CH ; Call DOS function 4CHINT 21H; EXIT to DOSMAIN PROC FARPUSH DSMOV AX, 0PUSH AX :; body
5、 of the main procedureRETMAINENDPKey point8匯編程序執行完后,要保證系統回到DOS操作系統下。兩種返回方式:1、將代碼段定義為一個屬性為far的子程序,程序框架如下: main proc far push ds xor ax,axpush ax .; 代碼段的功能主體 ret main endp2、在代碼段主程序的最后(不一定是代碼段最后)調用DOS 4CH,結束程序返回。此時,主程序無需定義為FAR過程。 Program Structure9Common control structure of Assembly programSequence pr
6、ogramBranch (decision) program Loop program Sequence Programstatements within a structured program are executed in the same sequence as they are listed within source program.順序結構的程序按指令序列在存儲器中的存放順序來執行。3.5.2 Branch Program10A branch program is based on a decision. It is similar to a IF.THEN or IF.THEN
7、.ELSE statement in the high level language.A decision is some sort of branch within the code that switches between two possible execution paths based on some condition. Normally (though not always), conditional instruction sequences are implemented with the conditional jump instructions.分支程序設計需要對復雜的
8、問題進行邏輯判斷,根據不同的條件實現不同的分支。通常使用條件轉移指令實現。Branch Flow11 case 1 case 2 case n? case 1 case 2 case n SWITCH-CASE structure IF-THEN-ELSE structureUsing Conditional Jump Instructions121. 用條件轉移指令實現程序分支例 編程實現表達式 1 x0 y= 0 x=0 x: -128127 signed number -1 x 0 0= 0Example13DATA SEGMENTX DB -68 Y DB ? DATA ENDSSTA
9、CK SEGMENT DW 100 DUP(0) STACK ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA, SS:STACK ; 段地址說明 START: MOV AX, DATA MOV DS, AX ; 數據段地址裝填;(堆棧段地址由系統裝填) Example14 MOV AL, X CMP AL, 0 ;和0比較 JGE LOP1 ;高于等于0時轉移 MOV Y, 0FFH ;否則設為1 JMP DONE ;無條件跳轉 LOP1: JZ ZEROO ;等于0轉移 MOV Y, 1 ;大于0則賦值1 JMP DONE ZEROO: MOV Y, 0
10、DONE: MOV AH,4CH ;返回DOS INT 21HCODE ENDS END STARTSwitch/Case Branch152. 用跳轉表形成多路分支The SWITCH checks the value of register against the constants, const1. constn. If a match is found then the corresponding statements execute.the SWITCH normally uses an indirect jump to transfer control to any one of
11、several statements with a single computation.Branch Table16例:根據 AL 寄存器中哪一位為 1(從低位到高位), 把程序轉移到 8 個不同的程序分支Branch_table DW routine1 DW routine2 DW routine3 DW routine4 DW routine5 DW routine6 DW routine7 DW routine8CODE for SWITCH 17 ; indirect jump with REG cmp al, 0 ;Test AL je continue lea bx, branc
12、h_tableL: shr al, 1 ;Logic shift right jnc add1 jmp word ptr bx ;Intra-segment indirect jumpadd1: add bx, type branch_table;add bx,2 jmp Lcontinue: routine1: routine2: SWITCH 18 cmp al, 0 je continue mov si, 0 L: shr al, 1 ;邏輯右移 jnc add1 jmp branch_tablesi ;段內間接轉移add1: add si, type branch_table jmp
13、Lcontinue: routine1: routine2: 寄存器相對尋址Indirect jump with MEM3.5.3 LOOP Program191. LOOP instructions 4條 不影響標志 LOOP LOOPE/LOOPZ LOOPNE/LOOPNZ JCXZ2. Program loops consist of three components: optional initialization component 初始化 loop termination test 循環控制 the body of the loop循環體LOOP203. LOOP structu
14、reDO-UNTILexecutes the initialization code, the loop body, then tests some condition to see if the loop should repeat.先執行后判斷, 條件不成立繼續循環DO-WHILEtests the termination condition at the beginning of the loop.先判斷后執行, 條件成立繼續循環4. Termination conditionCounter Conditional jumpExample 121例3編定程序,將帶符號的字節數組ARRY中
15、最大數找出來,送到MAX單元中。分析:在字節數組中找出最大數,可以把每1個數送AL中,讓AL與第2個數及它后面的每一個數進行比較,每次比較時將大者放AL中,最后把AL的值送MAX單元。DATA SEGMENTARRY DB 23H,78H,0ABH,0CDH,00H,56H DB 14H,86H,0EFH,0BCH,10H,0C0H CNT EQU $-ARRY ;字節個數 MAX DB ? DATA ENDS STACK SEGMENT PAPA STACK DW 20 DUP(?) STACK ENDSExample22CODE SEGMENT ASSUME CS:CODE,DS:DATA
16、,SS:STACK ; 段地址說明MAIN PROC FARSTART: PUSH DS XOR AX, AX PUSH AX MOV AX, DATA MOV DS, AX ;數據段地址裝填 LEA SI, ARRY ;初始化地址指針 MOV CX, CNT-1 ;設置循環次數 MOV AL, SI LOP1: INC SI ;地址指針增1 CMP AL, SI ;與下一個數比較 JGE LOP2 ;大于等于時轉LOP2 MOV AL, SI ;取較大的數放AL寄存器中 LOP2: LOOP LOP1 ;判斷終止條件,CX-10則循環 MOV MAX, AL ;保存最大數 RETMAIN E
17、NDPCODE ENDS END STARTExample 223例:試編制一程序,從鍵盤輸入一行字符,要求第一個鍵入的字符必須是空格符,如不是則退出程序;如是,則開始接收鍵入的字符并順序存放在首地址為buffer的緩沖區中(空格符不存入),直到接收到第二個空格符時退出程序。分析:這一程序要求接收的字符從空格符開始又以空格符結束,因此程序中必須區分所接收的字符是否是第一個字符。為此,設立作為標志的存儲單元FLAG。一開始將其置為0,接收第一個字符后可將其置1。Example 224Example 225; 定義數據段datareasegmentbufferdb80 dup(?)flagdb?d
18、atareaends; 定義代碼段prognamsegment mainprocfarassume cs:prognam, ds:datareaStart:pushdssubax, axpushaxmovax, datareamovds, axleabx, buffermovflag, 0Example 226next: mov ah, 01int21h; read a charactertestflag, 01h; test if it is the first characterjnz/jnefollow; if not, branch to followcmpal, 20h; else,
19、 test spacejnz/jneexit; if the first is not a space, exitmovflag, 1; if space, set flagjmpnext; continue follow: cmpal, 20h; test spacejz/jeexit; if input second space, exitmovbx, al; else, store the characterincbxjmpnext; continueexit: ret ; exitmainendp prognam ends end start 3.5.4 Procedure27 子程序
20、設計1. Defining ProcedureA procedure is a group of instructions that usually performs one task.A procedure begins with the PROC directive and ends with ENDP directive.The CALL instruction links to the procedureRET instruction returns from procedure. The stack stores the return address whenever a proce
21、dure is called during the execution of a program.Procedure282. Saving the State of the Machine 保護和恢復現場Preserving register meansYou need save the involving registers upon entry into the subroutine and restore them before leaving.Use PUSHandPOPinstructions to preserve register values while you need to
22、 use them for something else.主程序與被調用的子程序可能會用到同一個寄存器。在進入過程前保存該過程所使用的寄存器內容,稱保護現場;從過程返回主程序前,恢復這些寄存器的內容,稱恢復現場。Procedure29Example:SUBRPPROC FARPUSH AXPUSH BXPUSH CXPUSH DX :POP DXPOP CXPOP BXPOP AXRETSUBRPENDPNOTE:在主程序和子程序傳送參數的寄存器不應保護和恢復。Procedure303. Pass parameters to and from procedure主程序和子程序間的參數傳遞Mos
23、t procedures require some input data and return some data to the caller. Parameters are values that you pass to and from a procedure.The parameters are pass by:RegisterAddress tableStack 4. Procedure Example31編程將內存中字變量number的值,在屏幕上以二進制形式顯示出來。將一位二進制數轉換為ASCII碼編成子程序。子程序名:HEXD入口參數:待轉換的一位十六進制數,存于AL中出口參數:轉換后的ASCII碼,存于AL中子程序清單:HEXDPROC FARCMP AL, 0AHJC XXXADD AL, 07HXXX:ADD AL, 30HRETHEXDENDPProcedure Example32主程序:DATA SEGMENTNUMBERDW 3A5DHSTRINGDB 4 DUP (?), $DATAENDSSTACKSEGMENTDW 100 DUP (0)STACKENDS
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 政府采購車輛居間合同范本(2篇)
- 2024-2025日常安全培訓考試試題及參考答案(綜合卷)
- 2025員工三級安全培訓考試試題及答案解析
- 專題 J-13【簡語法-單選題專練】定語從句 2025年中考英語講解+練習題匯編(全國)
- 2025石油化工代理合同
- 2025企業借款合同范本模板
- 可行性研究報告 意見
- 2025網站開發合同協議書模板
- 2025《股權轉讓合同》
- 2025年舒血寧注射液合作協議書
- 2025-2030中國生物質能發電行業市場現狀供需分析及投資評估規劃分析研究報告
- 夫妻債務轉讓協議書范本
- 2025年房地產經紀人(業務操作)考前必刷綜合題庫(800題)附答案
- 桌球助教合同協議
- 電商行業10萬字PRD
- 10.2 保護人身權(課件)-2024-2025學年七年級道德與法治下冊
- 高一下學期《雙休時代自由時間背后暗藏殘酷篩選+你是“獵手”還是“獵物”?》主題班會
- 交互式影像中敘事與視覺表達的融合及其觀眾體驗研究
- 廣東省茂名市2025屆高三二模考試地理試題(含答案)
- 2025年上半年福建福州市金融控股集團限公司招聘22人易考易錯模擬試題(共500題)試卷后附參考答案
- 胰島素皮下注射團體標準
評論
0/150
提交評論