Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,"— Presentation transcript:

1 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 (yohanes@cs.ui.ac.id) Bobby Nazief (nazief@cs.ui.ac.id) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

2 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 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 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 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 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 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 8 Conditional Jump: signed °Assume comparison was conducted to signed numbers

9 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 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 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 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 0111 0011+ 1010 1100 1011+ 0111 110 7 3 – 6 –4 – 5 7 0

13 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 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 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 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 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 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 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 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 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 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 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, 0 20. je end_while_factor ; if !(guess % factor != 0) 21. add ebx,2 ; factor += 2; 22. jmp while_factor

24 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 += 2 10. jmp while_limit 11.end_while_limit: 12. popa 13. mov eax, 0 ; return back to C 14. leave 15. ret

25 25 prime.exe


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

Similar presentations


Ads by Google