cs33-machine_programming_control

cs33-machine_programming_control - Machine-Level...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Some notes adopted from Bryant and O’Hallaron Machine-Level Programming: Control Flow Chapter 3 of B&O
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Condition Codes • Single Bit Registers – CF Carry Flag SF Sign Flag – ZF Zero Flag OF Overflow Flag • Implicitly Set By 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 • Not Set by leal instruction
Background image of page 2
Setting Condition Codes (cont.) • 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)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Setting Condition Codes (cont.) • Explicit Setting by Test instruction – testl Src2,Src1 • Useful to have one of the operands be a mask – testl b,a like computing a&b without setting destination – SF set when a&b < 0
Background image of page 4
Reading Condition Codes • SetX Instructions – Set single byte 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 Greater (Signed) setge ~(SF^OF) Greater or Equal (Signed) setl (SF^OF) Less (Signed) setle (SF^OF)|ZF Less or Equal (Signed) seta Above (unsigned) setb CF Below (unsigned)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reading Condition Codes (Cont.) • SetX Instructions – Set single byte based on combinations of condition codes – One of 8 addressable byte registers • Embedded within first 4 integer registers • Does not alter remaining 3 bytes • Typically use movzbl to finish job %eax %edx %ecx %ebx %esi %edi %esp %ebp %al %ah %dl %dh %cl %ch %bl %bh int gt (int x, int y) { return x > y; } movl 12(%ebp),%eax # eax = y cmpl %eax,8(%ebp) # Compare x : y setg %al # al = x > y movzbl %al,%eax # Zero rest of %eax Note inverted ordering! Body
Background image of page 6
Reading Condition Codes: x86-64 • SetX Instructions: – Set single byte based on combination of condition codes – Does not alter remaining 3 bytes int gt (long x, long y) { return x > y; } xorl %eax, %eax # eax = 0 cmpq %rsi, %rdi # Compare x and y setg %al # al = x > y Body (same for both) long lgt (long x, long y) { return x > y; } Is %rax zero? Yes: 32-bit instructions set high order 32 bits to 0!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja Above (unsigned) jb CF Below (unsigned)
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

cs33-machine_programming_control - Machine-Level...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online