42
ECE291 ECE291 Lecture 4 Lecture 4 Jumping in head first Jumping in head first

ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

Embed Size (px)

Citation preview

Page 1: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE291ECE291

Lecture 4Lecture 4

Jumping in head firstJumping in head first

Page 2: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 22 of 42 of 42

Lecture outlineLecture outline

Multiplication and DivisionMultiplication and Division

Program control instructionsProgram control instructions

Unconditional jumpsUnconditional jumps

Conditional branchingConditional branching

Numerical comparisonNumerical comparison

LoopingLooping

High-level program constructsHigh-level program constructs

Page 3: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 33 of 42 of 42

MultiplicationMultiplication

The product after a multiplication is always a double-The product after a multiplication is always a double-width product width product

if we multiply two 16-bit numbers, they generate a 32-bit productif we multiply two 16-bit numbers, they generate a 32-bit product unsigned: (2unsigned: (21616 - 1) * (2 - 1) * (21616 - 1) = 2 - 1) = 23232 - 2 * 2 - 2 * 21616 + 1 < (2 + 1 < (23232 - 1) - 1) signed: (-2signed: (-21515) * (-2) * (-21515) = 2) = 23030 < (2 < (23131 - 1) - 1) overflow cannot occuroverflow cannot occur

Modification of FlagsModification of Flags Most flags are undefined after multiplicationMost flags are undefined after multiplication OO and and CC flags clear to 0 if the result fit into half-size register flags clear to 0 if the result fit into half-size register if the most significant 16 bits of the product are 0, both flags if the most significant 16 bits of the product are 0, both flags CC

and and OO clear to 0 clear to 0

Page 4: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 44 of 42 of 42

MultiplicationMultiplicationTwo different instructions for multiplicationTwo different instructions for multiplication

MULMUL Unsigned integer multiplicationUnsigned integer multiplication IMULIMUL Singed integer multiplicationSinged integer multiplication

MUL Multiplication is performed on bytes, words, or MUL Multiplication is performed on bytes, words, or doublesdoublesSize of the multiplier determines the operationSize of the multiplier determines the operationThe multiplier can be any register or any memory locationThe multiplier can be any register or any memory locationFor MUL, the multiplicand is always AL, AX or EAXFor MUL, the multiplicand is always AL, AX or EAX

mulmul cx cx ; AX * CX (unsigned result in DX:AX);; AX * CX (unsigned result in DX:AX);imulimul word [si] word [si] ; AX * [word content of memory location; AX * [word content of memory location

; addressed by SI] (signed product; addressed by SI] (signed product in DX:AX)in DX:AX)

Page 5: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 55 of 42 of 42

Unsigned multiplicationUnsigned multiplication

16-bit multiplication16-bit multiplicationMultiplicandMultiplicand AXAXMultiplierMultiplier 16-bit register or 16-bit memory variable16-bit register or 16-bit memory variableProductProduct High word in DX, Low word in AXHigh word in DX, Low word in AX

Multiplier

AX

AXDX

X

Page 6: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 66 of 42 of 42

Unsigned multiplicationUnsigned multiplication

8-bit multiplication8-bit multiplication Multiplicand Multiplicand ALAL Multiplier Multiplier 8-bit register or 8-bit memory variable8-bit register or 8-bit memory variable ProductProduct AXAX

32-bit multiplication32-bit multiplication MultiplicandMultiplicand EAXEAX Multiplier Multiplier 32-bit register or 32-bit memory 32-bit register or 32-bit memory

variable)variable) ProductProduct High word in EDX : Low word in EAX)High word in EDX : Low word in EAX)

Page 7: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 77 of 42 of 42

Signed multiplicationSigned multiplication

Not available in 8088 and 8086Not available in 8088 and 8086

For 2’s Complement signed integers onlyFor 2’s Complement signed integers only

Four formsFour forms imul reg/mem – assumes AL, AX or EAXimul reg/mem – assumes AL, AX or EAX imul reg, reg/memimul reg, reg/mem imul reg, immediate dataimul reg, immediate data imul reg, reg/mem, immediate dataimul reg, reg/mem, immediate data

Page 8: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 88 of 42 of 42

Signed multiplicationSigned multiplication

