Analogue Electronics examInstructions:Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 1 Carnegie Mellon Assembly & C: Condi;onal Opera;ons Carnegie Mellon Instructors: Malcolm Heywood csci2122 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 2 Carnegie Mellon Today ! Control: Condi2on codes ! Condi2onal branches ! Loops Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 3 Carnegie Mellon Processor State (x86-64, Par2al) ! Informa2on about currently execu2ng program ! Temporary data ( %rax, … ) ! Loca;on of run;me stack ( %rsp ) ! Loca;on of current code control point ( %rip, … ) ! Status of recent tests ( CF, ZF, SF, OF ) %rip Registers Current stack top Instruc;on pointer CF ZF SF OF Condi;on codes %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 4 Condi2on Code Registers Flag Condi2on Code Carry (CF) Current opera;on generated a carry from MSB. Detects overflow for unsigned operand. Zero (ZF) Current opera;on generated a zero. Sign (SF) Current opera;on generated a nega;ve value. Overflow (OF) Current opera;on generated a two’s complement overflow. Could be posi;ve or nega;ve. Can be set implicitly or explicitly. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 5 Carnegie Mellon Condi2on Codes (Implicit SeGng) ! Implicitly set (think of it as side effect) by arithme2c opera2ons Example: 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 leaq instruc2on Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 6 Carnegie Mellon Condi2on Codes (Explicit SeGng: Compare) ! Explicit SeGng by Compare Instruc2on !cmpq Src2, Src1 !cmpq b,a like compu;ng a-b without seYng des;na;on !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) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 7 Carnegie Mellon Condi2on Codes (Explicit SeGng: Test) ! Explicit SeGng by Test instruc2on !testq Src2, Src1 !testq b,a like compu;ng a&b without seYng des;na;on !Sets condi;on codes based on value of Src1 & Src2 !Useful to have one of the operands be a mask !ZF set when a&b == 0 !SF set when a&b < 0 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 8 Carnegie Mellon Reading Condi2on Codes ! SetX Instruc2ons ! Query the outcome from: t = a –t w b ! i.e. two’s complement calcula;on with ‘w’ precision ! Set low-order byte of des;na;on to 0 or 1 based on combina;ons of condi;on codes SetX Condi;on Descrip;on sete ZF Equal / Zero setne ~ZF Not Equal / Not Zero sets SF Nega;ve setns ~SF Nonnega;ve 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) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 9 %rsp x86-64 Integer Registers ! Can reference low-order byte %al %bl %cl %dl %sil %dil %spl %bpl %r8b %r9b %r10b %r11b %r12b %r13b %r14b %r15b %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 10 Example 1: SET instruc2on int gt (long x, long y) { return x > y; } Comp: 1. cmpq %rsi, %rdi 2. setl %al 3. movzbl %al, %eax 4. ret Register Use(s) %rdi Argument x %rsi Argument y %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 11 Example 2: type associa2on int comp(data_t a, data_t b) { return a COMP b; } ! Let data_t be defined by ‘type_def’ and COMP defined by ‘#define’ ! Let a and b appear in (some part) of %rdx and %rsi respec;vely ! What type does ‘type_def’ assume for the following: 1. Cmpl %esi, %edi Setl %al 2. Cmpb %sil, %dil Setbe %al 3. Cmpb %rsi, %rdi Set ne %a Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 12 Carnegie Mellon Today ! Control: Condi2on codes ! Condi2onal branches ! Loops Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 13 Carnegie Mellon Jumping ! jX Instruc2ons ! Jump to different part of code depending on condi;on codes jX Condi;on Descrip;on jmp 1 Uncondi;onal je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Nega;ve jns ~SF Nonnega;ve 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) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 14 Carnegie Mellon Example: Condi2onal Branch long absdiff(long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } absdiff: cmpq %rsi, %rdi jle .L2 movq %rdi, %rax subq %rsi, %rax ret .L2: # x <= y movq %rsi, %rax subq %rdi, %rax ret ! Genera;on gcc –c absdiff.c ! Disassembly objdump –d absdiff.o Register Use(s) %rdi Argument x %rsi Argument y %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 15 Carnegie Mellon Expressing with Goto Code long absdiff(long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } ! C allows goto statement ! Jump to posi;on designated by label long absdiff_j(long x, long y) { long result; if (x <= y) goto Else; result = x-y; return result; Else: result = y-x; return result; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 16 Condi2onal Branches with Condi2onal Control General form of C ‘if-else’ if (test-expression) then-statement(s) else else-statement(s) Corresponding Assembly (C syntax for ctrl [and alternate]) t = test-expression; if (!t) [(t)] goto false; [true] then-statement(s) goto done; false: [true:] else-statement(s) Done: return-statement Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 17 Condi2onal Branches with Condi2onal Move General form of C condi2onal expression v = test-expr ? then-state : else-state; Assembly – Case 1 if (! test-expr) goto false; v = then-state; goto done; False: else-state; Done: return-state; Assembly – Case 2 v = then-state; ve = else-state; t = test-expr; if (!t) v = ve; Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 18 Carnegie Mellon Condi2onal Move Example absdiff: movq %rdi, %rax # x subq %rsi, %rax # result = x-y movq %rsi, %rdx subq %rdi, %rdx # eval = y-x cmpq %rsi, %rdi # compare x:y cmovle %rdx, %rax # if <=, result = eval ret long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 19 Carnegie Mellon Expensive Computa;ons Bad Cases for Condi2onal Move ! Both values get computed ! Only makes sense when computa2ons are very simple val = Test(x) ? Hard1(x) : Hard2(x); Risky Computa;ons ! Both values get computed ! May have undesirable effects val = p ? *p : 0; Computa;ons with side effects ! Both values get computed ! Must be side-effect free val = x > 0 ? x*=7 : x+=3; NB: the ‘condi;onal move’ structure with memory access poten;ally results in vulnerabili;es. See for example, ‘Spectre’ and ‘Meltdown’ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 20 Advantages of the ‘condi2onal move’ ! In a nutshell, ‘speed’ ! Register value is either wrinen over or not, that is the only difference between taken or not taken ! No change in program flow ! Penalty for branch miss predic;on is low ! Overhead of execu;ng ‘both paths’ of condi;on low ! Just a slot in instruc;on pipeline Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 21 Carnegie Mellon Today ! Control: Condi2on codes ! Condi2onal branches ! Loops Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 22 Carnegie Mellon C Code do Body while (Test-expr); Goto Version loop: Body t = Test-expr; if (t) goto loop General “Do-While” Transla2on ! Body: { Statement1; Statement2; … Statementn; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 23 Do-while example C code long dw_loop(long x) { long y = x*x; long *p = &x; long n = 2*x; do { x += y; (*p)++; n–; } while(n > 0); return x; } Assembly code 1. dw_loop: 2. movq %rdi, %rax 3. movq %rdi, %rcx 4. imulq %rdi, %rcx 5. leaq (%rdi,%rdi), %rdx 6. .L2: 7. leaq 1(%rcx,%rax), %rax 8. subq $1, %rdx 9. testq %rdx, %rdx 10.jg .L2 11.rep; ret Here’s ‘x’, but then what happens? Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 24 Carnegie Mellon C Code while (Test-expr) Body Goto Version goto test; loop: Body test: t = Test-expr; if (t) goto loop; General “While” Transla2on (1 of 2) – jump to middle version ! Body: { Statement1; Statement2; … Statementn; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 25 While ‘jump to middle’ example C code Long wloop(long a, long b) { long result = ………; while (…………) { result = …………; a = …………; } return result; } Assembly code 1. wloop: 2. movl $1, %eax 3. Jmp .L2 4. .L3: 5. Leaq (%rdi,%rsi), %rdx 6. imulq %rdx, %rax 7. Addq $1, %rdi 8. .L2 9. Cmpq %rsi, %rdi 10.jl .L3 11.rep; ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 26 Carnegie Mellon While version while (Test) Body Do-While Version if (!Test) goto done; do Body while(Test); done: General “While” Transla2on (2 of 2) – guarded do ! “Do-while” conversion ! Used with –O1 Goto Version if (!Test) goto done; loop: Body if (Test) goto loop; done: Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 27 While ‘guarded do’ example C code Long w2loop(long a, long b) { long result = ………; while (…………) { result = …………; a = …………; } return result; } Assembly code 1. w2loop: 2. testq %rsi, %rsi 3. jle .L8 4. movq %rsi, %rax 5. .L7: 6. imulq %rdi, %rax 7. subq %rdi, %rsi 8. testq %rsi, %rsi 9. jg .L7 10.rep; ret 11. .L8: 12.movq %rsi, %rax 13.ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 28 For loop for (init-expr; test-expr; update-expr) body-statement Jump-to-middle form init-expr; goto test; loop: body-statement; update-expr; test: t = test-expr; if (t) goto loop; Guarded-do form init-expr; t = test-expr; if (!t) goto done; loop: body-statement; update-expr; t = test-expr; if (t) goto loop; done: Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 29 While ‘guarded do’ example C code long something(long x) { long val = 0; long i; for (…………) { ………… } return val; } Assembly code 1. something: 2. movl $64, %edx 3. movl $0, %eax 4. .Loop: 5. movq %rdi, %rcx 6. andl $1, %ecx 7. addq %rax, %rax 8. orq %rcx, %rax 9. shrq %rdi 10.Subq $1, %rdx 11.jne .Loop 12.rep; ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 30 Carnegie Mellon Summarizing ! C Control ! if-then-else ! do-while ! while, for ! Assembler Control ! Condi;onal jump ! Condi;onal move ! Indirect jump (via jump tables) ! Compiler generates code sequence to implement more complex control ! Standard Techniques ! Loops converted to do-while or jump-to-middle form

