1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 07: CALL, RET, Stacks Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Conditional Processing
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Conditional Processing If … then … else While … do; Repeat … until.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
(Flow Control Instructions)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 ICS 51 Introductory Computer Organization Fall 2009.
26-Nov-15 (1) CSC Computer Organization Lecture 6: Pentium IA-32.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Chapter 5 Branching and Looping.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Assembly Language Wei Gao. Assembler language Instructions.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Practical Session 2 Computer Architecture and Assembly Language.
CSC 221 Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Practical Session 2.
CSC 221 Computer Organization and Assembly Language
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Basic Assembly Language
Assembly IA-32.
Assembly Language Lab (4).
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
CS 301 Fall 2002 Control Structures
Computer Organization and Assembly Language
Flags Register & Jump Instruction
Practical Session 2.
Lecture 1 Instruction set of 8086 Лектор: Люличева И.А. 1.
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Presentation transcript:

1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization, ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 24 Maret 2004 L. Yohanes Stefanus Bobby Nazief bahan kuliah:

2 Control Transfer Instructions °The processor provides both: conditional transfers -taken only for specified states of the status flags in the EFLAGS register unconditional transfers -always executed

3 Control Transfer Instructions: Summary °JMP Jump °JE/JZ Jump if equal/Jump if zero °JNE/JNZ Jump if not equal/Jump if not zero °JA/JNBE Jump if above/Jump if not below or equal °JAE/JNB Jump if above or equal/Jump if not below °JB/JNAE Jump if below/Jump if not above or equal °JBE/JNA Jump if below or equal/Jump if not above °JG/JNLE Jump if greater/Jump if not less or equal °JGE/JNL Jump if greater or equal/Jump if not less °JL/JNGE Jump if less/Jump if not greater or equal °JLE/JNG Jump if less or equal/Jump if not greater °JC Jump if carry °JNC Jump if not carry °JO Jump if overflow °JNO Jump if not overflow °JS Jump if sign (negative) °JNS Jump if not sign (non-negative) °JPO/JNP Jump if parity odd/Jump if not parity °JPE/JP Jump if parity even/Jump if parity °JCXZ/JECXZ Jump register CX zero/Jump register ECX zero °LOOP Loop with ECX counter °LOOPZ/LOOPE Loop with ECX and zero/Loop with ECX and equal °LOOPNZ/LOOPNE Loop with ECX and not zero/Loop with ECX and not equal

4 JMP °The JMP (jump) instruction unconditionally transfers program control to a destination instruction. °A destination operand specifies the address (the instruction pointer) of the destination instruction. °The address can be a relative address or an absolute address. A relative address is a displacement (offset) with respect to the address in the EIP register. -The destination address (a near pointer) is formed by adding the displacement to the address in the EIP register. -The displacement is specified with a signed integer, allowing jumps either forward or backward in the instruction stream. An absolute address is a offset from address 0 of a segment. It can be specified in either of the following ways: -An address in a general-purpose register -An address specified using the standard addressing modes of the processor.

5 Format Instruksi JMP °JMP rel8 Jump short, relative, displacement relative to next instruction °JMP rel16 Jump near, relative, displacement relative to next instruction °JMP rel32 Jump near, relative, displacement relative to next instruction °JMP r/m16 Jump near, absolute indirect, address given in r/m16 °JMP r/m32 Jump near, absolute indirect, address given in r/m32 °JMP ptr16:16 Jump far, absolute, address given in operand °JMP ptr16:32 Jump far, absolute, address given in operand °JMP m16:16 Jump far, absolute indirect, address given in m16:16 °JMP m16:32 Jump far, absolute indirect, address given in m16:32

6 Type of Target Addresses °Short jump—A near jump where the jump range is limited to –128 to +127 from the current EIP value. To specify a short jump, use the SHORT keyword immediately before the label in the JMP instruction °Near jump—A jump to an instruction within the current code segment (the segment currently pointed to by the CS register), sometimes referred to as an intrasegment jump. The two byte type can be specified by putting the WORD keyword before the label in the JMP instruction °Far jump—A jump to an instruction located in a different segment than the current code segment but at the same privilege level, sometimes referred to as an intersegment jump. °Task switch—A jump to an instruction located in a different task.

7 Conditional Jump: unsigned °The Jcc (conditional) jump instructions transfer program control to a destination instruction if the conditions specified with the condition code (cc) associated with the instruction are satisfied.

8 Conditional Jump: signed °Assume comparison was conducted to signed numbers

