chapter five–80x86 assembly program development principles of microcomputers 2015年12月1日...
TRANSCRIPT
2023年4月21日 星期五 1
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Chapter Five
80x86 Assembly Program Development
( 3 )
2023年4月21日 星期五 2
Chapter Five–80x86 Assembly program development Principles of Microcomputers
5.6 汇编语言程序设计的基本方法
通常 , 编制一个汇编语言源程序应按如下步骤进行 :
⑴ 明确任务 , 确定算法。⑵ 绘制流程图。⑶ 根据流程图编写汇编语言程序。⑷ 上机调试程序。程序的基本结构有四种:顺序结构、分支结构
、循环结构和子程序结构。
2023年4月21日 星期五 3
Chapter Five–80x86 Assembly program development Principles of Microcomputers
5.6.1 顺序程序设计
顺序结构也称线性结构,其特点是其中的语句或结构被连续执行。
顺序程序是最简单的,也是最基本的一种程序结构。这种结构的程序从开始到结尾一直是顺序执行的,中途没有任何分支。从这种结构的流程图来看,除了有一个起始框,一个终止框外,就是若干执行框,没有判断框。
S1S1
S2S2
S3S3
开始开始
结束结束
2023年4月21日 星期五 4
Chapter Five–80x86 Assembly program development Principles of Microcomputers
[例 1 ]求两个数的平均值。这两个数分别存放在 X 单元和 Y 单元中,而平均值放在 Z 单元中。源程序编制如下 :
DATA SEGMENTX DB 8CHY DB 64HZ DB ?
DATA ENDSSTACK SEGMENT
DW 20H DUP(0)TOP LABLE WORD
STACK ENDS
2023年4月21日 星期五 5
Chapter Five–80x86 Assembly program development Principles of Microcomputers
CODE SEGMENTMAIN PROC FAR
ASSUME CS:CODE ASSUME DS:DATA
ASSUME SS:STACKSTART: PUSH DS
MOV AX, 0PUSH AXMOV AX, DATAMOV DS, AXMOV AX, STACKMOV SS, AXMOV SP, OFFSET TOP
2023年4月21日 星期五 6
Chapter Five–80x86 Assembly program development Principles of Microcomputers
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
2023年4月21日 星期五 7
Chapter Five–80x86 Assembly program development Principles of Microcomputers
5.6.2 分支程序设计分支程序结构也称条件结构,通常是在两种或两
个以上的不同的操作中选择其中的一个,如下图所示:
分支是通过条件转移指令来实现的。分支结构有一个共同点:运行方向总是向前的。
条件条件
S1S1 S2S2
条件条件
SS S1S1 S2S2 SnSn
条件条件
NN NN
YY YY
……
2023年4月21日 星期五 8
Chapter Five–80x86 Assembly program development Principles of Microcomputers
[例 2] 现有一符号函数 :1 当 X>0时
Y= 0 当 X=0时 -1 当 X<0时
假定 X为 -25,且存放在 VARX单元中,函数值 Y存放在 VARY单元,试编写程序根据 X的值确定函数 Y的值。
根据题意画出流程图如下:
2023年4月21日 星期五 9
Chapter Five–80x86 Assembly program development Principles of Microcomputers
实现符号函数程序的流程图
开始开始
AL←XAL←X
AL≥0?AL≥0?
Y←-1Y←-1
结束结束
AL = 0AL = 0
Y←1Y←1 Y←0Y←0
YY
YYNN
NN
2023年4月21日 星期五 10
Chapter Five–80x86 Assembly program development Principles of Microcomputers
编写程序如下 :DSEG SEGMENT
VARX DB -25VARY DB ?
DSEG ENDSCSEG SEGMENT
ASSUME CS:CSEG,DS:DSEGSTART: MOV AX, DSEG
MOV DS, AXMOV AL, VARXCMP AL, 0JGE NEXTMOV AL, 0FFHJMP HALT
2023年4月21日 星期五 11
Chapter Five–80x86 Assembly program development Principles of Microcomputers
NEXT: JE ZAREMOV AL, 1JMP HALT
ZARE: MOV AL, 0HALT: MOV VARY, AL
MOV AH, 4CHINT 21H
CODE ENDSEND START
MOV DL, VARYMOV CL, 4ROR DL, CLAND DL, 0FHCMP DL, 0AHJB NUMADD DL, 7
NUM: ADD DL, 30HMOV AH, 2INT 21HMOV DL, VARYAND DL, OFHCMP DL, 0AHJB NUBADD DL, 7
NUB: ADD DL, 30HMOV AH, 2INT 21HMOV DL, ‘H’MOV AH, 2INT 21H
2023年4月21日 星期五 12
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Flow of Control
Principles of Microcomputer
Computer Science Department, XIPT
2023年4月21日 星期五 13
Chapter Five–80x86 Assembly program development Principles of Microcomputers
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
2023年4月21日 星期五 14
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Specifying the Jump Target
• Instruction Label– A symbolic name defined to be an address in th
e code segment of a program– A label may be attached to any point in the cod
e of a program• a_Label: jmp a_Label
– Labels definitions usually have a colon at the end signifying them as NEAR
2023年4月21日 星期五 15
Chapter Five–80x86 Assembly program development Principles of Microcomputers
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 re
gisters
2023年4月21日 星期五 16
Chapter Five–80x86 Assembly program development Principles of Microcomputers
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
2023年4月21日 星期五 17
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Conditional Jumps
• Jxxx destination– There are 30 some variations that interrupt sequ
ential 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)
2023年4月21日 星期五 18
Chapter Five–80x86 Assembly program development Principles of Microcomputers
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:
2023年4月21日 星期五 19
Chapter Five–80x86 Assembly program development Principles of Microcomputers
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
2023年4月21日 星期五 20
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Implementing an IF-THEN
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:
2023年4月21日 星期五 21
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Implementing an IF-ELSE
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:
2023年4月21日 星期五 22
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Implementing a WHILE
int n;
while (n>0) n-=2;• This loop could be opt
imized 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:
2023年4月21日 星期五 23
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Compound Conditions
char n; int w,x;
if (n>='A' && w==x)
whatever();• This example uses sho
rt-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-partcall whatever
no_go:
2023年4月21日 星期五 24
Chapter Five–80x86 Assembly program development Principles of Microcomputers
Compound Conditions - OR
char n,k; unsigned int w;
if (n<>k || w<=10)
whatever();• This example uses sho
rt-circuit evaluation– if the first condition is t
rue 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_ifthen_:call whatever
end_if:
2023年4月21日 星期五 25
Chapter Five–80x86 Assembly program development Principles of Microcomputers
LOOP
• 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,9top_loop: add n,cx ;n=n+x loop top_loop
2023年4月21日 星期五 26
Chapter Five–80x86 Assembly program development Principles of Microcomputers
JCXZ destination
• 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_ittop_loop: call do_it loop top_loopskip_it:
2023年4月21日 星期五 27
Chapter Five–80x86 Assembly program development Principles of Microcomputers
LOOPZ/E and LOOPNZ/E
• 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 ?)
2023年4月21日 星期五 28
Chapter Five–80x86 Assembly program development Principles of Microcomputers
LOOPZ Example
• 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,9next_char: int 21h cmp al,13 loopne next_char;determine count mov ax, 0239h sub al,clmov dl,al
int 21h