Get Professional Assignment Help Cheaply

Are you busy and do not have time to handle your assignment? Are you scared that your paper will not make the grade? Do you have responsibilities that may hinder you from turning in your assignment on time? Are you tired and can barely handle your assignment? Are your grades inconsistent?

Whichever your reason may is, it is valid! You can get professional academic help from our service at affordable rates. We have a team of professional academic writers who can handle all your assignments.

Our essay writers are graduates with diplomas, bachelor’s, masters, Ph.D., and doctorate degrees in various subjects. The minimum requirement to be an essay writer with our essay writing service is to have a college diploma. When assigning your order, we match the paper subject with the area of specialization of the writer.

Why Choose Our Academic Writing Service?

 

Plagiarism free papers
Timely delivery
Any deadline
Skilled, Experienced Native English Writers
Subject-relevant academic writer
Adherence to paper instructions
Ability to tackle bulk assignments
Reasonable prices
24/7 Customer Support
Get superb grades consistently

How It Works

1.      Place an order

You fill all the paper instructions in the order form. Make sure you include all the helpful materials so that our academic writers can deliver the perfect paper. It will also help to eliminate unnecessary revisions.

2.      Pay for the order

Proceed to pay for the paper so that it can be assigned to one of our expert academic writers. The paper subject is matched with the writer’s area of specialization.