ExamplesExamples imul blimul bl ; BL * AL ; BL * AL AX AX imul bximul bx ; BX * AX ; BX * AX DX:AX DX:AX imul cl, [bl]imul cl, [bl] ; CL * [DS:BL] ; CL * [DS:BL] CL CL

; overflows can occur; overflows can occur imul cx, dx, 12h imul cx, dx, 12h ; 12h * DX ; 12h * DX CX CX

Page 9: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 99 of 42 of 42

Binary MultiplicationBinary Multiplication

Long multiplication is done through shifts and additionsLong multiplication is done through shifts and additions

This works if both numbers are positive This works if both numbers are positive

To multiply negative numbers, the CPU will store the To multiply negative numbers, the CPU will store the sign bits of the numbers, make both numbers positive, sign bits of the numbers, make both numbers positive, compute the result, then negate the result if necessarycompute the result, then negate the result if necessary

0 1 1 0 0 0 1 0 (98) x 0 0 1 0 0 1 0 1 (37)

0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 0 - - 0 1 1 0 0 0 1 0 - - - - - (3626)

Page 10: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1010 of 42 of 42

DivisionDivision

Two different instructions for divisionTwo different instructions for division DIVDIV Unsigned divisionUnsigned division IDIVIDIV Signed division (2’s complement)Signed division (2’s complement)

Division is performed on bytes, words, or double wordsDivision is performed on bytes, words, or double words

the size of the divisor determines the operationthe size of the divisor determines the operation

The dividend is always a double-width dividend that is The dividend is always a double-width dividend that is divided by the operand (divisor)divided by the operand (divisor)

The divisor can be any register or any memory locationThe divisor can be any register or any memory location

Page 11: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1111 of 42 of 42

DivisionDivision

32-bit/16-bit division - the use of the AX (and DX) registers is implied32-bit/16-bit division - the use of the AX (and DX) registers is impliedDividend Dividend high word in DX, low word in AXhigh word in DX, low word in AXDivisorDivisor 16-bit register or 16-bit memory variable16-bit register or 16-bit memory variableQuotientQuotient AX AX RemainderRemainder DXDX

Remainder (DX)Quotient (AX)

AXDXDivisor (Y)

Dividend (X)

Page 12: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1212 of 42 of 42

DivisionDivision16-bit/8-bit16-bit/8-bit

Dividend Dividend AX AX DivisorDivisor 8-bit register or 8-bit memory variable8-bit register or 8-bit memory variable QuotientQuotient ALAL RemainderRemainder AHAH

64-bit/32-bit64-bit/32-bit Dividend Dividend high double word in EDX, low double word high double word in EDX, low double word

in EAXin EAX DivisorDivisor 32-bit register or 32-bit memory variable)32-bit register or 32-bit memory variable) QuotientQuotient EAXEAX RemainderRemainder EDXEDX

Page 13: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1313 of 42 of 42

DivisionDivisionDivision of two equally sized wordsDivision of two equally sized words

Unsigned numbers:Unsigned numbers: move zero into high order-word move zero into high order-word Signed numbers:Signed numbers: use use signed extensionsigned extension to fill high-word with to fill high-word with

ones or zerosones or zeros CBWCBW (convert byte to word)(convert byte to word)

AX = xxxx xxxx AX = xxxx xxxx snnn nnnnsnnn nnnn (before) (before)AX = AX = ssss ssss snnn nnnnssss ssss snnn nnnn (after) (after)

CWDCWD (convert word to double)(convert word to double)DX:AX = xxxx xxxx xxxx xxxx DX:AX = xxxx xxxx xxxx xxxx snnn nnnn nnnn nnnnsnnn nnnn nnnn nnnn (before) (before)DX:AX = DX:AX = ssss ssss ssss ssss snnn nnnn nnnn nnnnssss ssss ssss ssss snnn nnnn nnnn nnnn (after) (after)

CWDECWDE (convert double to double-word extended)(convert double to double-word extended)

Page 14: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1414 of 42 of 42

DivisionDivision

Flag settingsFlag settings none of the flag bits change predictably for a divisionnone of the flag bits change predictably for a division

A division can result in two types of errorsA division can result in two types of errors divide by zerodivide by zero divide overflow (a small number divides into a large number), divide overflow (a small number divides into a large number),

