1 / 28

Chapter Five 80x86 Assembly Program Development ( 3 )

Chapter Five 80x86 Assembly Program Development ( 3 ). 5.6 汇编语言程序设计的基本方法. 通常 , 编制一个汇编语言源程序应按如下步骤进行 : ⑴ 明确任务 , 确定算法。 ⑵ 绘制流程图。 ⑶ 根据流程图编写汇编语言程序。 ⑷ 上机调试程序。 程序的基本结构有四种:顺序结构、分支结构、循环结构和子程序结构。. 5.6.1 顺序程序设计. 开始. 顺序结构也称线性结构,其特点是其中的语句或结构被连续执行。

strom
Download Presentation

Chapter Five 80x86 Assembly Program Development ( 3 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Five80x86 Assembly Program Development(3) 2014年10月3日1

  2. 5.6 汇编语言程序设计的基本方法 通常,编制一个汇编语言源程序应按如下步骤进行: ⑴ 明确任务,确定算法。 ⑵ 绘制流程图。 ⑶ 根据流程图编写汇编语言程序。 ⑷ 上机调试程序。 程序的基本结构有四种:顺序结构、分支结构、循环结构和子程序结构。 2014年10月3日2

  3. 5.6.1 顺序程序设计 开始 顺序结构也称线性结构,其特点是其中的语句或结构被连续执行。 顺序程序是最简单的,也是最基本的一种程序结构。这种结构的程序从开始到结尾一直是顺序执行的,中途没有任何分支。从这种结构的流程图来看,除了有一个起始框,一个终止框外,就是若干执行框,没有判断框。 S1 S2 S3 结束 2014年10月3日3

  4. [例1]求两个数的平均值。这两个数分别存放在X单元和Y单元中,而平均值放在Z单元中。源程序编制如下:[例1]求两个数的平均值。这两个数分别存放在X单元和Y单元中,而平均值放在Z单元中。源程序编制如下: DATA SEGMENT X DB 8CH Y DB 64H Z DB ? DATA ENDS STACK SEGMENT DW 20H DUP(0) TOP LABLE WORD STACK ENDS 2014年10月3日4

  5. CODE SEGMENT MAIN PROC FAR ASSUME CS:CODE ASSUME DS:DATA ASSUME SS:STACK START: PUSH DS MOV AX, 0 PUSH AX MOV AX, DATA MOV DS, AX MOV AX, STACK MOV SS, AX MOV SP, OFFSET TOP 2014年10月3日5

  6. MOV AL, X ADD AL, Y MOV AH, 0 ADC AH, AH SHR AX, 1 MOV Z, AL RET MAIN ENDP CODE ENDS END START 2014年10月3日6

  7. 5.6.2 分支程序设计 分支程序结构也称条件结构,通常是在两种或两个以上的不同的操作中选择其中的一个,如下图所示: 分支是通过条件转移指令来实现的。分支结构有一个共同点:运行方向总是向前的。 Y Y 条件 条件 条件 … N N S S1 S2 Sn S1 S2 2014年10月3日7

  8. [例2] 现有一符号函数: 1 当X>0时 Y= 0 当X=0时 -1 当X<0时 假定X为-25,且存放在VARX单元中,函数值Y存放在VARY单元,试编写程序根据X的值确定函数Y的值。 根据题意画出流程图如下: 2014年10月3日8

  9. 开始 AL←X 实现符号函数程序的流程图 AL≥0? Y N AL=0 Y Y←-1 N Y←1 Y←0 结束 2014年10月3日9

  10. 编写程序如下: DSEG SEGMENT VARX DB -25 VARY DB ? DSEG ENDS CSEG SEGMENT ASSUME CS:CSEG,DS:DSEG START: MOV AX, DSEG MOV DS, AX MOV AL, VARX CMP AL, 0 JGE NEXT MOV AL, 0FFH JMP HALT 2014年10月3日10

  11. MOV DL, VARY MOV CL, 4 ROR DL, CL AND DL, 0FH CMP DL, 0AH JB NUM ADD DL, 7 NUM: ADD DL, 30H MOV AH, 2 INT 21H MOV DL, VARY AND DL, OFH CMP DL, 0AH JB NUB ADD DL, 7 NUB: ADD DL, 30H MOV AH, 2 INT 21H MOV DL, ‘H’ MOV AH, 2 INT 21H NEXT: JE ZARE MOV AL, 1 JMP HALT ZARE: MOV AL, 0 HALT: MOV VARY, AL MOV AH, 4CH INT 21H CODE ENDS END START 2014年10月3日11

  12. Flow of Control Principles of Microcomputer Computer Science Department, XIPT 2014年10月3日12

  13. Unconditional Jump • JMP [operator] destination • Intrasegment direct E9 disp_16 • Intrasegment direct short EB disp_8 • Intrasegment indirect FF mod 100 r/m ?? • Intersegment direct EA disp_16 seg_16 • Intersegment indirect FF mod 101 r/m ?? • operator : SHORT, NEAR PTR, or FAR PTR • NEAR PTR is the usual default 2014年10月3日13

  14. Specifying the Jump Target • Instruction Label • A symbolic name defined to be an address in the code segment of a program • A label may be attached to any point in the code of a program • a_Label: jmp a_Label • Labels definitions usually have a colon at the end signifying them as NEAR 2014年10月3日14

  15. Executing a Jump • Intrasegment jumps are caused by changing the IP register to a new value • Short jumps add a signed 8-bit displacement to IP • Near jumps add a signed 16-bit displacement to IP • Intersegment jumps change both the CS and IP registers • Far jumps simply assign new values to these registers 2014年10月3日15

  16. Sample Jump Encodings 1106:0100 EB2A JMP 012C • 012C-0102=002A 1106:0102 EBFC JMP 0100 • 0100-0104=FFFC 1106:0104 E97F00 JMP 0186 • 0186-0106=0080 (too far for short!) • 0186-0107=007F 1106:0107 E9F5FE JMP FFFF • FFFF-010A=FEF5 2014年10月3日16

  17. Conditional Jumps • Jxxx destination • There are 30 some variations that interrupt sequential flow based on various flag settings • JNZ - Jump if zero flag is clear (0) meaning the result of a previous operation was non-zero • JC - Jump if a previous operation caused the carry flag to be set (1) 2014年10月3日17

  18. Range of Conditional Jumps • All conditional jumps are SHORT • range is -128 to +127 bytes • 80386+ allow larger distances • Combine a conditional and unconditional jump to overcome this range limitation jz too_far ;ugh jnz is_close ;use code at jmp near ptr too_far ;right! is_close: 2014年10月3日18

  19. Using Conditional Jumps • Conditional jumps typically follow an instruction that alters the flag bits • CMP destination, source • Computes (destination-source) and sets flag bits • result is not stored • flags allow us to decide <, <=, >, >=, ==, <>, etc • we can also interpret the results meaningfully for signed or unsigned data 2014年10月3日19

  20. unsigned int n; if (n>7) do_it(); If n is a signed int, use jng (not greater) unsigned: above, below signed less, greater ;if (n>7) mov ax,n cmp ax,7 jna skip_it ;then-part call do_it ;end if skip_it: Implementing an IF-THEN 2014年10月3日20

  21. char n; if (n=='7') do_it(); else do_that(); Document the control structures and keep the parts in the usual order ;if (n=='7') cmp n,'7' jne else_ ;then-part call do_it jmp short endif else_: call do_that endif: Implementing an IF-ELSE 2014年10月3日21

  22. int n; while (n>0) n-=2; This loop could be optimized by keeping n in a register and storing to memory only at end of loop ;while (n>0) while_: cmp n,0 jle end_while ;loop-body sub n,2 jmp while_ end_while: Implementing a WHILE 2014年10月3日22

  23. char n; int w,x; if (n>='A' && w==x) whatever(); This example uses short-circuit evaluation if the first condition is false it immediately skips past the then-part ;if(n>='A'&&w==x) cmp n,'A' jl no_go mov ax,w cmp ax,x jne no_go ;then-part call whatever no_go: Compound Conditions 2014年10月3日23

  24. char n,k; unsigned int w; if (n<>k || w<=10) whatever(); This example uses short-circuit evaluation if the first condition is true it immediately skips to the then-part ;if(n<>k||w<=10) mov ah,n cmp ah,k jne then_ cmp w,10 ja end_if then_: call whatever end_if: Compound Conditions - OR 2014年10月3日24

  25. LOOP destination decrements CX but does not change any flags if CX is not zero after the decrement, control is transferred to the destination label This is a SHORT jump only for (x=9;x>0;x--) n+=x; ;for(x=9;x>0;x--) mov cx,9 top_loop: add n,cx ;n=n+x loop top_loop LOOP 2014年10月3日25

  26. Directly compares CX to 0 and jumps to the destination if equal This instruction does not affect the flags It is commonly used to bypass the first iteration of a loop if the count is already 0 ;while(x>0)do_it(); mov cx,x jcxz skip_it top_loop: call do_it loop top_loop skip_it: JCXZ destination 2014年10月3日26

  27. Enhancement of the LOOP instruction The state of the ZERO Flag may also cause loop termination Loop while ZF/equal && CX!=0 Loop while (NZ/ not equal) && CX!=0 Remember that LOOP decrements CX, but this does not affect the flags! LOOPZ == LOOPE LOOPNZ==LOOPNE Some action inside the loop should affect the zero flag (cmp ?) LOOPZ/E and LOOPNZ/E 2014年10月3日27

  28. This program accepts at most 9 characters from the keyboard When the 9th character is pressed (or the enter key is used) the number of keypresses is displayed mov ah,1 mov cx,9 next_char: int 21h cmp al,13 loopne next_char ;determine count mov ax, 0239h sub al,cl mov dl,al int 21h LOOPZ Example 2014年10月3日28

More Related