收藏 分享(赏)

华南理工大学汇编原理课件06.ppt

上传人:jinchen 文档编号:5431114 上传时间:2019-03-02 格式:PPT 页数:76 大小:575.50KB
下载 相关 举报
华南理工大学汇编原理课件06.ppt_第1页
第1页 / 共76页
华南理工大学汇编原理课件06.ppt_第2页
第2页 / 共76页
华南理工大学汇编原理课件06.ppt_第3页
第3页 / 共76页
华南理工大学汇编原理课件06.ppt_第4页
第4页 / 共76页
华南理工大学汇编原理课件06.ppt_第5页
第5页 / 共76页
点击查看更多>>
资源描述

1、Chapter 6,Procedure过程,Textbook: 7 Program Logic and Control,Outline,StackPUSH POPProcedureDirectivesCALL RETSubprogram programming Methods of passing parameters,8086 Stack 8086堆栈,6.1,Stack (堆栈),Stack contains any data and addresses that the program needs to save temporarily or for use by your own “c

2、alled” subroutines.The stack segment (SS) register addresses the stack segment.堆栈是供程序使用的连续的内存空间,用于保存一些临时数据。,Main Use,The program calls a subroutine for special processing; It saves the return address in the stack, which the subroutine later uses for returning.The program that calls the subroutine ma

3、y also pass data by placing it in the stack, where the subroutine accesses it.The program has to perform calculations that use the registers; it can save the present contents of the registers on the stack, make the calculations, and then restore the data from the stack to the registers.,Stack (堆栈),T

4、he stack differs from other segments in its method of storing data: It begins storing data at the highest location in the segment and stores data downward (or backward) through memory. Last in First Out是一种具有特殊访问方式的存储空间,特殊性在于:最后进入这个空间的数据,最先出去。,PUSH,POP,Stack,Initially, the stack is empty and it looks

5、 like this:,Examples,mov bx, 2266H push bx mov cx, 1122H push cx pop ax pop bx,PA Stack Frame,Operation of PUSH BX (1),Initially, the stack is empty and it looks like this:SS:SP: 1000:000E,Examples,SS=1000H SP=000EH bx = 2266H,PA Stack Frame Pointer,Operation of PUSH BX (2),1)Decrement SP by 2 (000C

6、) SP - SP 2SS:SP: 1000:000C,Examples,PA Stack Frame Pointer,Operation of PUSH BX (3),2) Store the contents of BX, 2266, in the stack. Note the sequence of the stored bytes.,Examples,SS=1000H SP=000CH,stores data downward (or backward) through memory.入栈时,栈顶从高地址向低地址方向增长,stack,Decrements SP by 2 (000Ah

7、) and stores the contents of CX in the stack.“After“ state:,Examples,mov cx, 1122H push cx,stack,Restores the word from where SP points in the stack (000AH) to AX and increments SP by 2 (000CH)“After“ state: ax = 1122H,Examples,pop ax,PA Stack Frame Pointer,stack,Restores the word from where SP poin

8、ts in the stack (000CH) to BX and increments SP by 2 (000EH)“After“ state: bx = 2266H,Examples,pop bx,PA Stack Frame Pointer,Operation of POP BX (1),Initial state:SS:SP 1000:000C,Examples,SS=1000H SP=000CH,PA Stack Frame Pointer,Operation of POP BX (2),1) Restores the word from where SP points in th

9、e stack (000CH) to BX.SS:SP 1000:000C,Examples,SS=1000H SP=000CH bx = 2266H,PA Stack Frame Pointer,Operation of POP BX (3),2) And increments SP by 2 (000EH) SP SP + 2SS:SP 1000:000EBX=2266H,Examples,PA Stack Frame Pointer,Operation of POP BX (3),Note: the POP instructions are coded in reverse sequen

10、ce from PUSH instructions.入栈时,栈顶从高地址向低地址方向增长。,Examples,PA Stack Frame Pointer,Stack (堆栈),Stack contains any data and addresses that the program needs to save temporarily.The stack segment (SS) register addresses the stack segment.堆栈是供程序使用的连续的内存空间,用于保存一些临时数据。,Instructions,PUSH & POP,PUSH; Decrements

