收藏 分享(赏)

华南理工大学汇编原理课件05-1.ppt

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

1、Chapter 5,Branching and Looping分支和循环,Textbook: 7 Program Logic and Control,Outline,The objective of this chapter is to describe the structures such as : if then , if then else (分支结构程序设计)while, until, for (循环结构程序设计),Transfer (控制转移类指令),Instructions that can transfer control outside the normal sequenti

2、al flow. By changing the offset value in IP or changing the value of IP and CS. jmp , jcc, loop,控制转移类指令通过改变IP(或同时修改CS和IP)值,实现程序执行顺序的改变,Short, near and far address,The assembler supports three types of addresses that are distinguish by their distance from the currently executing address.,3 types of a

3、ddresses - short,A short address, limited to a distance of -128 (80H) to 127(7FH) bytes.A short address is reached by a 1-byte offset.,3 types of addresses - near,A near address, limited to a distance of -32768 (8000H) to 32767(7FFFH) bytes within the same segment.A near address is reached by a 1-wo

4、rd or 2-word offset.,3 types of addresses - far,A far address, which may be within the same segment at a distance over 32K, or in another segment.A far address is reached by a segment address and an offset.,Transfer instructions (控制转移类指令),Unconditional (无条件转移指令) Jump (跳转) Procedure (过程) Interrupt (中

5、断) Conditional (条件转移指令) Looping (循环指令),Unconditional Jumps8086无条件转移,5.1,JMP Instruction,Unconditional : the operation transfers control under all circumstances.Operation: (IP) - (IP) + shift-value(CS) - (CS) of destination segment,JMP Instruction,Examples,JMP short, near, or far address;label / regi

6、ster / memory,jmp quit ;退出程序 quit: mov ax,4c00h ;退出并返回dosint 21h,JMP Instruction,A JMP operation to a label within -128 (80H) to +127 (7FH) bytes is a short jump. A JMP that exceeds -128 (80H) to +127 (7FH) bytes is a near jump (within 32K). A JMP that exceeds 32KB byte (within real model memory sys

7、tem) becomes a far jump (from a segment to another ).,address,The assembler generates one byte for the operation (EB) and one byte for the operand.,Jmp s - jmp 0008 - EB03,Examples,The assembler generates one byte for the operation (EB) and one byte for the operand.,Examples,Jmp s - jmp 000B - EB03,

8、JMP short / near / far address,Unconditional transfer instruction forJMP short / near / far address,JMP Instruction,Forward jumpBackward jump,jmp quit ;退出程序 quit: mov ax,4c00h ;退出并返回dosint 21h,Examples,forever: mov ax, 0jmp forever ;重复,Examples,Transfer type of JMP,Direct addressing vs. Indirect add

9、ressingIntra-segment transfer vs. Inter-segment transfer,Intra-segment vs. Inter-segment,实际编程时,汇编程序会根据目标地址的距离,自动处理成短转移、近转移或远转移程序员可用操作符short、near ptr 或far ptr 强制,Direct addressing vs. Indirect addressing,Direct addressing (直接寻址方式) 转移地址象立即数一样,直接包含在指令的机器代码中,就是直接寻址方式Indirect addressing (间接寻址方式) 转移地址在寄存器

10、或主存单元中,就是通过寄存器或存储器的间接寻址方式,用标号表达,用寄存器或存储器操作数表达,Transfer type of JMP,4 types,Types, Intra-segment direct addressing 段内转移 直接寻址 Intra-segment indirect addressing 段内转移 间接寻址 Inter-segment direct addressing 段间转移 直接寻址 Inter-segment Indirect addressing 段间转移 间接寻址, Intra-segment direct addressing,jmp again ; t

11、ransfer again: dec cx ; label “again”jmp output ; transfer output: mov result,al ;label “output”,Examples,实际为相对寻址,JMP label ;IPIP+shift,(2) Intra-segment indirect addressing,jmp ax ; IPAX jmp word ptr 2000h ; IP2000h,Examples,JMP r16/m16 ;IPr16/m16, Inter-segment direct addressing,jmp far ptr others

12、eg; otherseg is in another segment,Examples,JMP far ptr label ; IPlabel的偏移地址 ; CSlabel的段地址, Inter-segment direct addressing,jmp far ptr s,Examples, Inter-segment Indirect addressing,mov word ptr bx,0 mov word ptr bx+2,1500h JMP far ptr bx ;转移到1500h:0,Examples,JMP far ptr mem; IPmem,CSmem+2,JMP指令:段内相