9 Format Instruksi: JCC °JA rel8 Jump short if above (CF=0 and ZF=0) °JAE rel8 Jump short if above or equal (CF=0) °JB rel8 Jump short if below (CF=1) °JBE rel8 Jump short if below or equal (CF=1 or ZF=1) °JC rel8 Jump short if carry (CF=1) °JNC rel8 Jump short if not carry (CF=0) °JE rel8 Jump short if equal (ZF=1) °JNE rel8 Jump short if not equal (ZF=0) °JZ rel8 Jump short if zero (ZF = 1) °JNZ rel8 Jump short if not zero (ZF=0) °JA rel16/32 Jump near if above (CF=0 and ZF=0) °JAE rel16/32 Jump near if above or equal (CF=0) °JB rel16/32 Jump near if below (CF=1) °JBE rel16/32 Jump near if below or equal (CF=1 or ZF=1) °JC rel16/32 Jump near if carry (CF=1) °JNC rel16/32 Jump near if not carry (CF=0) °JE rel16/32 Jump near if equal (ZF=1) °JNE rel16/32 Jump near if not equal (ZF=0) °JZ rel16/32 Jump near if 0 (ZF=1) °JNZ rel16/32 Jump near if not zero (ZF=0)

10 Simple Condition °the following pseudo-code: if ( EAX == 0 ) EBX = 1; else EBX = 2; °could be written in assembly as: 1. cmp eax, 0 ; set flags (ZF set if eax - 0 = 0) 2. jz thenblock ; if ZF is set branch to thenblock 3. mov ebx, 2 ; ELSE part of IF 4. jmp next ; jump over THEN part of IF 5.thenblock: 6. mov ebx, 1 ; THEN part of IF 7.next:

11 Complex Condition (v1) °consider the following pseudo-code: if ( EAX >= 5 ) EBX = 1; else EBX = 2; °here is assembly code that tests for these conditions (assuming that EAX is signed): 1. cmp eax, 5 2. js signon ; goto signon if SF = 1 3. jo elseblock ; goto elseblock if OF = 1 and SF = 0 4. jmp thenblock ; goto thenblock if SF = 0 and OF = 0 5.signon: 6. jo thenblock ; goto thenblock if SF = 1 and OF = 1 7.elseblock: 8. mov ebx, 2 9. jmp next 10.thenblock: 11. mov ebx, 1 12.next :

12 Review: Overflow Detection °Overflow: the result is too large (or too small) to represent properly Example: - 8 < = 4-bit binary number <= 7 °When adding operands with different signs, overflow cannot occur! °Overflow occurs when adding: 2 positive numbers and the sum is negative 2 negative numbers and the sum is positive °Overflow can be detected by evaluating: Carry into MSB  Carry out of MSB – 6 –4 – 5 7 0

13 Complex Condition (v2) °consider the following pseudo-code: if ( EAX >= 5 ) EBX = 1; else EBX = 2; °here is assembly code that tests for these conditions (assuming that EAX is signed): 1. cmp eax, 5 2. jge thenblock 3. mov ebx, 2 4. jmp next 5.thenblock: 6. mov ebx, 1 7.next:

14 Instruksi Loop °LOOP: decrements the contents of the ECX register (or the CX register, if the address-size attribute is 16), then tests the register for the loop-termination condition if the count in the ECX register is non-zero, program control is transferred to the instruction address specified by the destination operand when the count in the ECX register reaches zero, program control is transferred to the instruction immediately following the LOOP instruction, which terminates the loop °LOOPE (loop while equal) & LOOPZ (loop while zero): same as LOOP, they also test the ZF flag. If the count in the ECX register is not zero and the ZF flag is set, program control is transferred to the destination operand. When the count reaches zero or the ZF flag is clear, the loop is terminated by transferring program control to the instruction immediately following the LOOPE/LOOPZ instruction. °LOOPNE (loop while not equal) & LOOPNZ (loop while not zero) instructions operate the same as the LOOPE/LOOPPZ instructions, except that they terminate the loop if the ZF flag is set.

15 JCXZ & JECXZ °The JECXZ (jump if ECX zero) instruction jumps to the location specified in the destination operand if the ECX register contains the value zero. °JCXZ (jump if CX is zero) instruction operates the same as the JECXZ instruction using CX register instead.

