control flow conditionals and control flow
play

Control flow Conditionals and Control Flow l A conditional - PowerPoint PPT Presentation

10/2/15 Control flow Conditionals and Control Flow l A conditional branch is sufficient to implement most control Condition codes flow constructs offered in higher


  1. 10/2/15 Control ¡flow Conditionals ¡and ¡Control ¡Flow l A ¡conditional ¡branch ¡is ¡sufficient ¡to ¡implement ¡ most ¡control ¡ Condition ¡codes flow ¡constructs ¡offered ¡in ¡higher ¡level ¡languages Conditional ¡ and ¡unconditional ¡jumps l if (condition) then {...} else {…} Loops l while (condition) {…} Switch ¡statements l do {…} while (condition) l for (initialization; condition; iterative) {...} l Unconditional ¡ branches ¡implement ¡ related ¡control ¡flow ¡ constructs l break, continue l x86 ¡calls ¡branches ¡“jumps” (conditional ¡or ¡unconditional) 1 2 Jumping Processor ¡State ¡(IA32, ¡Partial) jX Instructions Info ¡about ¡current ¡ %eax executing ¡program Jump ¡to ¡different ¡part ¡of ¡code ¡depending ¡on ¡condition ¡codes Takes ¡address ¡as ¡argument %ecx Temporary ¡ data ( ¡ %eax , ¡…) %edx General ¡purpose Location ¡of ¡stack registers jX Condition Description %ebx ( ¡ %ebp,%esp ) Unconditional jmp 1 %esi Location ¡of ¡next ¡instruction Equal ¡/ ¡Zero je ZF ( ¡ %eip ) %edi jne ~ZF Not ¡Equal ¡/ ¡Not ¡Zero Negative js SF Status ¡of ¡recent ¡ tests Current ¡stack ¡top %esp ( ¡ CF,ZF,SF,OF ) Nonnegative jns ~SF Current ¡stack ¡frame %ebp Greater ¡(Signed) jg ~(SF^OF)&~ZF Greater ¡or ¡Equal ¡(Signed) jge ~(SF^OF) jl (SF^OF) Less ¡(Signed) Instruction ¡pointer %eip Less ¡or ¡Equal ¡(Signed) jle (SF^OF)|ZF Above ¡(unsigned) ja ~CF&~ZF Condition ¡codes CF ZF SF OF Below ¡ (unsigned) jb CF 3 4 1

  2. 10/2/15 Condition ¡Codes (flags) cmp : compare ¡values ¡with ¡subtraction Single-­‑bit ¡ registers Single-­‑bit ¡ registers CF Carry ¡Flag ¡(for ¡unsigned) SF Sign ¡Flag ¡(for ¡signed) CF Carry ¡Flag ¡(for ¡unsigned) SF Sign ¡Flag ¡(for ¡signed) ZF Zero ¡Flag OF Overflow ¡Flag ¡(for ¡signed) ZF Zero ¡Flag OF Overflow ¡Flag ¡(for ¡signed) Arithmetic ¡ instructions ¡set ¡flags ¡implicitly Compare ¡instructions ¡set ¡flags Example: addl/addq Src,Dest ↔ t cmpl/cmpq Src2 , Src1 = a+b CF ¡set if ¡carry ¡out ¡from ¡most ¡significant ¡bit ¡(unsigned ¡overflow) cmpl b,a computes a-b, discards ¡result ZF ¡set ¡ if ¡ t CF ¡set ¡ if ¡carry ¡out ¡from ¡most ¡significant ¡bit ¡(used ¡for ¡unsigned ¡comparisons) == 0 SF ¡set ¡ if ¡ t < 0 (as ¡signed) ZF ¡set ¡ if ¡ a == b OF ¡set ¡ if ¡two’s ¡complement ¡(signed) ¡overflow SF ¡set ¡ if ¡ (a-b) 0 (as ¡signed) < sign(a) == sign(b) && sign(b) != sign(t) OF ¡set ¡ if ¡two’s ¡complement ¡(signed) ¡overflow sign(a) == sign(b) && sign(b) != sign(t) LEA ¡does ¡not ¡set ¡flags. Full ¡documentation (IA32): ¡ http://www.jegerlehner.ch/intel/IntelCodeTable.pdf 5 6 test : compare ¡values ¡with ¡& Inspecting ¡Condition ¡Codes Single-­‑bit ¡ registers SetX Instructions CF Carry ¡Flag ¡(for ¡unsigned) SF Sign ¡Flag ¡(for ¡signed) Set a ¡single ¡byte ¡to ¡0 ¡or ¡1 ¡based ¡on ¡combinations ¡of ¡condition ¡codes ZF Zero ¡Flag OF Overflow ¡Flag ¡(for ¡signed) SetX Condition Description Test ¡instructions ¡set ¡flags sete ZF Equal ¡/ ¡Zero testl / testq Src2 , Src1 setne ~ZF Not ¡Equal ¡/ ¡Not ¡Zero testl b,a computes a & b , ¡discards ¡result ¡ sets SF Negative setns ~SF Sets ¡condition ¡codes ¡based ¡on ¡value ¡of ¡ Src1 & Src2 Nonnegative setg ~(SF^OF)&~ZF Greater ¡ (Signed) Useful ¡to ¡have ¡one ¡of ¡the ¡operands ¡be ¡a ¡ mask setge ~(SF^OF) Greater ¡ or ¡Equal ¡(Signed) ZF ¡set if ¡ a&b == 0 setl (SF^OF) Less ¡(Signed) SF ¡set if ¡ a&b < 0 setle (SF^OF)|ZF Less ¡or ¡Equal ¡(Signed) seta ~CF&~ZF Above ¡(unsigned) testl %eax, %eax setb CF Below ¡(unsigned) Sets ¡SF ¡and ¡ZF, ¡check ¡if ¡ %eax is ¡+,0,-­‑ 7 8 2

  3. 10/2/15 Inspecting ¡Condition ¡Codes Jump! SetX Instructions: ¡ jX Instructions %eax %ah %al Set ¡single ¡byte ¡to ¡0 ¡or ¡1 ¡based ¡on ¡combination ¡of ¡ Jump ¡to ¡different ¡part ¡of ¡code ¡depending ¡on ¡condition ¡codes condition ¡codes %ecx %ch %cl Takes ¡address ¡as ¡argument One ¡of ¡8 ¡addressable ¡byte ¡registers %edx %dh %dl Does ¡not ¡alter ¡remaining ¡3 ¡bytes %ebx %bh %bl jX Condition Description Typically ¡use ¡ movzbl to ¡finish ¡job Unconditional %esi jmp 1 z ero-­‑extend ¡ from ¡ b yte ¡(8 ¡bits) ¡to ¡ l ongword (32 ¡bits) Equal ¡/ ¡Zero je ZF %edi jne ~ZF Not ¡Equal ¡/ ¡Not ¡Zero int gt (int x, int y) { %esp Negative js SF return x > y; Nonnegative jns ~SF } %ebp Greater ¡(Signed) jg ~(SF^OF)&~ZF Body: ¡ y ¡at ¡12(%ebp), ¡x ¡at ¡8(%ebp) Greater ¡or ¡Equal ¡(Signed) jge ~(SF^OF) jl (SF^OF) Less ¡(Signed) movl 12(%ebp),%eax # eax = y Less ¡or ¡Equal ¡(Signed) jle (SF^OF)|ZF cmpl %eax,8(%ebp) # compare: x - y Above ¡(unsigned) ja ~CF&~ZF setg %al # al = x > y Below ¡ (unsigned) jb CF movzbl %al,%eax # zero rest of %eax 9 10 Conditional ¡Branch ¡Example Conditional ¡Branch ¡Example int goto_ad(int x, int y) { absdiff: int absdiff(int x, int y) { int absdiff(int x, int y) { int result; pushl %ebp Setup int result; int result; if (x <= y) goto Else ; movl %esp, %ebp if (x > y) { if (x > y) { result = x-y; movl 8(%ebp), %edx result = x-y; result = x-y; Exit : movl 12(%ebp), %eax } else { } else { return result; cmpl %eax, %edx Body1 result = y-x; result = y-x; Else: jle .L7 } } result = y-x; subl %eax, %edx return result; return result; goto Exit ; movl %edx, %eax } } } .L8: leave Finish ret C ¡allows ¡“goto” ¡as ¡means ¡of ¡ .L7: ¢ transferring ¡ control subl %edx, %eax Body2 § Closer ¡to ¡machine-­‑level ¡ jmp .L8 programming ¡style Bad ¡style ¢ 11 12 3

  4. 10/2/15 Conditional ¡Branch ¡Example absdiff: int goto_ad(int x, int y) { pushl %ebp int result; movl %esp, %ebp if (x <= y) goto Else ; movl 8(%ebp), %edx result = x-y; movl 12(%ebp), %eax Exit : cmpl %eax, %edx return result; jle .L7 Else: subl %eax, %edx result = y-x; movl %edx, %eax goto Exit ; .L8: } leave ret .L7: subl %edx, %eax http://xkcd.com/292/ int x %edx jmp .L8 int y %eax 13 14 Typical ¡control ¡flow ¡code ¡in ¡assembly General ¡Conditional ¡Expression ¡Translation if ( Test ) C ¡Code val = Then-­‑Expr ; val = Test ? Then-­‑Expr : Else-­‑Expr ; else val = Else-­‑Expr ; result = x>y ? x-y : y-x; cmpl %eax,%ebx je label Goto Version … nt = !T est ; Test is ¡expression ¡returning ¡integer … if (nt) goto Else ; val = Then-­‑Expr ; = ¡0 ¡interpreted ¡ as ¡false … Done: ≠ 0 ¡interpreted ¡as ¡true … label: . . . Create ¡separate ¡code ¡regions ¡for ¡ then ¡ an d ¡ Else: else expressions val = Else-­‑Expr ; Execute ¡appropriate ¡one goto Done; 19 21 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend