the hardware software interface
play

The Hardware/Software Interface CSE351 Spring 2013 x86 Programming - PowerPoint PPT Presentation

University of Washington The Hardware/Software Interface CSE351 Spring 2013 x86 Programming II University of Washington Todays Topics: control flow Condition codes Conditional and unconditional branches Loops 2 University of


  1. University of Washington The Hardware/Software Interface CSE351 Spring 2013 x86 Programming II

  2. University of Washington Today’s Topics: control flow  Condition codes  Conditional and unconditional branches  Loops 2

  3. University of Washington Conditionals and Control Flow  A conditional branch is sufficient to implement most control flow constructs offered in higher level languages if (condition) then {...} else {…}  while (condition ) {…}  do {…} while (condition)  for (initialization; condition; iterative) {...}   Unconditional branches implement some related control flow constructs break, continue   In x86, we’ll refer to branches as “jumps” (either conditional or unconditional) 3

  4. University of Washington Jumping  jX Instructions  Jump to different part of code depending on condition codes jX Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) 4

  5. University of Washington Processor State (IA32, Partial) %eax  Information about currently executing %ecx program %edx General purpose  Temporary data %ebx registers ( %eax , …) %esi  Location of runtime %edi stack ( %ebp,%esp ) %esp Current stack top  Location of current %ebp Current stack frame code control point ( %eip ) %eip Instruction pointer  Status of recent tests ( CF,ZF,SF,OF ) CF ZF SF OF Condition codes 5

  6. University of Washington Condition Codes (Implicit Setting)  Single-bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed)  Implicitly set (think of it as side effect) by arithmetic operations Example: addl/addq Src,Dest ↔ t = a+b  CF set if carry out from most significant bit (unsigned overflow)  ZF set if t == 0  SF set if t < 0 (as signed)  OF set if two’s complement (signed) overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)  Not set by lea instruction (beware!)  Full documentation (IA32): http://www.jegerlehner.ch/intel/IntelCodeTable.pdf 6

  7. University of Washington Condition Codes (Explicit Setting: Compare)  Single-bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed)  Explicit Setting by Compare Instruction cmpl/cmpq Src2 , Src1 cmpl b,a like computing a-b without setting destination  CF set if carry out from most significant bit (used for unsigned comparisons)  ZF set if a == b  SF set if (a-b) < 0 (as signed)  OF set if two’s complement (signed) overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) 7

  8. University of Washington Condition Codes (Explicit Setting: Test)  Single-bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed)  Explicit Setting by Test instruction testl / testq Src2 , Src1 testl b,a like computing a & b without setting destination  Sets condition codes based on value of Src1 & Src2  Useful to have one of the operands be a mask  ZF set if a&b == 0  SF set if a&b < 0  testl %eax, %eax  Sets SF and ZF, check if eax is +,0,- 8

  9. University of Washington Reading Condition Codes  SetX Instructions  Set a single byte to 0 or 1 based on combinations of condition codes SetX Condition Description sete ZF Equal / Zero setne ~ZF Not Equal / Not Zero sets SF Negative setns ~SF Nonnegative setg ~(SF^OF)&~ZF Greater (Signed) setge ~(SF^OF) Greater or Equal (Signed) setl (SF^OF) Less (Signed) setle (SF^OF)|ZF Less or Equal (Signed) seta ~CF&~ZF Above (unsigned) setb CF Below (unsigned) 9

  10. University of Washington Reading Condition Codes (Cont.)  SetX Instructions: %eax %ah %al Set single byte to 0 or 1 based on combination of %ecx %ch %cl condition codes %edx %dh %dl  One of 8 addressable byte registers  Does not alter remaining 3 bytes %ebx %bh %bl  Typically use movzbl to finish job %esi int gt (int x, int y) %edi { %esp return x > y; } %ebp Body: y at 12(%ebp), x at 8(%ebp) movl 12(%ebp),%eax # eax = y What does each of cmpl %eax,8(%ebp) # Compare x : y setg %al # al = x > y these instructions do? movzbl %al,%eax # Zero rest of %eax 10

  11. University of Washington Reading Condition Codes (Cont.)  SetX Instructions: %eax %ah %al Set single byte to 0 or 1 based on combination of %ecx %ch %cl condition codes %edx %dh %dl  One of 8 addressable byte registers  Does not alter remaining 3 bytes %ebx %bh %bl  Typically use movzbl to finish job %esi int gt (int x, int y) %edi { %esp return x > y; } %ebp Body: y at 12(%ebp), x at 8(%ebp) movl 12(%ebp),%eax # eax = y cmpl %eax,8(%ebp) # Compare x and y (x – y) setg %al # al = x > y movzbl %al,%eax # Zero rest of %eax 11

  12. University of Washington Jumping  jX Instructions  Jump to different part of code depending on condition codes jX Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) 12

  13. University of Washington Conditional Branch Example absdiff: int absdiff(int x, int y) pushl %ebp { Setup movl %esp, %ebp int result; movl 8(%ebp), %edx if (x > y) { movl 12(%ebp), %eax result = x-y; cmpl %eax, %edx } else { Body1 jle .L7 result = y-x; subl %eax, %edx } movl %edx, %eax return result; .L8: } leave Finish ret .L7: subl %edx, %eax Body2 jmp .L8 13

  14. University of Washington Conditional Branch Example (Cont.) int goto_ad(int x, int y) int absdiff(int x, int y) { { int result; int result; if (x <= y) goto Else ; if (x > y) { result = x-y; result = x-y; Exit : } else { return result; result = y-x; Else: } result = y-x; return result; goto Exit ; } } C allows “ goto ” as means of  transferring control  Closer to machine-level programming style Generally considered bad coding  style 14

  15. University of Washington Conditional Branch Example (Cont.) absdiff: int goto_ad(int x, int y) pushl %ebp { movl %esp, %ebp int result; movl 8(%ebp), %edx if (x <= y) goto Else ; movl 12(%ebp), %eax result = x-y; cmpl %eax, %edx Exit : jle .L7 return result; subl %eax, %edx Else: movl %edx, %eax result = y-x; .L8: goto Exit ; leave } ret .L7: subl %edx, %eax int x %edx jmp .L8 int y %eax 15

  16. University of Washington Conditional Branch Example (Cont.) absdiff: int goto_ad(int x, int y) pushl %ebp { movl %esp, %ebp int result; movl 8(%ebp), %edx if (x <= y) goto Else ; movl 12(%ebp), %eax result = x-y; cmpl %eax, %edx Exit : jle .L7 return result; subl %eax, %edx Else: movl %edx, %eax result = y-x; .L8: goto Exit ; leave } ret .L7: subl %edx, %eax int x %edx jmp .L8 int y %eax 16

  17. University of Washington Conditional Branch Example (Cont.) absdiff: int goto_ad(int x, int y) pushl %ebp { movl %esp, %ebp int result; movl 8(%ebp), %edx if (x <= y) goto Else ; movl 12(%ebp), %eax result = x-y; cmpl %eax, %edx Exit : jle .L7 return result; subl %eax, %edx Else: movl %edx, %eax result = y-x; .L8: goto Exit ; leave } ret .L7: subl %edx, %eax int x %edx jmp .L8 int y %eax 17

  18. University of Washington Conditional Branch Example (Cont.) absdiff: int goto_ad(int x, int y) pushl %ebp { movl %esp, %ebp int result; movl 8(%ebp), %edx if (x <= y) goto Else ; movl 12(%ebp), %eax result = x-y; cmpl %eax, %edx Exit : jle .L7 return result; subl %eax, %edx Else: movl %edx, %eax result = y-x; .L8: goto Exit ; leave } ret .L7: subl %edx, %eax int x %edx jmp .L8 int y %eax 18

  19. University of Washington Conditional Branch Example (Cont.) absdiff: int goto_ad(int x, int y) pushl %ebp { movl %esp, %ebp int result; movl 8(%ebp), %edx if (x <= y) goto Else ; movl 12(%ebp), %eax result = x-y; cmpl %eax, %edx Exit : jle .L7 return result; subl %eax, %edx Else: movl %edx, %eax result = y-x; .L8: goto Exit ; leave } ret .L7: subl %edx, %eax int x %edx jmp .L8 int y %eax 19

  20. University of Washington 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;  Test is expression returning integer Goto Version = 0 interpreted as false  0 interpreted as true nt = !Test ; if (nt) goto Else ;  Create separate code regions for val = Then-Expr ; then & else expressions Done:  Execute appropriate one . . . Else: val = Else-Expr ;  How might you make this more goto Done; efficient? 20

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