16 Format Instruksi: JCXZ, JECXZ, LOOP, LOOPCC °JCXZ rel8 Jump short if CX register is 0 °JECXZ rel8 Jump short if ECX register is 0 °LOOP rel8 Decrement count; jump short if count ≠ 0 °LOOPE rel8 Decrement count; jump short if count ≠ 0 and ZF=1 °LOOPZ rel8 Decrement count; jump short if count ≠ 0 and ZF=1 °LOOPNE rel8 Decrement count; jump short if count ≠ 0 and ZF=0 °LOOPNZ rel8 Decrement count; jump short if count ≠ 0 and ZF=0

17 Loop °the following pseudo-code: sum = 0; for ( i=10; i >0; i-- ) sum += i; °could be translated into assembly as: 1. mov eax, 0; eax is sum 2. mov ecx, 10; ecx is i 3.loop_start: 4. add eax, ecx 5. loop loop_start

18 IF Statement °The following pseudo-code: if ( condition ) then_block ; else else_block ; °could be implemented as: 1. ; code to set FLAGS 2. jxx else_block ; select xx so that branches if ; condition false 3. ; code for then_block 4. jmp endif 5.else_block: 6. ; code for else_block 7.endif: if there is no ‘else’: 1. ; code to set FLAGS 2. jxx endif; select xx so that branches if ; condition false 3. ; code for then_block 4.endif:

19 WHILE Loop °The while loop is a top tested loop: while( condition ) { body of loop; } °This could be translated into: 1.while: 2. ; code to set FLAGS based on condition 3. jxx endwhile ; select xx so that branches if false 4. ; body of loop 5. jmp while 6.endwhile:

20 DO WHILE Loop °The do while loop is a bottom tested loop: do { body of loop; } while( condition ); °This could be translated into: 1.do: 2. ; body of loop 3. ; code to set FLAGS based on condition 4. jxx do ; select xx so that branches if true

21 ~prime.c #include int main() { unsigned guess;/* current guess for prime */ unsigned factor;/* possible factor of guess */ unsigned limit;/* find primes up to this value */ printf("Find primes up to: "); scanf("%u", &limit); printf("2\n");/* treat first two primes as special case */ printf("3\n"); guess = 5;/* initial guess */ while ( guess <= limit ) { /* look for a factor of guess */ factor = 3; while ( factor*factor < guess && guess % factor != 0 ) factor += 2; if ( guess % factor != 0 ) printf("%d\n", guess); guess += 2; /* only look at odd numbers */ } return 0; }

22 Contoh: prime.asm (1/3) 1.%include "asm_io.inc" 2.segment.data 3.Message db "Find primes up to: ", 0 4.segment.bss 5.Limit resd 1 ; find primes up to this limit 6.Guess resd 1 ; the current guess for prime 7.segment.text 8. global _asm_main 9._asm_main: 10. enter 0,0 ; setup routine 11. pusha 12. mov eax, Message 13. call print_string 14. call read_int ; scanf("%u", & limit ); 15. mov [Limit], eax 16. mov eax, 2 ; printf("2\n"); 17. call print_int 18. call print_nl

23 Contoh: prime.asm (2/3) 1. mov eax, 3 ; printf("3\n"); 2. call print_int 3. call print_nl 4. mov dword [Guess], 5 ; Guess = 5; 5.while_limit: ; while ( Guess <= Limit ) 6. mov eax,[Guess] 7. cmp eax, [Limit] 8. jnbe end_while_limit ; use jnbe since numbers are ; unsigned 9. mov ebx, 3 ; ebx is factor = 3; 10.while_factor: 11. mov eax,ebx 12. mul eax ; edx:eax = eax*eax 13. jo end_while_factor ; if answer won't fit in eax alone 14. cmp eax, [Guess] 15. jnb end_while_factor ; if !(factor*factor < guess) 16. mov eax,[Guess] 17. mov edx,0 18. div ebx ; edx = edx:eax % ebx 19. cmp edx, je end_while_factor ; if !(guess % factor != 0) 21. add ebx,2 ; factor += 2; 22. jmp while_factor

24 Contoh: prime.asm (3/3) 1.end_while_factor: 2. je end_if ; if !(guess % factor != 0) 3. mov eax,[Guess] ; printf("%u\n") 4. call print_int 5. call print_nl 6.end_if: 7. mov eax,[Guess] 8. add eax, 2 9. mov [Guess], eax ; guess += jmp while_limit 11.end_while_limit: 12. popa 13. mov eax, 0 ; return back to C 14. leave 15. ret

25 prime.exe