Upcoming SlideShare
×

# Chapter2b

246 views
197 views

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
246
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Chapter2b

1. 1. Group Activity 1. printf(“%c”,x); 2. sum = x + y; 3. prod = x * y; (assume x and y are byte-size variables)
2. 2. Group Activity 1. printf(“%c”,x); mov eax, 4 mov ebx, 1 mov ecx, x mov edx, 1 int 80h
3. 3. Group Activity 2. sum = x + y; mov bl, [x] add bl, [y] mov [sum], bl
4. 4. Group Activity 3. prod = x * y; mov al, [x] mul byte[y] mov [prod], ax
5. 5. Structured Assembly LanguageProgramming TechniquesControl Transfer Instructions
6. 6. Control Transfer Instructions  allows program control to transfer to specified label  Unconditional or Conditional  Unconditional – executed without regards to any situation or condition in the program – transfer of control goes from one code to another by force jmp label – unconditional jump
7. 7. Control Transfer Instructions mov al, 5 next: add [num1],al mov eax, 4 jmp next mov ebx, 1 mov ecx, num2 mov eax, 4 mov edx, 1 mov ebx, 1 int 80h mov ecx, num1 mov edx, 1 int 80h (1) (2)
8. 8. Control Transfer Instructions  Conditional – a jump carried out on the basis of a truth value – the information on which such decisions are based is contained in the flags registers
9. 9. Boolean Expressions  evaluates to True or False  compares two values  cmp source1, source2  Source1 may be a register or memory  Source2 may be a register, memory or immediate  Operands cannot be both memory.  Operands must be of the same size.
10. 10. Conditional Jumps  usually placed after a cmp instruction conditional_jump label  JE – branches if source1 == source2  JNE – branches if source1 ≠ source2
11. 11. Conditional Jumps  Signed Conditional Jump – JL or JNGE  branches if source1 < source2 – JLE or JNG  branches if source1 ≤ source2 – JG or JNLE  branches if source1 > source2 – JGE or JNL  branches if source1 ≥ source2
12. 12. Conditional Jumps  Unsigned Conditional Jumps – JB or JNAE  branches if source1 < source2 – JBE or JNA  branches if source1 ≤ source2 – JA or JNBE  branches if source1 > source2 – JAE or JNB  branches if source1 ≥ source2
13. 13. Signed or Unsigned mov al, FFh 1 1 1 1 1 1 1 1 cmp al, 10 jb label  FFh == 255  will not jump to label
14. 14. Signed or Unsigned mov al, FFh 1 1 1 1 1 1 1 1 cmp al, 10 jl label - 0 0 0 0 0 0 1  FFh == -1  will jump to label
15. 15. Control Structure: IF Statement if (boolean) cmp AX, CX {statements;} jg if_statement jmp next_statement if(AX>CX){ BX = DX + 2; if_statement: } add DX, 2 mov BX, DX next_statement: ...
16. 16. Better Design: Save on Jumps cmp AX, CX cmp AX, CX jg if_statement jng next_statement jmp next_statement if_statement: if_statement: add DX, 2 add DX, 2 mov BX, DX mov BX, DX next_statement: next_statement: ... ...
17. 17. Control Structure: IF-ELSE Statement if (boolean) cmp AX, CX {statements;} jg if_statement else jmp else_statement if_statement: {statements;} add DX, 2 mov BX, DX if(AX>CX){ jmp next_statement BX = DX + 2; else_statement: } else { sub DX, 2 mov BX, DX BX = DX – 2; next_statement: } ...
18. 18. Better Design: Save on Jumps cmp AX, CX cmp AX, CX jg if_statement jng else_statement jmp else_statement if_statement: if_statement: add DX, 2 add DX, 2 mov BX, DX mov BX, DX jmp next_statement jmp next_statement else_statement: else_statement: sub DX, 2 sub DX, 2 mov BX, DX mov BX, DX next_statement: next_statement: ... ...
19. 19. Compound Boolean Expressions  ANDed expressions – P and Q – True if and only if both expressions are True  ORed expressions – P or Q – False if and only if both expressions are False
20. 20. Short Circuited Evaluation  ANDed Expressions – if the first expression is False, there is no need to check the second expression.  ORed Expressions – If the first expression if True, there is no need to check the second expression.
21. 21. Short Circuited Evaluation  P and Q if (P == FALSE) then proceed to ELSE-part else if (Q == FALSE) then proceed to ELSE-part else proceed to THEN-part
22. 22. Short Circuited Evaluation  P or Q if (P == TRUE) then proceed to THEN-part else if (Q == TRUE) then proceed to THEN-part else proceed to ELSE-part
23. 23. ANDed Expressions if(AX >= 100) && cmp AX, 100 jge other_cond (AX <=120) { jmp else_stmt BX = AX; other_cond: } else { cmp AX, 120 jle if_stmt BX = CX; jmp else_stmt } if_stmt: mov BX, AX jmp next_stmt else_stmt: mov BX, CX next_stmt: ...
24. 24. ANDed Expressions if(AX >= 100) && cmp AX, 100 (AX <=120) { jnge else_part BX = AX; cmp AX, 120 } else { jnle else_part then_part: BX = CX; mov BX, AX } jmp next_part else_part: mov BX, CX next_part: ...
25. 25. ORed Expressions if(AX < 100) || cmp AX, 100 (AX > 120) { jl then_part BX = CX; cmp AX, 120 } else { jng else_part BX = AX; then_part: } mov BX, CX jmp next_part else_part: mov BX, AX next_part: ...
26. 26. Group Activity if ((al>=bl) && (al<10)){ printf(“%s”,msg1); } else { printf(“%s”,msg2); } cl=bl;