Download presentation
Presentation is loading. Please wait.
Published byKaren Norman Modified over 8 years ago
1
Assembly תרגול 7 תכנות באסמבלי, המשך
2
Condition Codes Single bit registers CF – carry flag ZF – zero flag SF – sign flag OF – overflow flag Relevant only for the most recent operation leal does not alter any condition code In logical operations, CF and OF are set to 0 For shift operations, OF is set to 0, CF is the last shifted out bit
3
Setting Condition Codes
4
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)
5
Setting Condition Codes (cont.) 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
6
Accessing the Condition Codes
7
Why does it work? Let’s take setl b,a for example If there is no overflow and a≥b SF=0, OF=1 If there is no overflow and a<b SF=1, OF=0 If there is a negative overflow (a>b) SF=1, OF=1 If there is a positive overflow (a<b) SF=0, OF=1
8
a < b in assembly Translate the line: return (a<b); Suppose a is in %edx, b is in %eax: cmpl %eax,%edx # compare a to b setl %al # set %al to 0 or 1 movzbl %al,%eax # set %eax to 0 or 1
9
Jump Instructions
10
Unconditional Jump Direct jump jmp L1 Indirect jump jmp *%eax jmp *(%eax)
11
Conditional Jump Can’t use indirect jump Use it to implement if conditions loops switch statements
12
Goto in C
13
If Condition in Assembly
14
Do-While Loops
15
Do-While Loops in Assembly
16
While Loops
17
Exercise InitiallyVariableRegister %eax %ebx %ecx %edx
18
Exercise’s Solution InitiallyVariableRegister aa%eax bb%ebx 0i%ecx aresult%edx
19
Exercise’s Solution Note the optimization done by the compiler!
20
For Loops
21
Exercise
22
Exercise’s Solution Note the optimization done by the compiler!
23
Switch Statements in C
24
Switch Statements in Assembly Building the jump table:
25
Switch Statements in Assembly
27
Exercise
28
Q & A Q: What were the values of the case labels in the switch statement body? What cases had multiple labels in the C code? A: The case labels had values -2,0,1,2,3,4 The case with the labels 2 and 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.