e.g., 3000 / 2e.g., 3000 / 2AX = 3000AX = 3000

Devisor is 2 => 8 bit division is performedDevisor is 2 => 8 bit division is performed

Quotient will be written to AL => but 1500 does not fit into AL Quotient will be written to AL => but 1500 does not fit into AL

consequently we have divide overflowconsequently we have divide overflow

in both cases microprocessor generates interrupt (interrupts are in both cases microprocessor generates interrupt (interrupts are covered later in this course)covered later in this course)

Page 15: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1515 of 42 of 42

ExampleExample

Division of the byte contents of memory NUMB Division of the byte contents of memory NUMB by the contents of NUMB1by the contents of NUMB1

Unsigned

MOV AL, [NUMB] ;get NUMBMOV AH, 0 ;zero extendDIV byte [NUMB1]MOV [ANSQ], AL ;save quotientMOV [ANSR], AH ;save remainder

Signed

MOV AL, [NUMB] ;get NUMBCBW ;signed-extendIDIV byte [NUMB1]MOV [ANSQ], AL ;save quotientMOV [ANSR], AH ;save remainder

Page 16: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1616 of 42 of 42

DivisionDivision

What do we do with remainder after division?What do we do with remainder after division? use the remainder to round the resultuse the remainder to round the result drop the remainder to truncate the resultdrop the remainder to truncate the result if the division is unsigned, rounding requires that double the if the division is unsigned, rounding requires that double the

remainder is compared with the divisor to decide whether to remainder is compared with the divisor to decide whether to round up the quotient round up the quotient

Example, sequence of instructions that divide AX by BL and Example, sequence of instructions that divide AX by BL and round the result round the result

DIV BLADD AH, AH ;double remainderCMP AH, BL ;test for roundingJB .NEXTINC AL

.NEXT

Page 17: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1717 of 42 of 42

Unconditional jumpsUnconditional jumps

Basically an instruction that causes execution to Basically an instruction that causes execution to transfer to some point other than the next transfer to some point other than the next sequential instruction in your code.sequential instruction in your code.

Essentially a “goto” statement.Essentially a “goto” statement.

Forget all the nasty things you heard about using Forget all the nasty things you heard about using “goto’s” since you were a kid programming “goto’s” since you were a kid programming Quick Basic in middle school.Quick Basic in middle school.

Assembly language couldn’t exist without Assembly language couldn’t exist without unconditional jumps.unconditional jumps.

Page 18: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1818 of 42 of 42

Unconditional jumpsUnconditional jumps

Short jumpShort jump – 2-byte instruction that allows jumps to memory – 2-byte instruction that allows jumps to memory locations within +127 and -128 bytes from the memory location locations within +127 and -128 bytes from the memory location following the jump statement.following the jump statement.

JMP SHORT JMP SHORT labellabel

Near jumpNear jump – 3-byte instruction that allows jumps within +/- 32KB – 3-byte instruction that allows jumps within +/- 32KB from the instruction in the current code segmentfrom the instruction in the current code segment

JMP JMP labellabel

Far jumpFar jump – 5-byte instruction that allows a jump to any memory – 5-byte instruction that allows a jump to any memory location within the entire memory spacelocation within the entire memory space

JMP JMP labellabel

In protected mode, the near jump and far jump are the sameIn protected mode, the near jump and far jump are the same

OP

OP

DISP8

DISP 16

OP OFFSET 16 SEGMENT 16

Page 19: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 1919 of 42 of 42

Conditional jumpsConditional jumps

Logic and arithmetic instructions set flagsLogic and arithmetic instructions set flagsFlags provide state information from previous Flags provide state information from previous instructionsinstructionsUsing flags we can perform conditional jumping. For Using flags we can perform conditional jumping. For example, transfer program execution to some different example, transfer program execution to some different place within the programplace within the programif condition is true if condition is true

jump back or forward in a code to the location specifiedjump back or forward in a code to the location specified instruction pointer (IP) gets updated to point to the instruction to instruction pointer (IP) gets updated to point to the instruction to

which the program will jumpwhich the program will jump

if condition is false if condition is false continue execution at the following instructioncontinue execution at the following instruction IP gets incremented as usualIP gets incremented as usual