11、SP by 2 and stores the contents of the source operand in the stack.,POP; Restores the word from where SP points in the stack to the destination operand and increments SP by 2,PUSH SRC ; push onto the stack ; SRC can be r16/m16/seg_reg/imm ; SPSP2 ; SS:SPr16/m16/seg,Operations: (SP) (SP) 2 (SP) (SCR)

12、 (SP)+1)(SCR+1)not affect flag,PUSH Instruction,POP Instruction,Operations: (DST)(SP) (DST+1) (SP)+1) (SP) (SP)+2,POP Des ; Des can be r16/m16/seg_reg (except cs) ; r16/m16/segSS:SP ; SPSP2,2. 出栈指令POP,现场保护恢复,push ax ; when enter a subroutine push bx push dx . pop dx ; when exit from the subroutine p

13、op bx pop ax,Examples,Note: (1) Allow PUSH imm., not allow POP imm. (2) Allow PUSH CS, not allow POP CS(3) PUSH instruction pushes a word onto stack and decrement the sp by 2 (8086 or 80286). PUSH instruction pushes 2 words onto stack and decrement the sp by 4 (80386 or later).,(PUSH ),Summary,1) St

14、ack is a location space in stack-segment, work according to First-in-last-out.2) Just one entry-exit, so only one SP. The content of SP point to the current top (in or out location) of the stack at any time.3) PUSH or POP a word (not allow byte) .4) Any addressing (except immediate-addressing) .5) P

15、USH and POP instruction not affect flag-bit.,Procedure Body, Call and Return 过程体、调用和返回,6.2,Procedure,A procedure is a block of code that begins with the PROC directive and terminates with ENDP directive.,Procedure,Organizing a program into procedures provides some benefits:1. Reduces the amount of c

16、ode because a common procedure can be called from any number of places in the code segment.2. Encourages better program organization.,Procedure,3. Facilitates debugging of a program because defects can be more clearly isolated.4. Helps in the ongoing maintenance of programs because procedures are re

17、adily identified for modification.,Outline,Procedure DirectiveInstructions CALL RETSubroutine Programming,Procedure Directives,A code segment contains one or more procedures One procedure coded as:,ProcName PROC near|far ;entry point for first instruction to execute. . . ;end a procedureProcName END

18、P,Procedure Directives,The procedure name (ProcName) must be present, unique and follow assembler naming conventions.The procedure attribute can is NEAR/FAR , default is NEAR (that is, within 32K)attribute.,ProcName PROC near|far . .ProcName ENDP,Calling procedure and procedure,Instructions,Format:,

19、label2: RET pop-value,label1: CALL proc-name,Instructions,Call instruction is to transfer control to a called procedure. RET instruction returns from the called procedure to the original calling procedure ( the instruction after CALL instruction). RET should be the last instruction in the called pro

20、cedure.,Near Call and Return,A call to a procedure within the same segment is near.Performs: 1) by means of a push operation, decrements SP by 2 (one word) and transfers IP value onto the stack. 2) inserts the offset address of the called procedure into IP.,Near Call and Return,A RET that returns fr

21、om a near procedure basically reverses the CALL steps:1) pops the old IP value from the stack back into IP. 2) increments SP by 2.,Far Call and Return,A far call calls a procedure labeled far, possibly in another code segment.Performs: 1) first pushes the contents of CS onto the stack and inserts th

22、e new segment address in CS. 2) then pushes IP onto the stack and inserts a new offset address in IP.,Far Call and Return,The CS:IP pair now points to the address of the first instruction to execute in the called subprogram.,Far Call and Return,On exit from the called procedure, A far RET reverses t

23、he CALL steps:pops both the original IP and CS addresses back into their respective registers.,Far Call and Return,The CS:IP pair now points to the address of the instruction following the original CALL,CALL Instruction,Four types: (JMP),CALL label ; Intra-segment direct 段内 直接 CALL r16/m16; Inter-se

