Title: CE302 MICROPROCESSORS
1CE302MICROPROCESSORS
- Levent EREN
- Izmir University of Economics
2Outline
- Unconditional jump
- Conditional branching
- Construction of loops
3Unconditional JumpJMP
- Short jump 2-byte instruction that allows jumps
or branches to memory locations within 127 and
-128 bytes from the memory location following the
jump - JMP SHORT Label
- Near jump 3-byte instruction that allows jumps or
branches within /- 32Kb from the instruction in
the current code segment - JMP Label
- Far jump 5-byte instruction that allows a jump to
any memory location with in the entire memory
space - JMP Label
- For 80386, 80486, the near jump is within /-2G
if the machine operates in the protected mode and
/-32K bytes if operates in the real mode
OPCODE DISP
OPCODE DISP low DISP high
OPCODE IP low IP high CS low
CS high
4Conditional Branching
- Logic and arithmetic instructions set flags
- Flags provide state information from previous
instruction(s) - Using flags we can perform conditional jumping,
i.e., transfer program execution to some
different place within the program - if condition was true
- jump back or forward in your code to the location
specified - instruction pointer (IP) gets updated (to point
to the instruction to which execution will jump) - if condition was false
- continue execution at the following instruction
- IP gets incremented as usual
5Conditional Branching (cont.)
- Conditional jumps are always short jumps in the
8086-80286 - the range of the jump is 127 bytes and -128
bytes from the location following the conditional
jump - In 80386, 80486 conditional jumps are either
short or near jumps - Conditional jumps test sign (S), zero (Z), carry
(C), parity (P), and overflow (O) - Note
- an FFh is above the 00h in the set of unsigned
numbers - an FFh (-1) is less than 00h for signed numbers
- when you compare unsigned FFh is above
00h, but - signed FFh is less than 00h
6Numerical Comparison
- CMP(comparison) compares A to B
- a subtraction that only changes the flag bits
- useful for checking the entire contents of a
register or a memory location against another
value - usually followed by a conditional jump
instruction - CMP AL, 10h compare with 10h
(contents of AL does not change) - JAE SUBER if 10h or above then jump
to memory location SUBER - SUB (subtraction) calculates difference A - B
- saves results to A and set flags
7Numerical ComparisonCondition Code Settings
Unsigned Operands Signed operands Z
equality/inequality Z equality/inequality C
Oprnd1 lt Oprnd2 (C1) C no meaning
Oprnd1 gt Oprnd2 (C0) S no meaning S and O
taken together O no meaning If ((S0) and
(O1)) or ((S1) and (O0)) then Oprnd1 lt
Oprnd2 If ((S0) and (O0)) or ((S1) and
(O1)) then Oprnd1 gt Oprnd2
8Comparing Signed Integers
- Consider CMP AX,BX computed by the CPU
- The Sign bit (S) will be set if the result of
AX-BX has a 1 at the most significant bit of the
result (i.e., 15th bit for 16-bit op) - The Overflow flag (O) will be set if the result
of AX-BX produced a number that was out of range
(-32768 - 32767 for 16-bit numbers) to be
represented by an integer. - Difference in JS (jump on sign) and JL (jump less
than) - The conditional jump JS looks at the sign bit (S)
of the last compare (or subtraction). If S 1
then jump. - The conditional jump JL looks (S XOR O) of the
last compare (or subtraction) - REGARDLESS of the value AX-BX, i.e., even if
AX-BX causes overflow, the JL will be correctly
executed
9Comparing Signed Integers (cont.)
- JL is true if the condition S xor O is met
- JL is true for two conditions
- S1, O0
- (AX-BX) was negative and (AX-BX) did not overflow
- Example (8-bit)
- (-5) - (2) (-7)
- Result (-7) has the sign bit set
- Thus (-5) is less than (2).
10Comparing Signed Integers (cont.)
- S0, O1
- Overflow!, Sign bit of the result is wrong!
- Consider the following case
- AX is a large negative number (-)
- BX is a positive number ().
- The subtraction of (-) and () is the
same as the addition of (-) and (-) - The result causes negative overflow, and
thus cannot be represented as a signed integer
correctly (O1). - The result of AX-BX appears positive
(S0). - Example (8-bit) (-128) - (1) (127)
- Result (127) overflowed. Answer should have been
(-129). - Result appears positive, but overflow occurred
- Thus (-128) is less than (1), i.e., the condition
is TRUE for executing JL
11Comparing Signed IntegersCMP AX, BX
AX BX 2 (-4) 2 4 6 0110 So s 0, no
overflow (o 0) Therefore AX gt BX
AX BX 6 (-3) 6 3 9 1001 So s 1,
overflow (o 1) Therefore AX gt BX
AX BX 2 4 -2 1110 So s 1, no overflow
(o 0) Therefore AX lt BX
12Conditional Branching (cont.)
- Terminology used to differentiate between jump
instructions that use the carry flag and the
overflow flag - Above/Below unsigned compare
- Greater/Less signed (/-) compare
- Names of jump instructions
- J gt Jump
- N gt Not
- A/B G/L gt Above/Below Greater/Less
- E gt Equal
13Summary of Conditional Jump Instructions
- Command Description Condition
- JAJNBE Jump if above C0 Z0
- Jump if not below or equal
- JBEJNA Jump if below or equal C1 Z1
- JAEJNBJNC Jump if above or equal C0
- Jump if not below
- Jump if no carry
- JBJNAEJC Jump if below C1
- Jump if carry
- JEJZ Jump if equal Z1
- Jump if Zero
- JNEJNZ Jump if not equal Z0
- Jump if not zero
- JS Jump Sign (MSB1) S1
14Summary of Conditional Jump Instructions
- Command Description Condition
- JNS Jump Not Sign (MSB0) S0
- JO Jump if overflow set O1
- JNO Jump if no overflow
O0 - JGJNLE Jump if greater
- Jump if not less or equal
SO Z0 - JGEJNL Jump if greater or equal SO
- Jump if not less
- JLJNGE Jump if less SO
- Jump if not greater or equal
- JLEJNG Jump if less or equal SO Z1
- Jump if not greater
- JCXZ Jump if register CXzero
CX0
15Mapping High Level Branches into Linear Code
CMP AX, BX JA
true_label . ltFalse
Processinggt . JMP
done_label . true_label ltTrue
processinggt . done_label ltresume executiongt
16Mapping High Level Branches into Linear Code
(cont.)
17Mapping High Level Branches into Linear Code
(cont.)
- LOOP instruction
- combination of a decrement CX and a conditional
jump - LOOP decrements CX (ECX if in 32-bit mode) and if
CX ? 0 it jumps to the address indicated by the
label - if CX becomes a 0, the next sequential
instruction executes - ADDS PROC NEAR
- MOV CX, 100 load count
- MOV SI, OFFSET BLOCK1
- MOV DI, OFFSET BLOCK2
- Again
- LODSW get Block1 data AX SI SI SI
2 - ADD AX, ESDI add Block2 data
- STOSW store in Block2 DI AX DI DI
2 - LOOP Again repeat 100 times
- RET
- ADDS ENDP
18Examples
- if (J lt K) then
- L L 1
- else L L - 1
- J, K, L are signed words
-
- MOV AX, J
- CMP AX, K
- JNEL DoElse
- INC L
- JMP ifDone
- DoElse
- DEC L
- ifDone
- while (J gt K) do begin
- J J - 1
- K K 1
- L J K
- end
- WhlLoop
- MOV AX, J
- CMP AX, K
- JNGE QuitLoop
- DEC J
- INC K
- MOV AX, J
- IMUL AX, K
- MOV L, AX
- JMP WhlLoop
- QuitLoop
19Example (LOOPNE)
- The LOOPNE instruction is useful for controlling
loops that stop on some condition or when the
loop exceeds some number of iterations - Consider String1 that contains a sequence of
characters that end with the byte containing zero - we want to convert those characters to upper case
and copy them to String2 - ..
- String1 BYTE This string contains lower case
characters, 0 - String2 BYTE 128 dup (0)
- ..
20Example (LOOPNE)
- LEA SI, String1 the same as use of OFFSET
- LEA DI, String2
- MOV CX, 127 Max 127 chars to String2
- StrLoop
- LODSB get char from String1 AL SI SI
SI 1 - CMP AL, a see if lower case
- JB NotLower chars are unsigned
- CMP AL, z
- JA NotLower
- AND AL, 5Fh convert lower -gt upper case
bit 6 must be 0 - NotLower
- STOSB DI AL DI DI 1
- CMP AL, 0 see if zero terminator
- LOOPNE StrLoop quit if AL or CX 0