Page 20: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2020 of 42 of 42

Conditional jumpsConditional jumps

Take the form J + some condition or Take the form J + some condition or combination of conditionscombination of conditions

L, G, A, B, Z, E, S, C, N are some of the L, G, A, B, Z, E, S, C, N are some of the more common conditionsmore common conditions

We’ll have a closer look in a few more We’ll have a closer look in a few more slidesslides

Page 21: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2121 of 42 of 42

Conditional jumpsConditional jumps

Conditional jumps are always short jumps in the 8086-Conditional jumps are always short jumps in the 8086-8028680286

the range of the jump is +127 bytes and -128 bytes from the the range of the jump is +127 bytes and -128 bytes from the location following the conditional jumplocation following the conditional jump

In the 80386 and above, conditional jumps are either In the 80386 and above, conditional jumps are either short or near jumpsshort or near jumpsIf you want to do a near jump you If you want to do a near jump you mustmust include the include the NEAR keyword after the jump instruction and before the NEAR keyword after the jump instruction and before the labellabelConditional jumps test: sign (S), zero (Z), carry (C), Conditional jumps test: sign (S), zero (Z), carry (C), parity (P), and overflow (O)parity (P), and overflow (O)An FFh is An FFh is aboveabove 00h in the set of unsigned numbers 00h in the set of unsigned numbersAn FFh (-1) is An FFh (-1) is lessless than 00h for signed numbers than 00h for signed numbers

Page 22: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2222 of 42 of 42

Numerical comparisonNumerical comparison

CMP A, BCMP A, B compares A to B compares A to B a subtraction that a subtraction that only changes the flag bitsonly changes the flag bits useful for checking the entire contents of a register or useful for checking the entire contents of a register or

a memory location against another valuea memory location against another value usually followed by a conditional jump instructionusually followed by a conditional jump instruction

CMP AL, 10h CMP AL, 10h ;compare with 10h (contents of AL do not change);compare with 10h (contents of AL do not change)

JAE foo JAE foo ;if 10h or above then jump to memory location foo;if 10h or above then jump to memory location foo

SUBSUB A,B calculates difference A - BA,B calculates difference A - B saves result to A and sets flagssaves result to A and sets flags

Page 23: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2323 of 42 of 42

Numerical comparisonNumerical comparison

CMP, SUB instructions always set flags CMP, SUB instructions always set flags the same way regardless if you are the same way regardless if you are interpreting the operands as signed or interpreting the operands as signed or unsigned.unsigned.

There are different flags you look at There are different flags you look at depending on what kind of data you’re depending on what kind of data you’re comparingcomparing

Page 24: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2424 of 42 of 42

Numerical comparisonNumerical comparison

Unsigned operandsUnsigned operandsZ: Z: equality/inequalityequality/inequalityC:C: A < B (C = 1)A < B (C = 1)

A > B (C = 0)A > B (C = 0)S:S: No meaningNo meaningO:O: No meaningNo meaning

Signed operandsSigned operandsZ:Z: equality/inequalityequality/inequalityC:C: No meaningNo meaningS and 0 together:S and 0 together:

if S xor O = 1 thenif S xor O = 1 thenA < BA < B

elseelseA > BA > B

CMP A, B

Page 25: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2525 of 42 of 42

Signed comparisonsSigned comparisons

Consider Consider CMP AX,BXCMP AX,BX computed by the CPU computed by the CPU The The Sign bitSign bit (S) will be set if the result of AX-BX has a (S) will be set if the result of AX-BX has a

1 at the most significant bit of the result1 at the most significant bit of the result

The The Overflow flagOverflow flag (O) will be set if the result of AX-BX (O) will be set if the result of AX-BX produced a number that was out of range [-32768, produced a number that was out of range [-32768, 32767].32767].

Case 1: Sign = 1 and Overflow = 0Case 1: Sign = 1 and Overflow = 0 AX-BX looks negative, andAX-BX looks negative, and No overflow so result was within range and is validNo overflow so result was within range and is valid Therefore, AX must be less than BXTherefore, AX must be less than BX

Page 26: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2626 of 42 of 42

Signed comparisonsSigned comparisons

