1 / 28

Conditional Codes

Conditional Codes. Outline. Conditional Codes Accessing the Conditional Codes Suggested reading Chap 3.6.1,3.6.2. FFFFFFFF. C0000000. BFFFFFFF. Stack. 80 000000. Heap. 7 FFFFFFF. 40 000000. DLLs. 3 FFFFFFF. Heap. Data. Text. 08 000000. 00 000000. Memory.

joy
Download Presentation

Conditional Codes

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. Conditional Codes

  2. Outline • Conditional Codes • Accessing the Conditional Codes • Suggested reading • Chap 3.6.1,3.6.2

  3. FFFFFFFF C0000000 BFFFFFFF Stack 80000000 Heap 7FFFFFFF 40000000 DLLs 3FFFFFFF Heap Data Text 08000000 00000000 Memory Assembly Programmer’s View %eax Addresses %edx %ecx Data %ebx %esi %edi Instructions • %esp • %ebp CPU %eip %eflags

  4. Condition codes • Condition codes • A set of single-bit • Maintained in a condition code register • Describe attributes of the most recently arithmetic or logical operation

  5. Condition codes • EFLAGS • CF: Carry Flag • The most recent operation generated a carry out of the most significant bit • Used to detect overflow for unsigned operations • OF: Overflow Flag • The most recent operation caused a two’s complement overflow — either negative or positive

  6. Condition codes • EFLAGS • ZF: Zero Flag • The most recent operation yielded zero • SF: Sign Flag • The most recent operation yielded a negative value

  7. Setting Conditional Codes • Implicit SettingBy Arithmetic Operations addl Src,Dest C analog: t = a+b • CF set if carry out from most significant bit • Used to detect unsigned overflow • ZF set if t == 0 • SF set if t < 0 • OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

  8. Conditional Code • lea instruction • has no effect on condition codes • Xorl instruction • The carry and overflow flags are set to 0 • Shift instruction • carry flag is set to the last bit shifted out • Overflow flag is set to 0

  9. Setting Conditional Codes • Explicit Setting by Compare Instruction • cmpl 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 • OF set if two’s complement overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

  10. Setting Conditional Codes • Explicit Setting by Test instruction • testl Src2,Src1 • Sets condition codes based on value of Src1&Src2 • Useful to have one of the operands be a mask • testl b,a like computing a&b without setting destination • ZF set when a&b == 0 • SF set when a&b < 0

  11. Accessing Conditional Codes • The condition codes cannot be read directly • One of the most common methods of accessing them is • setting an integer register based on some combination of condition codes • Set commands

  12. Accessing Conditional Codes • After each set command is executed • A single byte to 0 or to 1 is obtained • The descriptions of the different set commands apply to the case • where a comparison instruction has been executed

  13. Accessing Conditional Codes

  14. Accessing Conditional Codes • The destination operand is either • one of the eight single-byte register elements or • a memory location where the single byte is to be stored • To generate a 32-bit result • we must also clear the high-order 24 bits

  15. Accessing Conditional Codes Initially a is in %edx, b is in %eax 1 cmpl %eax, %edx #compare a:b 2 setl %al #set low order by to 0 or 1 3 movzbl %al, %eax #set remaining bytes of %eax to 0

  16. Control

  17. Outline • Jump instructions • Translate Control constructs in C to assembly • goto • branch • Loop • Suggested reading • Chap 3.6

  18. Control • Two of the most important parts of program execution • Data flow (Accessing and operating data) • Control flow (control the sequence of operations)

  19. Control • Sequential execution is default • the instructions are executed in the order they appear in the program • Chang the control flow • Jump instructions

  20. Jump Instructions 1 xorl %eax, %eax Set %eax to 0 2 jmp .L1 Goto .L1 3 movl (%eax), %edx Null pointer dereference 4 .L1: 5 popl %edx

  21. Unconditional jump • Jumps unconditionally • Direct jump: jmp label • jmp .L • Indirect jump: jmp *Operand • jmp *%eax • jmp *(%eax)

  22. Conditional jump • Either jump or continue executing at the next instruction in the code sequence • Depending on some combination of the condition codes • Such as jl, jge, jb, … etc. • All direct jump

  23. Jump Instructions • jle .L2 • .L5: • movl %edx, %eax • sarl $1, %eax • subl %eax, %edx • Leal (%edx, %edx, 2), %edx • testl %edx, %edx • jg .L5 9 .L2: 10 movl %edx, %eax

  24. Control Constructs in C • Gotos • goto L • break • continue • Branch • if () { } else { } • switch () { }

  25. Control Constructs in C • Loop • while () { } • do { } while () • for (init; test; incr) { }

  26. Translating Conditional Branches t = test-expr ; if ( t ) goto true ; else-statement goto done true: then-statement done: if ( test-expr ) then-statement else else-statement

  27. int absdiff(int x, int y) • { • if (x < y) • return y – x; • else • return x – y; • } • int absdiff(int x, int y) • { • int rval ; • if (x < y) • goto less • rval = x – y ; • goto done; • less: • rval = y – x; • done: • return rval; • } Translating Conditional Branches

  28. Jump Instructions • movl 8(%ebp), %edx get x • movl 12(%ebp), %eax get y • cmpl %eax, %edx cal x - y • jl .L3 if x < y goto less • subl %eax, %edx compute x - y • movl %edx, %eax set return val • jmp .L5 goto done • .L3: less: • subl %edx, %eax compute y – x • .L5: done: Begin Completion code

More Related