80 x86课程英文版的课件chapter 5.ppt_第1页
80 x86课程英文版的课件chapter 5.ppt_第2页
80 x86课程英文版的课件chapter 5.ppt_第3页
80 x86课程英文版的课件chapter 5.ppt_第4页
80 x86课程英文版的课件chapter 5.ppt_第5页
已阅读5页,还剩44页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Chapter 5 Branching and Looping,Contents: Jump instructions target addressCS:EIP Statementlable : address of other assembly language statement Similar to GOTO,JMP quit . . quit: INVOKE ExitProcess, 0,Example:5.1,output explain ; initial instructions mov sum,0 ; sum := 0 mov ebx,0 ; count := 0 foreve

2、r: output prompt ; prompt for number input number,16 ; read ASCII characters atod number ; convert to integer,Example:5.1,add sum,eax ; add number to sum inc ebx ; add 1 to count dtoa value,ebx ; convert count to ASCII output countLabel ; display label for count output value ; display count,Example:

3、5.1,dtoa value,sum ; convert sum to ASCII output sumLabel ; display label for sum output value ; display sum,Example:5.1,mov eax,sum ; get sum cdq ; extend sum to 64 bits idiv ebx ; sum / count dtoa value,eax ; convert average to ASCII output avgLabel ; display label for average output value ; outpu

4、t average,Example:5.1,output nextPrompt ; skip down ; start next prompt jmp forever ; repeat,Jump direction,Backward reference(向后跳转) Transfer control to a point that precedes the jmp statement itself. Example5.1 Forward reference(向前跳转) Transfer control to a point that is behind the jmp statement its

5、elf. Jmp quit,About JMP instructions,JMP instructions will change the value in the EIP register; Two kinds of JMP instructions Intersegment jump (段间转移) Change CS register Intrasegment jump(段内转移) Not change CS register,Relative jump/direct jump相对跳转/直接转移,Contains the sign displacement of the target fr

6、om the JMP statement itself. Positive for a forward reference Negative for a backward reference Target addressdisplacementaddr. of next instruction Target address label is contained in jump instruction. Example: Jmp forever,Displacement size,short relative jump(短转移) Displacement is a single byte nea

7、r relative format(近转移) 32-bit displacement,Indirect jump(间接转移),Use a 32-bit address for the target. Address is stored in a register or in a memory doubleword Example:,jmp edx ;edx-EIP Target dword 98098912h jmp Target ; target-EIP jmp DWORD PTR ebx ; ebx-EIP,5.2 Conditional JUMP (条件转移),J- targetStat

8、ement ;target address-EIP - identifies the condition under which the jump is to be executed. If the condition holds, then the jump takes place; Otherwise, the next instruction is executed. targetStatement must be relative addressing,Conditional jump instructions do not modify the flags; they only re

9、act to previously set flag values.,Example,add value to balance; if balance0 then design for negative banlance elseif balance=0 then design for zro balance Else design for positive balance End if;,add balance , eax jns elseIfZero jmp endBalanceCheck elseIfZero: jnz elsePos jmp endBanlanceCheck elseP

10、os: endBanlanceCheck:,Note the begin and end of selective structure.,Set or reset flag,Set flag(标志位置位) Give the value 1 to a flag Reset/clear flag(标志位复位) Give the value 0 to a flag Compare instructions are the most common way to establish flag values.,compare instruction,CMP operand1 , operand2 Calc

11、ulating operand1 minus operand2, like a SUB instruction Set flags but do not change operand1 Addressing mode is the same as SUB.,Example,if eax100 then jump to Bigger;,cmp eax, 100 ja Bigger ;(1) jg Bigger ;(2),Jump or not? EAX=00000000H (2) NOT JUMP EAX=80000000H JUMP NOT JUMP,IF structure,if value

12、10 then add 1 to smallCount; else add 1 to largetCount; endif,cmp ebx , 10; value 10 jnl elseLarge inc smallCount jmp endValueCheck elaseLarge: inc lartgeCout endValueCheck:,IF structure(2),if (total=100) or (count=10) then add value to total; endif,cmp total , 100; total =100? jge addValue cmp cx,

13、10; count=100? jne endAddCheck addValue: mov ebx, value; copy value add total , ebx ; add value to total endAddCheck:,IF structure(3),if (count0) and (ch=backspace) then subtract 1 from count; endif,cmp cx , 0 ; count0? jng endCheckCh cmp al , backspace ; ch a backspace? jne endCheckCh dec count ; s

14、ubtract 1 from count endCheckCh:,5.3 Implementing Loop Structures,Loop structures include while, until and for loops. Use jump instructions Use loop instructions,while continuation condition loop .body of loop end while;,for index:=initialValue to finalValue loop .body of loop end for;,until termina

15、tion condition loop .body of loop end until;,while loop structure,while (sum1000) loop body of loop end while;,whileSum: cmp sum , 1000 ; sum1000? jnl endWhileSum ;exit loop if not jmp whileSum ; go check condition again endWhileSum:,while loop structure(2),X:=0; twoToX:=1; while twoToX=number multi

16、ply twoToX by 2; add 1 to x; end while; subtract 1 from x,mov cx, 0 mov eax, 1 whileLE: cmp eax, number jnle endWhileLE Body: add eax, eax inc cx jmp whileLE endWhileLE: dec cx,while loop structure(3),while (sum1000) and (count=24) loop body of loop end while;,whileSum: cmp sum , 1000 ; sum1000? jnl