Case 2: Sign = 0 and Overflow = 1Case 2: Sign = 0 and Overflow = 1 AX-BX looks positive, butAX-BX looks positive, but Overflow is set, so result is out of range so the sign bit is wrongOverflow is set, so result is out of range so the sign bit is wrong Therefore AX must still be less than BXTherefore AX must still be less than BX

Case 3: Sign = 0 and Overflow = 0Case 3: Sign = 0 and Overflow = 0 AX-BX is positive, andAX-BX is positive, and There was no overflow, meaning sign bit is correctThere was no overflow, meaning sign bit is correct So AX must be greater than BXSo AX must be greater than BX

Case 4: Sign = 1 and Overflow = 1Case 4: Sign = 1 and Overflow = 1 AX-BX looks negative, butAX-BX looks negative, but There was an overflow so the sign is incorrectThere was an overflow so the sign is incorrect AX is actually greater than BXAX is actually greater than BX

Page 27: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2727 of 42 of 42

Signed comparisonsSigned comparisons

Difference inDifference in JSJS (jump on sign) and (jump on sign) and JLJL (jump less than) (jump less than)

JSJS looks at the sign bit (S) of the last compare looks at the sign bit (S) of the last compare (or subtraction) only. If S = 1 then jump.(or subtraction) only. If S = 1 then jump.

JLJL looks at looks at S S XOR XOR OO of the last compare of the last compare

REGARDLESS of the value AX-BX, i.e., even if REGARDLESS of the value AX-BX, i.e., even if AX-BX causes overflow, the JL will be correctly AX-BX causes overflow, the JL will be correctly executedexecuted

Page 28: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2828 of 42 of 42

Signed comparisonsSigned comparisons

JL is true if the condition: S xor O is metJL is true if the condition: S xor O is met

JL is true for two conditions:JL is true for two conditions:

S=1, O=0:S=1, O=0: (AX-BX) was negative and (AX-(AX-BX) was negative and (AX-BX) did not overflowBX) did not overflow Example (8-bit): CMP -5, 2Example (8-bit): CMP -5, 2

(-5) - (2) = (-7) (-5) - (2) = (-7)

Result (-7) has the sign bit set Result (-7) has the sign bit set

Thus (-5) is less than (2).Thus (-5) is less than (2).

Page 29: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 2929 of 42 of 42

Signed comparisonsSigned comparisons

S=0, O=1:S=0, O=1: Overflow!, Sign bit of the result is wrong!Overflow!, Sign bit of the result is wrong!Consider the following case:Consider the following case:

AX is a large negative number (-)AX is a large negative number (-) BX is a positive number (+).BX is a positive number (+). The subtraction of (-) and (+) is the same as the addition of (-) and (-)The subtraction of (-) and (+) is the same as the addition of (-) and (-) The result causes negative overflow, and thus cannot be represented The result causes negative overflow, and thus cannot be represented

as a signed integer correctly (O=1).as a signed integer correctly (O=1). The result of AX-BX appears positive (S=0).The result of AX-BX appears positive (S=0).

Example (8-bit): (-128) - (1) = (+127) Example (8-bit): (-128) - (1) = (+127) Result (+127) overflowed. Ans should have been -129.Result (+127) overflowed. Ans should have been -129. Result appears positive, but overflow occurred Result appears positive, but overflow occurred Thus (-128) is less than (1), the condition is Thus (-128) is less than (1), the condition is TRUE for JLTRUE for JL

Page 30: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3030 of 42 of 42

Conditional jumpsConditional jumps

Terminology used to differentiate between jump Terminology used to differentiate between jump instructions that use the instructions that use the carry flagcarry flag and the and the overflow flagoverflow flag Above/BelowAbove/Below unsigned compareunsigned compare Greater/LessGreater/Less signed (+/-) comparesigned (+/-) compare

Names of jump instructionsNames of jump instructions J J => => JumpJump N N => => NotNot A/B G/LA/B G/L => => Above/Below Greater/LessAbove/Below Greater/Less EE =>=> EqualEqual

Page 31: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3131 of 42 of 42

Conditional jumpsConditional jumps

CommandCommand DescriptionDescription ConditionCondition

JA=JNBEJA=JNBE Jump if above Jump if above C=0 & Z=0C=0 & Z=0 Jump if not below or equalJump if not below or equal

