X86 university of washington dowhile loop example c

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: terpreted as false ≠0 interpreted as true   Create separate code regions for then & else expressions   Execute appropriate one nt = !Test; if (nt) goto Else; val = Then ­Expr; Done: ... Else: val = Else ­Expr; goto Done;   How might you make this more efficient? x86 University of Washington CondiMonals: x86 ­64 int absdiff( int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; }   absdiff: movl movl subl subl cmpl cmovle ret # x in %edi, y in %esi %edi, %eax # eax = x %esi, %edx # edx = y %esi, %eax # eax = x-y %edi, %edx # edx = y-x %esi, %edi # x:y %edx, %eax # eax=edx if <= CondiMonal move instrucMon         cmovC src, dest Move value from src to dest if condiIon C holds More efficient than condiIonal branching (simple control flow) But overhead: both branches are evaluated x86 University of Washington PC RelaMve Addressing 0x100 0x102 0x104 … 0x172    cmp je … … add r2, r3 0x70 r3, r4 0x1000 0x1002 0x1004 … 0x1072 PC relaMve branches are relocatable (same code works no maver where code is stored in memory)    Absolute branches are not (actual branch address encoded in instrucIon) x86 University of Washington Compiling Loops C/Java code: while ( sum != 0 ) { <loop body> } Machine code: loopTop: cmpl $0, %eax je loopDone <loop body code> jmp loopTop loopDone:   How to compile other loops should be straigh{orward   The only slightly tricky part is to be sure where the condiIonal branch occurs: top or bovom of the loop   How would for(i=0; i<100; i++) be implemented? x86 University of Washington “Do ­While” Loop Example C Code int fact_do(int Goto Version int fact_goto(int x) { { int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } }     x) Use backward branch to...
View Full Document

Ask a homework question - tutors are online