【正文】
adcount=404 */ /* present=404 */ Void main ( ) /* *present=5 */ { /* present=406 */ int headcount=5。 int *present=headcount。 printf (“ headcount= %d \n headcount= %d \n present= %d \n *present= %d \n present= %d \n” ,headcount, headcount, present, *present, present )。 } DATA SEGMENT headcount DW 5 present DW OFFSET headcount DATA SEGMENT CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV AX, DATA MOV DS, AX MOV BX, present MOV AX, [BX] … CODE ENDS END START 4. 用棧傳遞參數(shù) 為了用棧傳遞參數(shù)到一過程,在主程序中的過程調用之前,應把這些參數(shù)壓進棧的某個地方,然后過程中的指令按需從棧中讀取參數(shù)。同理,過程也把傳回調用程序的參數(shù)寫入棧中,然后主程序中的指令把這些參數(shù)讀出。 S e g m e n t r e g i s t e r : C S D S E S S SO f f s e t r e g i s t e r ( s ) : I P S I , D I , B X S I , D I , B X S P , B PO f f s e t R e g i s t e r s f o r V a r i o u s S e g m e n t s R A MF F F F F H0 0 0 0 0 HX X X X 0 HX X X X 0 HX X X X 0 HC o d e s e g m e n t6 4 kD a t as e g m e n t6 4 kS t a c ks e g m e n t6 4 kE x t r as e g m e n t6 4 k[ B X ][ S P ][ B X ]I PC P U[ S I ][ D I ][ B P ][ D I ][ S I ]A H A LB H B LC H C LD H D LB PS ID IS PF l a gI PC SD SS SE S* 1 0 H DATA SEGMENT BCD_IN DB 17H BIN_VAL DB ? DATA ENDS STACK SEGMENT DW 100 DUP (0) STACK ENDS CODE SEGMENT ASSUME CS: CODE, DS: DATA, SS: STACK START: MOV AX, DATA MOV DS, AX MOV AX, STACK MOV SS, AX MOV AL, BCD_IN PUSH AX CALL BCD_BIN POP AX MOV BIN_VAL, AL NOP BCD_BIN PROC NEAR PUSHF PUSH AX PUSH BX PUSH CX PUSH BP MOV BP, SP MOV AX, [BP+12] B P HC LC HB LB HA LA HF LF HI P LI P HA H. . .S S * 1 0 H = 7 0 0 0 0 HB P 壓 棧 后 0 0 4 2 HA X 壓 棧 后 0 0 4 E HA X 壓 棧 前 0 0 5 0 HB P LA L棧 狀 態(tài) 圖 MOV BL, AL AND BL, 0FH AND AL, 0F0H MOV CL, 04 ROR AL, CL MOV BH, 0AH MUL BH ADD AL, BL MOV [BP+12], AL ; BP缺省段為 SS POP BP POP CX POP BX POP AX POPF RET BCD_BIN ENDP CODE ENDS END START ( d)用棧傳遞參數(shù)實例 include int tempc, tempf int c2f(int c)。 void get_temp(int *ptr)。 void main( ) { get_temp ( tempc )。 tempf=c2f ( tempc )。 printf ( “ The temperature in Celsius is %d \n” , temp )。 printf ( “ The temperature in Fahrenheit is %d \n” , temp )。 } int c2f ( int c ) { int f。 f=9*c/5+32。 return(f)。 } Void get_temp(int *ptr ) { printf ( “ Please enter the Celsius temperature. /n” )。 scanf (“ %d ” ,ptr )。 } 程序舉例:說明、調用和定義 C函數(shù)模板 參數(shù)傳遞小結: 在調用程序和過程之間可以用( 1)寄存器、( 2)指定存儲單元(用名字或指針存?。┗蛘撸?3)棧傳遞參數(shù)。選擇哪種方法很大程度上依賴于特定程序,沒有標準規(guī)則,不過有幾條建議。對于少量參數(shù)傳遞的簡單程序,使用寄存器通常是最容易的方法,對于向 /從過程傳遞數(shù)組或其他數(shù)據(jù)結構,則用寄存器傳遞指向這些數(shù)據(jù)結構開始地址的指針,如前面已解釋過的,傳遞指針到過程比過程用名字直接存取數(shù)據(jù)結構要靈活的多。 對于多用戶程序中的過程,從高級語言中調用的過程或過程調用自身,應該在棧中傳遞參數(shù)。當編寫在棧中傳遞參數(shù)的程序中,應該借助如上圖所示中的棧狀態(tài)圖幫助你記錄當前棧中任何內容的位置。 常用系統(tǒng)功能調用和 BIOS中斷調用 微型計算機系統(tǒng)( IBM PC 及兼容機)為匯編用戶提供了兩個程序接口,一個是 DOS 系統(tǒng)功能調用,另一個是 ROM中的 BIOS (Basic Input/Output System)。 系統(tǒng)功能調用和 BIOS 由一系列的中斷服務子程序構成,但調用與返回不是使用子程序調用指令 CALL 和返回指令 RET, 而是通過軟中斷指令 INT n 和中斷返回指令 IRET 調用和返回的。 DOS 系統(tǒng)功能調用和 BIOS 的服務子程序,使得程序設計人員不必涉及硬件就可以使用系統(tǒng)的硬件,尤其是 I / O 的使用與管理 。 用 戶 區(qū) :應 用 程 序 , D O SR A M6 4 0 KV i d e o D i s p l a yR A M1 2 8 KR O M2 5 6 K0 0 0 0 0 HC O O O HB F F F F HA O O O H9 F F F F HF F F F F HM e m o r y A l l o c a t i o n i n t h e I B M P C 類 型 0 指 針 除 法 錯 誤類 型 2 5 5 指 針 ( 可 用 )..類 型 3 3 指 針 ( 可 用 )類 型 3 2 指 針 ( 可 用 )類 型 3 1 指 針 ( 保 留 )..類 型 5 指 針 ( 保 留 )類 型 4 指 針 溢 出類 型 3 指 針 1 字 節(jié) I N T 指 令類 型 2 指 針 非 屏 蔽 中 斷類 型 1 指 針 單 步I N T 0 0I N T 0 1I N T F FI N T 0 5I N T 0 4I N T 0 3I N T 0 2C SI P0 0 0 H3 F F H3 F C H0 8 0 H0 7 C H0 1 4 H0 1 0 H0 0 C H0 0 8 H0 0 4 H8 0 8 6 中 斷 向 量 表 Difference between INT and CALL instructions ? A “ CALL FAR” instruction can jump to any location within the 1 megabyte address range of the 8086 / 8088 CPU, but “ INT nn” goes to a fixed memory location in the interrupt vector table to get the address of the interrupt service routine. ? A “ CALL FAR” instruction is used by programmer in the sequence of instructions in the program but an externally activated hardware interrupt can e in at any time, requesting the attention of the CPU. ? A “ CALL FAR” instruction cannot be masked (disabled), but “ INT nn” belonging to externally activated hardware interrupts can be masked. ? A “ CALL FAR” instruction automatically saves only CS:IP of the next instruction on the stack, while “ INT nn” saves FR (flag register) in addition to CS:IP of the next instruction. ? At the end of the subroutine that has been called by the “ CALL FAR” instruction, the RET (return FAR) is the last instruction, whereas the last instruction in the interrupt service routine (ISR) for “ INT nn” is the instruction IRET (interrupt return). The difference is that RET pops CS,IP off the stack but the IRET pops off the FR (flag register) in addition to CS and IP. 1 41 5R = r e s e r v e d S F = s i g n f l a gU = u n d e f i n e d Z F = z e r o f l a gO F = o v e r f l o w f l a g A F = a u x i l i a r y c a r r y f l a gD F = d i r e c t i o n f l