13、对转移,JMP指令:段内寄存器间接转移,JMP指令:段间直接转移,JMP指令:段间间接转移,Conditional Jumps, Compare Instructions, and if Structure条件转移、比较指令 和 if 结构,5.2,Conditional jump instruction,The processor supports a variety of conditional jump instructions that transfer control depending on settings in the flag register .,DEC CX JNZ A2

14、0 ; test the setting of ZF,Examples,label: Jnnn short-address; 条件满足,发生转移:IPIP8位位移量 ; 条件不满足,顺序执行,Conditional jump instruction: CMP,cmp al,100 jb below ; if al100, jump to belowsub al,100 ; else (al100), alal-100 below: ,执行比较指令之后,可以根据标志判断两个数是否相等、大小关系等,Examples,Conditional jump instruction,For the 8086

15、286, the distance for a conditional jump must be a short, within 128 to +127 byte. If the operation exceeds the limit, the assembler issues a message “relative jump out of range”.The 80386 and later processors allow reaching any address within 32K(=215).,label: Jnnn short-address,Jnnn Instructions,J

16、nnn instructions transfer control depending on settings in the Flags register. (Table) not affect flags1. Jumps based on Unsigned (logical) data 2. Jump based on signed (Arithmetic) data 3. Special arithmetic tests 1比较无符号数高低 2比较有符号数大小 3判断单个标志位状态及其他,Jnnn Instructions,实际虽然指令只有16条,但却有30个助记符采用多个助记符,只是为了

17、方便记忆和使用,Jumps based on Unsigned (logical) data (无符号数高低),Above - Equal - Below,Jumps based on Unsigned (logical) data,cmp ax,bx jnb next ; jump if axbxxchg ax,bx next: .,Examples,Jumps based on Signed (Arithmetic) data ( 有符号数大小),Greater - Equal - Less,Jumps based on Signed data,cmp ax,bx jnl next ; j

18、ump to next if axbxxchg ax,bx next: .,Examples,Special arithmetic tests,JZ / JNZ,test al,80h ; 测试最高位jz next0 ; D70(ZF1),转移mov ah,0ffh ; D71,顺序执行jmp done ; 无条件转向 next0: mov ah,0 done: .test al,80h ; 测试最高位jnz next1 ; D71(ZF0),转移mov ah,0 ; D70,顺序执行jmp done ; 无条件转向 next1: mov ah,0ffh done: .,Examples,JS

19、 / JNS,; 计算|XY|(绝对值) ; X和Y为存放16位操作数的变量 ; 结果存入变量resultmov ax,Xsub ax,Yjns nonnegneg ax ; neg是求补指令 nonneg: mov result,ax,Examples,JO / JNO,; 计算XY; ; X和Y为存放16位操作数的变量 ; 若溢出,则转移到overflow处理mov ax,Xsub ax,Yjo overflow. ; 无溢出,结果正确 overflow: . ; 有溢出处理,Examples,JP / JNP,; 设字符的ASCII码在AL寄存器中 ; 将字符加上奇校验位 ; 在字符AS

20、CII码中为“1”的个数已为奇数时 ; 则令其最高位为“0”; 否则令最高位为“1”and al,7fh; 最高位置0,同时判断1的个数jnp next; 个数已为奇数,则转向nextor al,80h ; 否则,最高位置“1” next: .,Examples,JC / JNC,; 记录BX中1的个数xor al,al ; AL0,CF0 again: test bx,0ffffh ; 等价于 cmp bx,0je nextshl bx,1jnc againinc aljmp again next: . ; AL保存1的个数,Examples,记录 BX 中 “1” 的个数,xor al,a

21、l ; AL0,CF0 again: cmp bx,0jz nextshl bx,1 ; 也可使用 shr bx,1adc al,0jmp again next: . ; AL保存1的个数,Examples,法 2,JCXZ,JCXZ just tests the contents of the CX register for zero, not affect and not tests flags.,JCXZ label; if CX = 0, then jump to label,Using the JCXZ instruction, search the memory beginning at location 2000H:0 for the first unit whose content is zero. And then store the offset of the unit in dx. 利用jcxz指令,在2000H:0开始的内存单元中查找第一个值为0的字节,找到后将它的偏移地址存储在 dx 中。,Exercises,Answer,

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

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

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


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

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

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