Presentation is loading. Please wait.

Presentation is loading. Please wait.

תרגול 5 תכנות באסמבלי, המשך

Similar presentations


Presentation on theme: "תרגול 5 תכנות באסמבלי, המשך"— Presentation transcript:

1 תרגול 5 תכנות באסמבלי, המשך
Assembly תרגול 5 תכנות באסמבלי, המשך

2 Condition Codes Single bit registers
CF – carry flag. Unsigned overflow. ZF – zero flag. Zero result. SF – sign flag. Negative result. OF – overflow flag. Signed overflow. 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
Specific instructions that alter only the condition codes. test command only changes ZF and SF

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) || (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 . Sets only ZF and SF others get 0. ZF set when a&b == 0 SF set when a&b < 0

6 Accessing the Condition Codes
D - one of the small 8 bit registers

7 Why does it work? Let’s take cmpl b,a for example
If there is no overflow and a≥b  SF=0, OF=0 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);

9 a < b in assembly Translate the line: return (a<b); Solution :
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

10 Jump Instructions

11 Unconditional Jump - always jumps ! Dangerous !
Direct jump - to label jmp L1 Indirect jump jmp *%eax - value inside register as jump to address jmp *(%eax) - value inside register as address containing the jump to address

12 Conditional Jump Can’t use indirect jump - must jump to label and not to address or register value Use it to implement if conditions loops switch statements Easiest to understand by converting to “go to” format.

13 Goto in C

14 If Condition in Assembly

15 If Condition in Assembly

16 Do-While Loops

17 Do-While Loops in Assembly

18 While Loops

19 Exercise Initially Variable Register %eax %ebx %ecx %edx

20 Exercise’s Solution Initially Variable Register a %eax b %ebx i %ecx
i %ecx result %edx

21 .p2align 4,,7- a command to the assembler that says I want the following command to start at an address that divides by 16 (2^4), while not wasting more than 7 bytes in order to achieve this. Inserts zeros at the end. Usually done for commands that we will use a lot - make sure they sit in an efficient place in memory. This alignment often improves cache hits (which reduces memory latency), as spatially local memory will end up in the same cache line / memory page.

22 Exercise’s Solution Note the optimization done by the compiler!
עד כאן עם הקבוצה הראשונה Note the optimization done by the compiler!

23 For Loops

24 Exercise

25 Exercise’s Solution Note the optimization done by the compiler!

26 Exercise’s Solution Note the optimization done by the compiler!
( Removal of y*x out of the loop )

27 Switch Statements in C

28 Switch Statements in Assembly
Building the jump table in the read only data section :

29 Switch Statements in Assembly
Calculates the case as the address I will jump to. Each table entry has to be the same size for easy address calculation. Have to have some kind of value for each entry ( can have no missing entries - missing ones will get a default value ). Will jump to : ‘index’ * 4 + initial table address.

30 Switch Statements in Assembly

31 Switch Statements in Assembly
עד כאן עם הקבוצה השניה

32 Exercise

33 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


Download ppt "תרגול 5 תכנות באסמבלי, המשך"

Similar presentations


Ads by Google