Lecture Computer organization and assembly language: Chapter 21 - Dr. Safdar Hussain Bouk

pptx
Số trang Lecture Computer organization and assembly language: Chapter 21 - Dr. Safdar Hussain Bouk 16 Cỡ tệp Lecture Computer organization and assembly language: Chapter 21 - Dr. Safdar Hussain Bouk 220 KB Lượt tải Lecture Computer organization and assembly language: Chapter 21 - Dr. Safdar Hussain Bouk 0 Lượt đọc Lecture Computer organization and assembly language: Chapter 21 - Dr. Safdar Hussain Bouk 0
Đánh giá Lecture Computer organization and assembly language: Chapter 21 - Dr. Safdar Hussain Bouk
4.7 ( 9 lượt)
Nhấn vào bên dưới để tải tài liệu
Để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs Lecture 20: Review BT (Bit Test) Instruction • Copies bit n from an operand into the Carry flag • Syntax: BT bitBase, n – bitBase may be r/m16 or r/m32 – n may be r16, r32, or imm8 Lecture 20: Review (cont.) LOOPZ (LOOPE) • Syntax: LOOPE/LOOPZ destination • Logic: ECX  ECX – 1 | if ECX > 0 and ZF=1, jump to destination • Useful when scanning an array for the first element that does not match a given value. LOOPNZ (LOOPNE) • Syntax: LOOPNZ/LOOPNE destination • Logic: ECX  ECX – 1; if ECX > 0 and ZF=0, jump to destination • Useful when scanning an array for the first element that matches a given value. Lecture 20: Review Conditional Structures • Block-Structured IF Statements • Compound Expressions with AND • Compound Expressions with OR • WHILE Loops • REPEAT Loops (cont.) Block-Structured IF Statements Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example: if( op1 == op2 ) X = 1; else X = 2; mov cmp jne mov jmp L1: mov L2: eax,op1 eax,op2 L1 X,1 L2 X,2 Compound Expression with AND • When implementing the logical AND operator, consider that HLLs use short-circuit evaluation • In the following example, if the first expression is false, the second expression is skipped: if (al > bl) AND (bl > cl) X = 1; cmp ja jmp L1: cmp ja jmp L2: mov next: al,bl L1 next ; first expression... bl,cl L2 next ; second expression... X,1 ; both are true ; set X to 1 Compound Expression with OR (1 of 2) • When implementing the logical OR operator, consider that HLLs use short-circuit evaluation • In the following example, if the first expression is true, the second expression is skipped: if (al > bl) OR (bl > cl) X = 1; cmp ja cmp jbe L1: next: al,bl L1 bl,cl next ; is AL > BL? ; yes ; no: is BL > CL? ; no: skip next statement mov X,1 ; set X to 1 WHILE Loops A WHILE loop is really an IF statement followed by the body of the loop, followed by an unconditional jump to the top of the loop. Consider the following example: while( eax < ebx) eax = eax + 1; This is a possible implementation: top: loop condition jae next inc eax jmp top next: cmp eax,ebx ; check ; false? exit loop ; body of loop ; repeat the loop Using the .IF Directive • • • • • Runtime Expressions Relational and Logical Operators MASM-Generated Code .REPEAT Directive .WHILE Directive Runtime Expressions • .IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate runtime expressions and create block-structured IF statements. • Examples: .IF eax > ebx mov edx,1 .ELSE mov edx,2 .ENDIF .IF eax > ebx && eax > ecx mov edx,1 .ELSE mov edx,2 .ENDIF • MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump instructions. .REPEAT Directive Executes the loop body before testing the loop condition associated with the .UNTIL directive. Example: ; Display integers 1 – 10: mov eax,0 .REPEAT inc eax call WriteDec call Crlf .UNTIL eax == 10 .WHILE Directive Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop. Example: ; Display integers 1 – 10: mov eax,0 .WHILE eax < 10 inc eax call WriteDec call Crlf .ENDW LET’s ENJOY ASSEMBLY LANGUAGE Summary ASSEMBLY IMPLEMENTATION OF: • Bit Test Instruction – Copies bit n from an operand into the Carry flag – Syntax: BT bitBase, n • Conditional LOOP Instructions – LOOPZ and LOOPE LOOPZ/LOOPE destination • Logic: – ECX  ECX – 1 – if ECX > 0 and ZF=1, jump to destination – LOOPNZ and LOOPNE LOOPZ/LOOPE destination • Logic: – ECX  ECX – 1 – if ECX > 0 and ZF=0, jump to destination Summary ASSEMBLY IMPLEMENTATION OF: • Block Structures – Block-Structured IF Statements – Compound Expressions with AND – Compound Expressions with OR – WHILE Loops – REPEAT Loops (cont.) Reference Most of the Slides are taken from Presentation: Chapter 6 Assembly Language for Intel-Based Computers, 4th Edition Kip R. Irvine (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.