17、 endWhileSum ;exit loop if not cmp cx , 24 ; count=24 jnle endWhileSum ; exit if not ;body of loop jmp whileSum ; go check condition again endWhileSum:,while loop structure(4),while (sum1000) or (flag=1) loop body of loop end while;,whileSum: cmp eax , 1000 ; sum1000? jl body ;execute bbody if so cm

18、p dh , 1 ; flag=1? jne endWhileSum ; exit if not Body: ;body of loop jmp whileSum ; go check condition again endWhileSum:,while loop structure(5),sum:=0 while (number keyed in is not negative) loop add number to sum; end while;,mov ebx , 0 whileNotNeg: output prompt input number ,10 atod number js e

19、ndwhile add ebx , eax jmp whileSum endWhile:,For loop structure,Index:=initialValue while index =finalValue loop .body of loop add 1 to index; end while;,for index:=initialValue to finalValue loop .body of loop end for;,for loop structure,Prompt for tally of numbers; Input tally; Sun:=0 For count:=1

20、 to tally loop Prompt for number; Input number; Add number to sum; End for ;,output prompt1 input value , 20 atoi value mov tally , ax mov edx, 0 ; sum:=0 mov bx, 1; count:=1,forCount: cmp bx, tally jnle endfor; exit if not output prompt2 input value, 20 atod value add edx, eax inc bx jmp forCount ;

21、repeat endFor:,until loop structure,Count :=0; Until (sum1000) or (count =100) loop . body of loop Add 1 to count; End until;,mov cx,0;count :=0 Until: ;body of loop inccx;add 1 to count cmp sum, 1000; sum1000? jg endUntil; exit if sum 1000 cmp cx , 100; count=100? jne until ; continue if count not

22、=100 endUntil:,Loop instructions,Loop statementLabel statementLabel is the label of a statement that is a short displacement from the loop instruction. ECX -1-ECX if ECX =0, then execute the statement following the looop instruction if ECX !=0, then a jump to the instruction at statementLabel takes

23、place,For loop structure,for count:=20 downto 1 loop body of loop end for,mov ecx,20;number of iterations 循环次数 forCount: . . ;body of loop loop forCount ; repeat body 20 times,For loop structure,mov ecx,number;number of iterations 循环次数 cmp ecx , 0 je/jecxz endFor; skip loop if number=0 forIndex: . ;

24、body of loop loop forIndex ; repeat body number times,How many times would repeat if ECX=0?,232=4294967296,For loop structure,for counter := 50 downto 1 loop .body of loop end for;,mov ecx , 50; number of iterations forCount: ; body of loop dec ecx;decrement loop counter jecxz endfor; exit if counte

25、r =0 jmp forCounter ; otherwise repeat body,mov ecx , 50; number of iterations forCount: .; body of loop loop forCount; repeat body 20 times,For loop structure,for index:=1 to 50 loop body of loop end for,mov ecx,50;number of iterations 循环次数 mov ebx , 1 ; index:=1 forCount: . ;body of loop inc ebx l

26、oop forCount ; repeat body 20 times,Conditional loop,loopz/loope if ECX!=0 and ZF=1 then loop again loopnz/loopne if ECX!=0 and ZF=0 then loop again,For loop structure,for year:=10 downto 1 until balance=0 loop body of loop end for,mov ecx,10;maximum number of iterations forYear: . ;body of loop cmp

27、 ebx , 0 ;balance=0? loopne forYear ; repeat body 20 times,Other instructions,lea destination, source destination will normally be a 32-bit register; source is any reference to memory the address of the source is loaded into the register MOV destination , OFFSET source jecxz targetstatement jump if

28、ecx=0,Game program,untilDone: output prompt1 ; ask player 1 for target input stringIn, 20 ; get number atod stringIn ; convert to integer mov target,eax ; store target output clear ; clear screen mov cx, 0 ; zero count,Game program(2),untilMatch: inc cx ; increment count of guesses output prompt2 ;

29、ask player 2 for guess input stringIn, 20 ; get number atod stringIn ; convert to integer cmp eax, target ; compare guess and target jne ifLess ; guess = target ?,Game program(3),equal: output gotItOutput ; display you got it jmp endCompare ifLess: jnl isGreater ; guess target ? output lowOutput ; d

30、isplay too low jmp endCompare isGreater: output highOutput ; display too high,Game program(4),endCompare: cmp eax, target ; compare guess and target jne untilMatch ; ask again if guess not = target itoa countOut, cx ; convert count to ASCII output countLabel ; display label, count and prompt input s

31、tringIn, 20 ; get response cmp stringIn, n ; response = n ? je endUntilDone ; exit if so cmp stringIn, N ; response = N ? jne untilDone ; repeat if not endUntilDone:,Program using array,; input a collection of numbers ; report their average and the numbers which are ;above average output directions

32、; display directions mov nbrElts,0 ; nbrElts := 0 lea ebx,nbrArray ; get address of nbrArray,Program using array,whilePos: output prompt ; prompt for number input number,20 ; get number atod number ; convert to integer jng endWhile ; exit if not positive mov ebx,eax ; store number in array inc nbrElts ; add 1 to nbrElts add ebx,4 ; get address of next item of array jmp whilePos

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论