JBE=JNAJBE=JNA Jump if below or equalJump if below or equal C=1 | Z=1C=1 | Z=1 JAE=JNB=JNC JAE=JNB=JNC Jump if above or equal Jump if above or equal C=0C=0 Jump if not below Jump if not below

Jump if no carry Jump if no carry JB=JNA=JCJB=JNA=JC Jump if below Jump if below C=1C=1

Jump if carry Jump if carry JE=JZJE=JZ Jump if equal Jump if equal Z=1Z=1

Jump if Zero Jump if Zero JNE=JNZ JNE=JNZ Jump if not equalJump if not equal Z=0Z=0

Jump if not zero Jump if not zero JSJS Jump Sign (MSB=1) Jump Sign (MSB=1) S=1S=1

Page 32: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3232 of 42 of 42

Conditional jumpsConditional jumps

CommandCommand DescriptionDescription ConditionCondition

JNSJNS Jump Not Sign (MSB=0)Jump Not Sign (MSB=0) S=0 S=0 JO JO Jump if overflow setJump if overflow set O=1O=1JNOJNO Jump if no overflowJump if no overflow O=0O=0JG=JNLE JG=JNLE Jump if greater Jump if greater

Jump if not less or equalJump if not less or equal S=O & Z=0S=O & Z=0JGE=JNLJGE=JNL Jump if greater or equalJump if greater or equal S=OS=O

Jump if not less Jump if not less JL=JNGEJL=JNGE Jump if lessJump if less S^OS^O

Jump if not greater or equalJump if not greater or equal JLE=JNG JLE=JNG Jump if less or equalJump if less or equal S^O | Z=1 S^O | Z=1 Jump if not greater Jump if not greater JCXZJCXZ Jump if register CX=zeroJump if register CX=zero CX=0CX=0

Page 33: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3333 of 42 of 42

LoopingLooping

The LOOP instruction is a combination of The LOOP instruction is a combination of the DEC and JNZ instructionsthe DEC and JNZ instructions

LOOP decrements CX and if CX has not LOOP decrements CX and if CX has not become zero jumps to the specified labelbecome zero jumps to the specified label

If CX is zero, the instruction following the If CX is zero, the instruction following the LOOP is executedLOOP is executed

Page 34: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3434 of 42 of 42

Loop exampleLoop exampleADDSADDS

mov cx, 100mov cx, 100 ;load count;load countmovmov si, BLOCK1si, BLOCK1movmov di, BLOCK2di, BLOCK2

.Again.Againlodswlodsw ;get Block1 data;get Block1 data

; AX = [SI]; SI = SI + 2; AX = [SI]; SI = SI + 2

addadd AX, [ES:DI]AX, [ES:DI] ;add Block2 data;add Block2 data

stoswstosw ;store in Block2;store in Block2; [DI] = AX; DI = DI + 2; [DI] = AX; DI = DI + 2

looploop .Again.Again ;repeat 100 times;repeat 100 times

retret

Page 35: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3535 of 42 of 42

If…then…else clausesIf…then…else clauses

‘ ‘ Visual Basic ExampleVisual Basic Example

if ax > bx thenif ax > bx then

‘‘true instructionstrue instructions

elseelse

‘‘false instructionsfalse instructions

end ifend if

cmp ax, bxcmp ax, bx

ja .trueja .true

;false instructions;false instructions

jmp .donejmp .done

.true.true

;true instructions;true instructions

.done.done

Page 36: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3636 of 42 of 42

Switch…case clausesSwitch…case clauses

/* C code example *//* C code example */switch (choice) {switch (choice) {

case ‘a’:case ‘a’:/* a instructions *//* a instructions */break;break;

case ‘b’:case ‘b’:/* b instructions *//* b instructions */break;break;

default:default:/* default instructions /* default instructions

*/*/}}

cmp al, ‘a’cmp al, ‘a’jne .bjne .b

; a instructions; a instructionsjmp .donejmp .done

.b.bcmp al, ‘b’cmp al, ‘b’jne .defaultjne .default

; b instructions; b instructionsjmp .donejmp .done

.default.default; default instructions; default instructions

.done.done

Page 37: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3737 of 42 of 42

Do…while clauseDo…while clause