24、gment direct 段内 间接 CALL far ptr label ; Inter-segment direct 段间 直接 CALL far ptr mem ; Inter-segment Indirect 段间 间接,CALL Instruction,The assembler can tell from the procedure whether RET is near or far and generates the appropriate object code. But you can use “near ptr” or “far ptr” to set the attri

25、bute.,CALL Instruction,Four types of CALL,CALL label ; 段内调用,相对寻址; SPSP-2; SS:SPIP, IPIP+16位位移量CALL r16/m16 ; 段内调用,间接寻址; SPSP-2; SS:SPIP, IPr16/m16,label:the procedure name,(Near call and return ),Near call,(program),before call,memory,stack,Figure 8- Effect on the stack and IP by Near call,0FFF+ 000

26、31002,CALL Instruction,Four types of CALL,CALL far ptr label ; 段间调用,直接寻址; SPSP-2, SS:SPCS, ; SPSP-2, SS:SPIP, ; IPlabel偏移地址,CSlabel段地址,子程序调用指令,Four types of CALLNote the arrangement of the memory.,CALL far ptr mem ; 段间调用,间接寻址; SPSP-2, SS:SPCS, ; SPSP-2, SS:SPIP, ; IPmem, CSmem+2,RET Instruction,RET

27、instruction returns from the called procedure to the original calling procedure ( the instruction after CALL instruction). Format:,RET,Calling Procedure and Called Procedure,Subprogram programming,A programming convention is to provide comments at the start of each procedure to identify its purpose

28、and registers used.And, if necessary, to push changed registers at the start and to pop them on exiting.,subprogram,subname proc push ax push bx push cx ;procedure bodypop cx ;note : in reverse orderpop bxpop axret subname endp,Examples,if necessary, to push changed registers at the start and to pop

29、 them on exiting.,Display CR (carriage return) & LF(line feed),Examples,Examples,Display CR & LF,result,Parameters and Local Variable 参数和局部变量,6.3,Passing Parameters,Passing parameters by value (the actual data item) Pass values in registers Pass values in stackPassing parameters by reference (the ad

30、dress of the data) Addresses in registers Addresses in stack,例4.11 求校验和,子程序计算数组元素的“校验和” 校验和是指不记进位的累加入口参数: 数组的逻辑地址(传址)元素个数(传值)出口参数: 求和结果(传值),Passing parameters in register,入口参数:cx元素个数,DS:BX数组的段地址:偏移地址出口参数:al校验和,Examples,Passing parameters in registers,Examples,Passing parameters in registers,Passing

31、parameters in variables,入口参数:count元素个数,array数组名(含段地址:偏移地址)出口参数:result校验和,Examples,Passing parameters in variables,Recursion 递归,6.4,recursive,A recursive procedure or function is one that calls itself, either directly or indirectly.Write a complete 8086 assembly language program to calculate N!,Defin

32、ition of “N!” :0! = 1N! = N * (N-1)! (N0),Examples,flowchart,a. Main procedure,flowchart,b. subprogram,计算N!参考源程序,Type in 1,2 and 3 from keyboard to call three independence subprogram. Separate show A, B and C.assume cs:code, ds:data data segment TABLE DW ONEDW TWODW THREE data ends,Examples,code seg

33、ment start: mov ax,data mov ds,axTOP:MOV AH,1 ;read number from keyboard,INT 21HSUB AL,31H ;transfer ASCII of 1, 2, 3 to 0,1,2JB TOP ;if below 0, go to TOPCMP AL,2 JA TOP ;if above 2, go to TOP,Examples,MOV AH, 0 ;generate table address MOV BX, AX ADD BX, BX ;transfer 0,1,2 to 0,2,4 in BX CALL TABLEBX ;call subprogram one, two, threemov ax,4c00h int 21h,Examples,ONE PROC NEARMOV AH,2MOV DL, A ;show AINT 21HRET ONE ENDPTWO PROC NEARMOV AH,2MOV DL, B ;show BINT 21HRET TWO ENDP,Examples,THREE PROC NEARMOV AH,2MOV DL, C ;show CINT 21HRET THREE ENDPcode ends END start,Examples,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 中等教育 > 职业教育

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报