3.      Track the progress

You communicate with the writer and know about the progress of the paper. The client can ask the writer for drafts of the paper. The client can upload extra material and include additional instructions from the lecturer. Receive a paper.

4.      Download the paper

The paper is sent to your email and uploaded to your personal account. You also get a plagiarism report attached to your paper.

Get Professional Assignment Help Cheaply
Are you busy and do not have time to handle your assignment? Are you scared that your paper will not make the grade? Do you have responsibilities that may hinder you from turning in your assignment on time? Are you tired and can barely handle your assignment? Are your grades inconsistent?
Whichever your reason may is, it is valid! You can get professional academic help from our service at affordable rates. We have a team of professional academic writers who can handle all your assignments.
Our essay writers are graduates with diplomas, bachelor’s, masters, Ph.D., and doctorate degrees in various subjects. The minimum requirement to be an essay writer with our essay writing service is to have a college diploma. When assigning your order, we match the paper subject with the area of specialization of the writer.
Why Choose Our Academic Writing Service?

Plagiarism free papers
Timely delivery
Any deadline
Skilled, Experienced Native English Writers
Subject-relevant academic writer
Adherence to paper instructions
Ability to tackle bulk assignments
Reasonable prices
24/7 Customer Support
Get superb grades consistently

How It Works
1.      Place an order
You fill all the paper instructions in the order form. Make sure you include all the helpful materials so that our academic writers can deliver the perfect paper. It will also help to eliminate unnecessary revisions.
2.      Pay for the order
Proceed to pay for the paper so that it can be assigned to one of our expert academic writers. The paper subject is matched with the writer’s area of specialization.
3.      Track the progress
You communicate with the writer and know about the progress of the paper. The client can ask the writer for drafts of the paper. The client can upload extra material and include additional instructions from the lecturer. Receive a paper.
4.      Download the paper
The paper is sent to your email and uploaded to your personal account. You also get a plagiarism report attached to your paper.

 

PLACE THIS ORDER OR A SIMILAR ORDER WITH Essay fount TODAY AND GET AN AMAZING DISCOUNT

The post analogue electronics exam appeared first on Essay fount.


What Students Are Saying About Us

.......... Customer ID: 12*** | Rating: ⭐⭐⭐⭐⭐
"Honestly, I was afraid to send my paper to you, but you proved you are a trustworthy service. My essay was done in less than a day, and I received a brilliant piece. I didn’t even believe it was my essay at first 🙂 Great job, thank you!"

.......... Customer ID: 11***| Rating: ⭐⭐⭐⭐⭐
"This company is the best there is. They saved me so many times, I cannot even keep count. Now I recommend it to all my friends, and none of them have complained about it. The writers here are excellent."


"Order a custom Paper on Similar Assignment at essayfount.com! No Plagiarism! Enjoy 20% Discount!"