do {do {

// instructions// instructions

while (A=B);while (A=B);

.begin.begin

; instructions; instructions

mov ax, [A]mov ax, [A]

cmp ax, [B]cmp ax, [B]

je .beginje .begin

Page 38: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3838 of 42 of 42

While…do clauseWhile…do clause

while (A=B) {while (A=B) {

// instructions// instructions

}}

.begin.begin

mov ax, [A]mov ax, [A]

cmp ax, [B]cmp ax, [B]

jne .donejne .done

; instructions; instructions

jmp .beginjmp .begin

Page 39: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 3939 of 42 of 42

For loopsFor loops

for (i = 10; i > 0; i--) {for (i = 10; i > 0; i--) {

// instructions// instructions

}}

mov cx, 10mov cx, 10

.begin.begin

; instructions; instructions

loop .beginloop .begin

Page 40: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 4040 of 42 of 42

ExamplesExamples;; if (J <= K) thenif (J <= K) then

;; L := L + 1L := L + 1

;; elseelse L := L - 1L := L - 1

; ; J, K, L are signed wordsJ, K, L are signed words

movmov ax, [J]ax, [J]

cmpcmp ax, [K]ax, [K]

jneljnel .DoElse.DoElse

incinc word [L]word [L]

jmpjmp .ifDone.ifDone

.DoElse:.DoElse:

decdec word [L]word [L]

.ifDone:.ifDone:

;; while (J >= K) do beginwhile (J >= K) do begin

;; J := J - 1;J := J - 1;

;; K := K + 1;K := K + 1;

;; L := J * K;L := J * K;

;; end;end;

.WhlLoop:.WhlLoop:movmov ax, [J]ax, [J]cmpcmp ax, [K]ax, [K]jngejnge .QuitLoop.QuitLoopdecdec word [J]word [J]incinc word [K]word [K]movmov ax, [J]ax, [J]imulimul [K][K]movmov [L], ax[L], axjmpjmp .WhlLoop.WhlLoop

.QuitLoop:.QuitLoop:

Page 41: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 4141 of 42 of 42

LOOPNELOOPNE

The LOOPNE instruction is useful for controlling The LOOPNE instruction is useful for controlling loops that stop on some condition or when the loops that stop on some condition or when the loop exceeds some number of iterationsloop exceeds some number of iterations

Consider String1 that contains a sequence of Consider String1 that contains a sequence of characters that end with the byte containing zerocharacters that end with the byte containing zero

we want to convert those characters to upper we want to convert those characters to upper case and copy them to String2case and copy them to String2……....

String1 DB “This string contains lower case characters”, 0String1 DB “This string contains lower case characters”, 0

String2 128 DB 0String2 128 DB 0

…………....

Page 42: ECE291 Lecture 4 Jumping in head first. ECE 291 Lecture 4Page 2 of 42 Lecture outline Multiplication and Division Program control instructions Unconditional

ECE 291 Lecture 4ECE 291 Lecture 4

Page Page 4242 of 42 of 42

Example (LOOPNE)Example (LOOPNE)mov si, String1 mov si, String1 ;si points to beginning of ;si points to beginning of String1String1lea di, String2lea di, String2 ;di points to beginning of ;di points to beginning of String2String2mov cx, 127mov cx, 127 ;Max 127 chars to String2;Max 127 chars to String2

.StrLoop:.StrLoop:lodsblodsb ;get char from String1;;get char from String1;AL=[DS:SI]; SI = SI + 1AL=[DS:SI]; SI = SI + 1cmp al, ‘a’cmp al, ‘a’ ;see if lower case;see if lower casejb .NotLowerjb .NotLower ;chars are unsigned;chars are unsignedcmp al, ‘z’cmp al, ‘z’ja .NotLowerja .NotLowerand al, 5Fhand al, 5Fh ;convert lower -> upper case;convert lower -> upper case

;bit 6 must be 0 ;bit 6 must be 0 .NotLower:.NotLower:

stosbstosb ;[ES:DI] = AL; DI = DI + 1;[ES:DI] = AL; DI = DI + 1cmp al, 0cmp al, 0 ;see if zero terminator;see if zero terminatorloopne .StrLooploopne .StrLoop ;quit if AL or CX = 0;quit if AL or CX = 0