1 / 25

IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures

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

kagami
Download Presentation

IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IKI10230Pengantar Organisasi KomputerKuliah no. 06: Control Structures Sumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. 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. Control Transfer Instructions • The processor provides both: • conditional transfers • taken only for specified states of thestatus 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 specifiesthe address (the instruction pointer) of the destination instruction. • The address can be arelative address or an absolute address. • A relative address is a displacement (offset) with respect to the address in the EIP register. • Thedestination address (a near pointer) is formed by adding the displacement to the address in theEIP register. • The displacement is specified with a signed integer, allowing jumps either forwardor backward in the instruction stream. • An absolute address is a offset from address 0 of a segment. It can be specified in either of thefollowing ways: • An address in a general-purpose register • An address specified using the standard addressing modes of the processor.

  5. Format Instruksi JMP • JMP rel8Jump short, relative, displacement relative to next instruction • JMP rel16Jump near, relative, displacement relative to next instruction • JMP rel32Jump near, relative, displacement relative to next instruction • JMP r/m16Jump near, absolute indirect, address given in r/m16 • JMP r/m32Jump near, absolute indirect, address given in r/m32 • JMP ptr16:16Jump far, absolute, address given in operand • JMP ptr16:32Jump far, absolute, address given in operand • JMP m16:16Jump far, absolute indirect, address given in m16:16 • JMP m16:32Jump 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 currentEIP value. • To specifya short jump, use the SHORT keyword immediately before the label inthe JMP instruction • Near jump—A jump to an instruction within the current code segment (the segmentcurrently pointed to by the CS register), sometimes referred to as an intrasegment jump. • The two byte type can be specified by putting the WORD keywordbefore the label in the JMP instruction • Far jump—A jump to an instruction located in a different segment than the current codesegment 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 rel8Jump short if above (CF=0 and ZF=0) • JAE rel8Jump short if above or equal (CF=0) • JB rel8Jump short if below (CF=1) • JBE rel8Jump short if below or equal (CF=1 or ZF=1) • JC rel8Jump short if carry (CF=1) • JNC rel8Jump short if not carry (CF=0) • JE rel8Jump short if equal (ZF=1) • JNE rel8Jump short if not equal (ZF=0) • JZ rel8Jump short if zero (ZF = 1) • JNZ rel8Jump short if not zero (ZF=0) • JA rel16/32Jump near if above (CF=0 and ZF=0) • JAE rel16/32Jump near if above or equal (CF=0) • JB rel16/32Jump near if below (CF=1) • JBE rel16/32Jump near if below or equal (CF=1 or ZF=1) • JC rel16/32Jump near if carry (CF=1) • JNC rel16/32Jump near if not carry (CF=0) • JE rel16/32Jump near if equal (ZF=1) • JNE rel16/32Jump near if not equal (ZF=0) • JZ rel16/32Jump near if 0 (ZF=1) • JNZ rel16/32Jump 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: • cmp eax, 0; set flags (ZF set if eax - 0 = 0) • jz thenblock ; if ZF is set branch to thenblock • mov ebx, 2 ; ELSE part of IF • jmp next ; jump over THEN part of IF • thenblock: • mov ebx, 1 ; THEN part of IF • 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): • cmp eax, 5 • js signon ; goto signon if SF = 1 • jo elseblock ; goto elseblock if OF = 1 and SF = 0 • jmp thenblock ; goto thenblock if SF = 0 and OF = 0 • signon: • jo thenblock ; goto thenblock if SF = 1 and OF = 1 • elseblock: • mov ebx, 2 • jmp next • thenblock: • mov ebx, 1 • next:

  12. 0 1 1 0 0 1 1 1 7 1 1 0 0 –4 3 – 5 + 0 0 1 1 + 1 0 1 1 1 0 1 0 – 6 0 1 1 1 7 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 • Overflowcan be detected by evaluating: • Carry into MSB  Carry out of MSB

  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): • cmp eax, 5 • jge thenblock • mov ebx, 2 • jmp next • thenblock: • mov ebx, 1 • next:

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

  15. JCXZ & JECXZ • The JECXZ (jump if ECX zero) instruction jumps to the location specified in the destinationoperand 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 rel8Jump short if CX register is 0 • JECXZ rel8Jump short if ECX register is 0 • LOOP rel8Decrement count; jump short if count ≠ 0 • LOOPE rel8Decrement count; jump short if count ≠ 0 and ZF=1 • LOOPZ rel8Decrement count; jump short if count ≠ 0 and ZF=1 • LOOPNE rel8Decrement count; jump short if count ≠ 0 and ZF=0 • LOOPNZ rel8Decrement count; jump short if count ≠ 0 and ZF=0

  17. Loop • thefollowing pseudo-code: sum = 0; for ( i=10; i >0; i-- ) sum += i; • could be translated into assembly as: • mov eax, 0; eax is sum • mov ecx, 10; ecx is i • loop_start: • add eax, ecx • loop loop_start

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

  19. WHILE Loop • The while loop is a top tested loop: while( condition ) { body of loop; } • This could be translated into: • while: • ; code to set FLAGS based on condition • jxx endwhile ; select xx so that branches if false • ; body of loop • jmp while • 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: • do: • ; body of loop • ; code to set FLAGS based on condition • jxx do ; select xx so that branches if true

  21. ~prime.c #include <stdio.h> 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) • %include "asm_io.inc" • segment .data • Message db "Find primes up to: ", 0 • segment .bss • Limit resd 1 ; find primes up to this limit • Guess resd 1 ; the current guess for prime • segment .text • global _asm_main • _asm_main: • enter 0,0 ; setup routine • pusha • mov eax, Message • call print_string • call read_int ; scanf("%u", & limit ); • mov [Limit], eax • mov eax, 2 ; printf("2\n"); • call print_int • call print_nl

  23. Contoh: prime.asm (2/3) • mov eax, 3 ; printf("3\n"); • call print_int • call print_nl • mov dword [Guess], 5 ; Guess = 5; • while_limit: ; while ( Guess <= Limit ) • mov eax,[Guess] • cmp eax, [Limit] • jnbe end_while_limit; use jnbe since numbersare ; unsigned • mov ebx, 3 ; ebx is factor = 3; • while_factor: • mov eax,ebx • mul eax ; edx:eax = eax*eax • jo end_while_factor ; if answer won't fit in eax alone • cmp eax, [Guess] • jnb end_while_factor ; if !(factor*factor < guess) • mov eax,[Guess] • mov edx,0 • div ebx ; edx = edx:eax % ebx • cmp edx, 0 • je end_while_factor ; if !(guess % factor != 0) • add ebx,2 ; factor += 2; • jmp while_factor

  24. Contoh: prime.asm (3/3) • end_while_factor: • je end_if ; if !(guess % factor != 0) • mov eax,[Guess] ; printf("%u\n") • call print_int • call print_nl • end_if: • mov eax,[Guess] • add eax, 2 • mov [Guess], eax ; guess += 2 • jmp while_limit • end_while_limit: • popa • mov eax, 0 ; return back to C • leave • ret

  25. prime.exe

More Related