1 iki10230 pengantar organisasi komputer kuliah no. 06: control structures sumber: 1. paul carter,...
of 25
/25
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 ([email protected]) Bobby Nazief ([email protected]) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/
Embed Size (px)
TRANSCRIPT
AT90S8515Sumber:
2. Hamacher. Computer Organization, ed-5
3. Materi kuliah CS61C/2000 & CS152/1997, UCB
24 Maret 2004
conditional transfers
taken only for specified states of the status flags in the EFLAGS register
unconditional transfers
always executed
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
JO Jump if overflow
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
*
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
*
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
*
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.
*
Conditional Jump: unsigned
*
*
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)
*
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:
next:
EBX = 2;
here is assembly code that tests for these conditions (assuming that EAX is signed):
cmp eax, 5
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:
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:
Overflow can be detected by evaluating:
Carry into MSB Carry out of MSB
0
1
1
1
0
0
1
1
0
1
1
1
1
1
0
7
3
– 6
–4
– 5
7
0
Recalled from some earlier slides that the biggest positive number you can represent using 4-bit is 7 and the smallest negative you can represent is negative 8.
So any time your addition results in a number bigger than 7 or less than negative 8, you have an overflow.
Keep in mind is that whenever you try to add two numbers together that have different signs, that is adding a negative number to a positive number, overflow can NOT occur.
Overflow occurs when you to add two positive numbers together and the sum has a negative sign. Or, when you try to add negative numbers together and the sum has a positive sign.
If you spend some time, you can convince yourself that If the Carry into the most significant bit is NOT the same as the Carry coming out of the MSB, you have a overflow.
+2 = 41 min. (Y:21)
EBX = 2;
here is assembly code that tests for these conditions (assuming that EAX is signed):
cmp eax, 5
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.
*
JCXZ & JECXZ
*
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
*
sum += i;
loop_start:
; condition false
; code to set FLAGS
; condition false
while( condition ) {
while:
jxx endwhile ; select xx so that branches if false
; body of loop
do {
do:
jxx do ; select xx so that branches if true
*
unsigned limit; /* find primes up to this value */
printf("Find primes up to: ");
printf("3\n");
factor = 3;
factor += 2;
}
segment .bss
Guess resd 1 ; the current guess for prime
segment .text
global _asm_main
mov [Limit], eax
call print_int
call print_nl
call print_int
call print_nl
while_limit: ; while ( Guess <= Limit )
; unsigned
while_factor:
jo end_while_factor ; if answer won't fit in eax alone
cmp eax, [Guess]
mov eax,[Guess]
cmp edx, 0
add ebx,2 ; factor += 2;
call print_int
call print_nl
jmp while_limit
leave
ret
2. Hamacher. Computer Organization, ed-5
3. Materi kuliah CS61C/2000 & CS152/1997, UCB
24 Maret 2004
conditional transfers
taken only for specified states of the status flags in the EFLAGS register
unconditional transfers
always executed
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
JO Jump if overflow
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
*
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
*
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
*
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.
*
Conditional Jump: unsigned
*
*
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)
*
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:
next:
EBX = 2;
here is assembly code that tests for these conditions (assuming that EAX is signed):
cmp eax, 5
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:
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:
Overflow can be detected by evaluating:
Carry into MSB Carry out of MSB
0
1
1
1
0
0
1
1
0
1
1
1
1
1
0
7
3
– 6
–4
– 5
7
0
Recalled from some earlier slides that the biggest positive number you can represent using 4-bit is 7 and the smallest negative you can represent is negative 8.
So any time your addition results in a number bigger than 7 or less than negative 8, you have an overflow.
Keep in mind is that whenever you try to add two numbers together that have different signs, that is adding a negative number to a positive number, overflow can NOT occur.
Overflow occurs when you to add two positive numbers together and the sum has a negative sign. Or, when you try to add negative numbers together and the sum has a positive sign.
If you spend some time, you can convince yourself that If the Carry into the most significant bit is NOT the same as the Carry coming out of the MSB, you have a overflow.
+2 = 41 min. (Y:21)
EBX = 2;
here is assembly code that tests for these conditions (assuming that EAX is signed):
cmp eax, 5
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.
*
JCXZ & JECXZ
*
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
*
sum += i;
loop_start:
; condition false
; code to set FLAGS
; condition false
while( condition ) {
while:
jxx endwhile ; select xx so that branches if false
; body of loop
do {
do:
jxx do ; select xx so that branches if true
*
unsigned limit; /* find primes up to this value */
printf("Find primes up to: ");
printf("3\n");
factor = 3;
factor += 2;
}
segment .bss
Guess resd 1 ; the current guess for prime
segment .text
global _asm_main
mov [Limit], eax
call print_int
call print_nl
call print_int
call print_nl
while_limit: ; while ( Guess <= Limit )
; unsigned
while_factor:
jo end_while_factor ; if answer won't fit in eax alone
cmp eax, [Guess]
mov eax,[Guess]
cmp edx, 0
add ebx,2 ; factor += 2;
call print_int
call print_nl
jmp while_limit
leave
ret