43
Branch loops and Arithmetic I Assembly Instructions Brief overview of some basic instructions: Arithmetic instructions Jump instructions Loop instruction Logical instructions Shift instructions Rotate instructions These instructions allow you to write reasonable assembly language programs

Assembly Branches Loops and Arithmatics

Embed Size (px)

Citation preview

Page 1: Assembly Branches Loops and Arithmatics

1

Branch loops and Arithmetic I

Assembly Instructions

• Brief overview of some basic instructions:∗ Arithmetic instructions∗ Jump instructions∗ Loop instruction∗ Logical instructions∗ Shift instructions∗ Rotate instructions

• These instructions allow you to write reasonable assembly language programs

Page 2: Assembly Branches Loops and Arithmatics

2

Arithmetic Instructions I

INC and DEC instructions∗ Format:

inc destination dec destination

∗ Semantics:destination = destination +/- 1

» destination can be 8-, 16-, or 32-bit operand, in memory or register

No immediate operand

• Examplesinc BX dec value

Arithmetic Instructions (cont’d)

Add instructions∗ Format:

add destination,source

∗ Semantics:destination = destination + source

• Examplesadd BX, AX add value,35

∗ inc AX is better than add AX,1– inc takes less space– inc is faster than add

Page 3: Assembly Branches Loops and Arithmatics

3

Arithmetic Instructions (cont’d)

Add instructions∗ Addition with carry∗ Format:

adc destination,source

∗ Semantics:destination = destination + source + CF

• Example: 32-bit additionadd AX, CX ; add lower 16 bitsadc BX, DX ; add upper 16 bits with carry

∗ 32-bit result in BX:AX

Arithmetic Instructions (cont’d)

Subtract instructions∗ Format:

sub destination,source

∗ Semantics:destination = destination - source

• Examplessub BX, AX sub value,35

∗ dec AX is better than sub AX,1– dec takes less space– dec is faster than sub

Page 4: Assembly Branches Loops and Arithmatics

4

Arithmetic Instructions (cont’d)

Subtract instructions∗ Subtract with borrow∗ Format:

sbb destination,source

∗ Semantics:destination = destination - source - CF

∗ Like the adc, sbb is useful in dealing with more than 32-bit numbers

• Negationneg destination

∗ Semantics:destination = 0 - destination

Arithmetic Instructions (cont’d)

CMP instruction∗ Format:

cmp destination,source

∗ Semantics:destination - source

∗ destination and source are not altered∗ Useful to test relationship (>, =) between two operands∗ Used in conjunction with conditional jump instructions

for decision making purposes• Examples

cmp EBX,EAX cmp count,100

Page 5: Assembly Branches Loops and Arithmatics

5

Status Flags

Status Flags (cont’d)

• Status flags are updated to indicate certain properties of the result∗ Example: If the result is zero, zero flag is set

• Once a flag is set, it remains in that state until another instruction that affects the flags is executed

• Not all instructions affect all status flags∗ add and sub affect all six flags∗ inc and dec affect all but the carry flag∗ mov, push, and pop do not affect any flags

Page 6: Assembly Branches Loops and Arithmatics

6

Status Flags (cont’d)

• Example; initially, assume ZF = 0mov AL,55H ; ZF is still zerosub AL,55H ; result is 0

; ZF is set (ZF = 1)push BX ; ZF remains 1mov BX,AX ; ZF remains 1pop DX ; ZF remains 1mov CX,0 ; ZF remains 1inc CX ; result is 1

; ZF is cleared (ZF = 0)

Status Flags (cont’d)

• Zero Flag∗ Indicates zero result

– If the result is zero, ZF = 1– Otherwise, ZF = 0

∗ Zero can result in several ways (e.g. overflow)mov AL,0FH mov AX,0FFFFH mov AX,1add AL,0F1H inc AX dec AX

» All three examples result in zero result and set ZF

∗ Related instructionsjz jump if zero (jump if ZF = 1)jnz jump if not zero (jump if ZF = 0)

Page 7: Assembly Branches Loops and Arithmatics

7

Status Flags (cont’d)

• Uses of zero flag∗ Two main uses of zero flag

» Testing equality– Often used with cmp instruction

cmp char,’$’ ; ZF = 1 if char is $

cmp AX,BX

» Counting to a preset value– Initialize a register with the count value– Decrement it using dec instruction– Use jz/jnz to transfer control

Status Flags (cont’d)

• Consider the following code

sum := 0for (i = 1 to M)

for (j = 1 to N)sum := sum + 1

end forend for

• Assembly code

sub AX,AX ; AX := 0mov DX,M

outer_loop:mov CX,N

inner_loop:inc AXloop inner_loopdec DXjnz outer_loop

exit_loops:mov sum,AX

Page 8: Assembly Branches Loops and Arithmatics

8

Status Flags (cont’d)

• Two observations∗ loop instruction is equivalent to

dec DXjnz outer_loop

» This two instruction sequence is more efficient than the loopinstruction (takes less time to execute)

» loop instruction does not affect any flags!

∗ This two instruction sequence is better than initializing DX = 1 and executing

inc DXcmp DX,Mjle inner_loop

Status Flags (cont’d)

• Carry Flag∗ Records the fact that the result of an arithmetic

operation on unsigned numbers is out of range∗ The carry flag is set in the following examples

mov AL,0FH mov AX,12AEHadd AL,0F1H sub AX,12AFH

∗ Range of 8-, 16-, and 32-bit unsigned numberssize range

8 bits 0 to 255 (28 − 1)16 bits 0 to 65,535 (216 − 1)32 bits 0 to 4,294,967,295 (232−1)

Page 9: Assembly Branches Loops and Arithmatics

9

Status Flags (cont’d)

∗ Carry flag is not set by inc and dec instructions» The carry flag is not set in the following examples

mov AL,0FFH mov AX,0inc AL dec AX

∗ Related instructionsjc jump if carry (jump if CF = 1)jnc jump if no carry (jump if CF = 0)

∗ Carry flag can be manipulated directly usingstc set carry flag (set CF to 1)clc clear carry flag (clears CF to 0)cmc complement carry flag (inverts CF value)

Status Flags (cont’d)

• Uses of carry flag∗ To propagate carry/borrow in multiword

addition/subtraction1 ← carry from lower 32 bits

x = 3710 26A8 1257 9AE7Hy = 489B A321 FE60 4213H

7FAB C9CA 10B7 DCFAH

∗ To detect overflow/underflow condition» In the last example, carry out of leftmost bit indicates overflow

∗ To test a bit using the shift/rotate instructions» Bit shifted/rotated out is captured in the carry flag» We can use jc/jnc to test whether this bit is 1 or 0

Page 10: Assembly Branches Loops and Arithmatics

10

Status Flags (cont’d)

• Overflow flag∗ Indicates out-of-range result on signed numbers

– Signed number counterpart of the carry flag∗ The following code sets the overflow flag but not the

carry flagmov AL,72H ; 72H = 114Dadd AL,0EH ; 0EH = 14D

∗ Range of 8-, 16-, and 32-bit signed numberssize range

8 bits − 128 to +127 27 to (27 − 1)16 bits − 32,768 to +32,767 215 to (215 − 1)32 bits −2,147,483,648 to +2,147,483,647 231 to (231 − 1)

Status Flags (cont’d)

Unsigned interpretation

mov AL,72Hadd AL,0EHjc overflow

no_overflow:

(no overflow code here). . . .

overflow:

(overflow code here). . . .

Signed interpretation

mov AL,72Hadd AL,0EHjo overflow

no_overflow:

(no overflow code here). . . .

overflow:

(overflow code here). . . .

• Signed or unsigned: How does the system know?∗ The processor does not know the interpretation∗ It sets carry and overflow under each interpretation

Page 11: Assembly Branches Loops and Arithmatics

11

Status Flags (cont’d)

∗ Related instructionsjo jump if overflow (jump if OF = 1)jno jump if no overflow (jump if OF = 0)

• Uses of overflow flag∗ Main use

» To detect out-of-range result on signed numbers

Status Flags (cont’d)

• Sign flag∗ Indicates the sign of the result

– Useful only when dealing with signed numbers– Simply a copy of the most significant bit of the result

∗ Examplesmov AL,15 mov AL,15add AL,97 sub AL,97clears the sign flag as sets the sign flag asthe result is 112 the result is −82(or 0111000 in binary) (or 10101110 in binary)

∗ Related instructionsjs jump if sign (jump if SF = 1)jns jump if no sign (jump if SF = 0)

Page 12: Assembly Branches Loops and Arithmatics

12

Status Flags (cont’d)

• Consider the count down loop:for (i = M downto 0)

<loop body>end for

• If we don’t use the jns, we need cmp as shown below:

cmp CX,0jl for_loop

The count down loop can be implemented as

mov CX,Mfor_loop:

<loop body>dec CXjns for_loop

• Usage of sign flag∗ To test the sign of the result∗ Also useful to efficiently implement countdown loops

Status Flags (cont’d)

• Auxiliary flag∗ Indicates whether an operation produced a carry or

borrow in the low-order 4 bits (nibble) of 8-, 16-, or 32-bit operands (i.e. operand size doesn’t matter)

∗ Example1 ← carry from lower 4 bits

mov AL,43 43D = 0010 1011Badd AL,94 94D = 0101 1110B

137D = 1000 1001B

» As there is a carry from the lower nibble, auxiliary flag is set

Page 13: Assembly Branches Loops and Arithmatics

13

Status Flags (cont’d)

• Parity flag∗ Indicates even parity of the low 8 bits of the result

– PF is set if the lower 8 bits contain even number 1 bits– For 16- and 32-bit values, only the least significant 8 bits

are considered for computing parity value∗ Example

mov AL,53 53D = 0011 0101Badd AL,89 89D = 0101 1001B

142D = 1000 1110B» As the result has even number of 1 bits, parity flag is set

∗ Related instructionsjp jump on even parity (jump if PF = 1)jnp jump on odd parity (jump if PF = 0)

FLAGS with Signed and Unsigned Numbers

Page 14: Assembly Branches Loops and Arithmatics

14

Unsigned Integers

• When you use unsigned integers the bits in the FLAGS register (also called “flags”) are important:∗ ZF: The Zero Flag (set to 1 if result is 0)∗ CF: The Carry Flag

• Consider: cmp a, b (which computes a-b)∗ If a = b: ZF is set, CF is not set∗ If a < b: ZF is not set, CF is set (borrow)∗ If a > b: ZF is not set, CF is not set

• Therefore, by looking at ZF and CF you can determine the result of the comparison!

Signed Integers

• For signed integers you should care about three flags∗ ZF: zero flag ∗ OF: overflow flag (set to 1 if the result overflows or

underflows)∗ SF: sign flag (set to 1 if the result is negative)

• Consider: cmp a, b (which computes a-b)∗ If a = b: ZF is set, OF is not set, SF is not set∗ If a < b: ZF is not set, and SF ≠ OF∗ If a > b: ZF is not set, and SF = OF

• Therefore, by looking at ZF, SF, and OF you can determine the result of the comparison!

Page 15: Assembly Branches Loops and Arithmatics

15

Signed Integers: SF and OF???

• Why do we have this odd relationship between SF an OF?• Consider two signed integers a and b, and remember that

we compute (a-b)• If a < b

∗ If there is no underflow, then (a-b) is a negative number!∗ If there is underflow, then (a-b) is (erroneously) a positive number∗ Therefore, in both cases SF ≠ OF

• If a > b∗ If there is no overflow, the (correct) result is positive∗ If there is an overflow, the (incorrect) result is negative∗ Therefore, in both cases SF = OF

The FLAGS register

• Is it very important to remember that many instructions change the bits of the FLAGS register

• So you should “act” on flag values immediately, and not expect them to remain unchanged inside FLAGS∗ or you can save them by-hand for later use perhaps

Page 16: Assembly Branches Loops and Arithmatics

16

Summary

cmp a,b ZF CF OF SF

a=b 1 0

a<b 0 1unsigned a>b 0 0

a=b 1 0 0

signeda<b 0 v !v

a>b 0 v v

JUMP and Branches (Conditional Jumps)

Page 17: Assembly Branches Loops and Arithmatics

17

Unconditional Jump

∗ Format:jmp label

∗ Semantics:» Execution is transferred to the instruction identified by label

• Target can be specified in one of two ways∗ Directly

» In the instruction itself

∗ Indirectly» Through a register or memory

Unconditional Jump (cont’d)

Example• Two jump instructions

∗ Forward jumpjmp CX_init_done

∗ Backward jumpjmp repeat1

• Programmer specifies target by a label

• Assembler computes the offset using the symbol table

. . .mov CX,10jmp CX_init_done

init_CX_20:mov CX,20

CX_init_done:mov AX,CX

repeat1:dec CX

. . .jmp repeat1

. . .

Page 18: Assembly Branches Loops and Arithmatics

18

Unconditional Jump (cont’d)

• Address specified in the jump instruction is not the absolute address∗ Uses relative address

» Specifies relative byte displacement between the target instruction and the instruction following the jump instruction

» Displacement is w.r.t the instruction following jmp– Reason: IP points to this instruction after reading jump

∗ Execution of jmp involves adding the displacement value to current IP

∗ Displacement is a signed 16-bit number» Negative value for backward jumps» Positive value for forward jumps

Target Location

• Inter-segment jump∗ Target is in another segment

CS = target-segment (2 bytes)IP = target-offset (2 bytes)

» Called far jumps (needs five bytes to encode jmp)

• Intra-segment jumps∗ Target is in the same segment

IP = IP + relative-displacement (1 or 2 bytes)∗ Uses 1-byte displacement if target is within −128 to +127

» Called short jumps (needs two bytes to encode jmp)∗ If target is outside this range, uses 2-byte displacement

» Called near jumps (needs three bytes to encode jmp)

Page 19: Assembly Branches Loops and Arithmatics

19

Target Location (cont’d)

• In most cases, the assembler can figure out the type of jump ∗ For backward jumps, assembler can decide whether to

use the short jump form or not• For forward jumps, it needs a hint from the

programmer∗ Use SHORT prefix to the target label∗ If such a hint is not given

» Assembler reserves three bytes for jmp instruction» If short jump can be used, leaves one byte of nop (no operation)

– See the next example for details

Example. . .

8 0005 EB 0C jmp SHORT CX_init_done0013 - 0007 = 0C

9 0007 B9 000A mov CX,10 10 000A EB 07 90 jmp CX_init_done

nop 0013 - 000D = 0711 init_CX_20: 12 000D B9 0014 mov CX,20 13 0010 E9 00D0 jmp near_jump

00E3 - 0013 = D014 CX_init_done: 15 0013 8B C1 mov AX,CX

Page 20: Assembly Branches Loops and Arithmatics

20

Example (cont’d)16 repeat1: 17 0015 49 dec CX 18 0016 EB FD jmp repeat1

0015 - 0018 = -3 = FDH. . . 84 00DB EB 03 jmp SHORT short_jump

00E0 - 00DD = 3 85 00DD B9 FF00 mov CX, 0FF00H 86 short_jump: 87 00E0 BA 0020 mov DX, 20H 88 near_jump: 89 00E3 E9 FF27 jmp init_CX_20

000D - 00E6 = -217 = FF27H

Indirect Jumps

• Direct jump∗ Target address is encoded in the instruction itself

• Indirect jump∗ Introduces a level of indirection

» Address is specified either through memory of a general-purpose register

∗ Examplejmp CX

jumps to the address in CX

∗ Address is absolute» Not relative as in direct jumps

Page 21: Assembly Branches Loops and Arithmatics

21

Indirect Jumps (cont’d)Switch (ch) {

Case ’0’:count[0]++; break;

Case ’1’:count[1]++; break;

Case ’2’:count[2]++; break;

Case ’3’:count[3]++; break;

Default:count[3]++;

}

Indirect Jumps (cont’d)

Turbo C assembly code for the switch statement

_main PROC NEAR. . .

mov AL,chcbwsub AX,48 ; 48 = ASCII for 0mov BX,AXcmp BX,3ja defaultshl BX,1 ; BX = BX * 2jmp WORD PTR CS:jump_table[BX]

Indirect jump

Page 22: Assembly Branches Loops and Arithmatics

22

Indirect Jumps (cont’d)case_0: inc WORD PTR [BP-10]

jmp SHORT end_switch case_1: inc WORD PTR [BP-8]

jmp SHORT end_switch case_2: inc WORD PTR [BP-6]

jmp SHORT end_switch case_3: inc WORD PTR [BP-4]

jmp SHORT end_switchdefault: inc WORD PTR [BP-2]end_switch:

. . ._main ENDP

Indirect Jumps (cont’d)jump_table LABEL WORD

DW case_0DW case_1DW case_2DW case_3

. . .

• Indirect jump uses this table to jump to the appropriate case routine

• The indirect jump instruction uses segment override prefix to refer to the jump_table in the CODE segment

Jump table for the indirect jump

Page 23: Assembly Branches Loops and Arithmatics

23

Conditional Jumps

• Three types of conditional jumps∗ Jumps based on the value of a single flag

» Arithmetic flags such as zero, carry can be tested using these instructions

∗ Jumps based on unsigned comparisons» Operands of cmp instruction are treated as unsigned

numbers

∗ Jumps based on signed comparisons» Operands of cmp instruction are treated as signed numbers

Conditional Jumps (cont’d)

Format:j<cond> lab

– Execution is transferred to the instruction identified by label only if <cond> is met

• Example: Testing for carriage returnread_char:

. . . cmp AL,0DH ; 0DH = ASCII carriage returnje CR_receivedinc CLjmp read_char

. . .CR_received:

Page 24: Assembly Branches Loops and Arithmatics

24

Jumps Based on Single Flags

Testing for zerojz jump if zero jumps if ZF = 1je jump if equal jumps if ZF = 1

jnz jump if not zero jumps if ZF = 0jne jump if not equal jumps if ZF = 0

jcxz jump if CX = 0 jumps if CX = 0(Flags are not tested)

Jumps Based on Single Flags (cont’d)

Testing for carryjc jump if carry jumps if CF = 1jnc jump if no carry jumps if CF = 0

Testing for overflowjo jump if overflow jumps if OF = 1jno jump if no overflow jumps if OF = 0

Testing for signjs jump if negative jumps if SF = 1jns jump if not negative jumps if SF = 0

Page 25: Assembly Branches Loops and Arithmatics

25

Jumps Based on Single Flags (cont’d)

Testing for parityjp jump if parity jumps if PF = 1jpe jump if parity jumps if PF = 1

is even

jnp jump if not parity jumps if PF = 0jpo jump if parity jumps if PF = 0

is odd

Jumps Based on Unsigned Comparisons

Mnemonic Meaning Conditionje jump if equal ZF = 1jz jump if zero ZF = 1

jne jump if not equal ZF = 0jnz jump if not zero ZF = 0

ja jump if above CF = ZF = 0jnbe jump if not below CF = ZF = 0

or equal

Page 26: Assembly Branches Loops and Arithmatics

26

Jumps Based on Unsigned Comparisons

Mnemonic Meaning Conditionjae jump if above CF = 0

or equaljnb jump if not below CF = 0

jb jump if below CF = 1jnae jump if not above CF = 1

or equal

jbe jump if below CF=1 or ZF=1or equal

jna jump if not above CF=1 or ZF=1

Jumps Based on Signed Comparisons

Mnemonic Meaning Conditionje jump if equal ZF = 1jz jump if zero ZF = 1

jne jump if not equal ZF = 0jnz jump if not zero ZF = 0

jg jump if greater ZF=0 & SF=OFjnle jump if not less ZF=0 & SF=OF

or equal

Page 27: Assembly Branches Loops and Arithmatics

27

Jumps Based on Signed Comparisons (cont’d)

Mnemonic Meaning Conditionjge jump if greater SF = OF

or equaljnl jump if not less SF = OF

jl jump if less SF ≠ OFjnge jump if not greater SF ≠ OF

or equal

jle jump if less ZF=1 or SF ≠ OFor equal

jng jump if not greater ZF=1 or SF ≠ OF

A Note on Conditional Jumps

target:. . .

cmp AX,BXje targetmov CX,10

. . .

traget is out of range for a short jump

• Use this code to get around

target:

. . . cmp AX,BXjne skip1jmp target

skip1:mov CX,10

. . .

• All conditional jumps are encoded using 2 bytes∗ Treated as short jumps

• What if the target is outside this range?

Page 28: Assembly Branches Loops and Arithmatics

28

Block-Structured IF Statements

Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example:

mov eax,op1cmp eax,op2jne L1body1jmp L2

L1: body2L2:

if( op1 == op2 )body1;

elsebody2;

Your turn . . .

Implement the following pseudocode in assembly language. All values are unsigned:

cmp ebx,ecxja nextbody

next:

if( ebx <= ecx ){

body}

(There are multiple correct solutions to this problem.)

Page 29: Assembly Branches Loops and Arithmatics

29

Your turn . . .

Implement the following pseudocode in assembly language. All values are 32-bit signed integers:

mov eax,var1cmp eax,var2jle L1mov var3,6mov var4,7jmp L2

L1: mov var3,10L2:

if( var1 <= var2 )var3 = 10;

else{

var3 = 6;var4 = 7;

}

(There are multiple correct solutions to this problem.)

Compound Expression with AND (1 of 3)

• We can implement a Boolean expression that uses the local AND operator:

• In the following example, if the first expression is false, the second expression is skipped;

• The Assembler will execute the body if both conditions are satisfied.

if (al > bl) AND (bl > cl){

body}

Page 30: Assembly Branches Loops and Arithmatics

30

Compound Expression with AND (2 of 3)

cmp al,bl ; first expression...ja L1jmp next

L1:cmp bl,cl ; second expression...ja L2jmp next

L2: ; both are truemov X,1 ; set X to 1

next:

if (al > bl) AND (bl > cl)X = 1;

This is one possible implementation . . .

Compound Expression with AND (3 of 3)

cmp al,bl ; first expression...jbe next ; quit if falsecmp bl,cl ; second expression...jbe next ; quit if falsemov X,1 ; both are true

next:

if (al > bl) AND (bl > cl)X = 1;

But the following implementation uses 29% less code by reversing the first relational operator. We allow the program to"fall through" to the second expression:

Page 31: Assembly Branches Loops and Arithmatics

31

Your turn . . .

Implement the following pseudocode in assembly language. All values are unsigned:

cmp ebx,ecxja nextcmp ecx,edxjbe nextmov eax,5mov edx,6

next:

if( ebx <= ecx&& ecx > edx )

{eax = 5;edx = 6;

}

(There are multiple correct solutions to this problem.)

Compound Expression with OR (1 of 2)

• We can implement a Boolean expression that uses the local OR operator

• In the following example, if the first expression is true, the second expression is skipped;

• The Assembler will execute the body if at least one condition is satisfied.

if (al > bl) OR (bl > cl)body;

Page 32: Assembly Branches Loops and Arithmatics

32

Compound Expression with OR (2 of 2)

cmp al,bl ; is AL > BL?ja L1 ; yescmp bl,cl ; no: is BL > CL?jbe next ; no: skip next statement

L1: mov X,1 ; set X to 1next:

if (al > bl) OR (bl > cl)X = 1;

We can use "fall-through" logic to keep the code as short as possible:

Loop Instructions

Unconditional loop instruction∗ Format:

loop target

∗ Semantics:» Decrements CX and jumps to target if CX ≠ 0

– CX should be loaded with a loop count value• Example: Executes loop body 50 times

mov CX,50repeat:

<loop body>loop repeat

...

Page 33: Assembly Branches Loops and Arithmatics

33

Loop Instructions (cont’d)

• The previous example is equivalent tomov CX,50

repeat:<loop body>dec CXjnz repeat

...∗ Surprisingly,

dec CXjnz repeat

executes faster than loop repeat

Loop Instructions (cont’d)

• Conditional loop instructions∗ loope/loopz

» Loop while equal/zeroCX = CX – 1If (CX = 0 and ZF = 1)

jump to target

∗ loopne/loopnz» Loop while not equal/not zero

CX = CX – 1If (CX = 0 and ZF = 0)

jump to target

Page 34: Assembly Branches Loops and Arithmatics

34

For Loops• Let’s translate the following loop:

sum = 0;for (i = 0; i <= 10; i++)

sum += i• Translation

mov eax, 0 ; eax is summov ebx, 0 ; ebx is i

loop_start:cmp ebx, 10 ; compare i and 10jg loop_end ; if (i > 10) goto end_loopadd eax, ebx ; sum += Iinc ebxjmp loop_start ; goto loop

loop_end:

For Loops

• Let’s translate the following loop:sum = 0;for (i = 0; i <= 10; i++)

sum += i• The x86 loop instruction requires that

∗ The loop index be stored in ecx∗ The loop index be decremented∗ The loop exists when the loop index is equal to zero

• Given this, we really have to think of this loop in reversesum = 0for (i = 10; i > 0; i--)

sum += i• This loop is equivalent to the previous one, but now it can be directly

translated to assembly using the loop instruction

Page 35: Assembly Branches Loops and Arithmatics

35

Using the loop Instruction

• Here is our “reversed” loopsum = 0for (i = 10; i > 0; i--)

sum += i• And the translation

mov eax, 0 ; eax is summov ecx, 10 ; ecx is i

loop_start:add eax, ecx ; sum += iloop loop_start ; if i > 0, go to loop_start

While Loops

• A generic while loopwhile (condition) {

body}

• Translated as:while:

; instructions to set flags (e.g., cmp...)jxx end_while ; branches if condition=false; body of loopjmp while

end_while

Page 36: Assembly Branches Loops and Arithmatics

36

Do While Loops

• A generic do while loopdo {

body} while (condition)

• Translated as:do:

; body of loop; instructions to set flags (e.g., cmp...)jxx do ; branches if condition=true

Logical And Shift Instructions

Page 37: Assembly Branches Loops and Arithmatics

37

Logical Instructions

∗ Format:and destination,sourceor destination,sourcexor destination,sourcenot destination

∗ Semantics:» Performs the standard bitwise logical operations

– result goes to destination ∗ test is a non-destructive and instruction

test destination,source

∗ Performs logical AND but the result is not stored in destination (like the CMP instruction)

Logical Instructions (cont’d)

Example:. . . and AL,01H ; test the least significant bitjz bit_is_zero

<bit 1 code>jmp skip1

bit_is_zero:<bit 0 code>

skip1:. . .

• test instruction is better in place of and

Page 38: Assembly Branches Loops and Arithmatics

38

Shift Instructions• Two types of shifts

» Logical» Arithmetic

∗ Logical shift instructionsShift left

shl destination,count shl destination,CL

Shift rightshr destination,count shr destination,CL

∗ Semantics:» Performs left/right shift of destination by the value in count or CL register

– CL register contents are not altered

Shift Instructions (cont’d)

Logical shift∗ Bit shifted out goes into the carry flag

» Zero bit is shifted in at the other end

Page 39: Assembly Branches Loops and Arithmatics

39

Shift Instructions (cont’d)

∗ count is an immediate valueshl AX,5

∗ Specification of count greater than 31 is not allowed» If a greater value is specified, only the least significant 5 bits

are used

∗ CL version is useful if shift count is known at run time» Ex: when the shift count value is passed as a parameter in a

procedure call» Only the CL register can be used

Shift count value should be loaded into CLmov CL,5shl AX,CL

Shift Instructions (cont’d)

Arithmetic shift∗ Two versions as in logical shift

sal/sar destination,countsal/sar destination,CL

Page 40: Assembly Branches Loops and Arithmatics

40

Double Shift Instructions

• Double shift instructions work on either 32- or 64-bit operands

• Format ∗ Takes three operands

shld dest,src,count ; left shiftshrd dest,src,count ; right shift

∗ dest can be in memory or register∗ src must be a register∗ count can be an immediate value or in CL as in other

shift instructions

Double Shift Instructions (cont’d)

∗ src is not modified by doubleshift instruction∗ Only dest is modified∗ Shifted out bit goes into the carry flag

Page 41: Assembly Branches Loops and Arithmatics

41

Rotate Instructions

∗ Two types of ROTATE instructions∗ Rotate without carry

» rol (ROtate Left)» ror (ROtate Right)

∗ Rotate with carry» rcl (Rotate through Carry Left)» rcr (Rotate through Carry Right)

∗ Format of ROTATE instructions is similar to the SHIFT instructions

» Supports two versions– Immediate count value– Count value in CL register

Rotate Instructions (cont’d)

∗ Bit shifted out goes into the carry flag as in SHIFT instructions

Page 42: Assembly Branches Loops and Arithmatics

42

Rotate Instructions (cont’d)

∗ Bit shifted out goes into the carry flag as in SHIFT instructions

Rotate Instructions (cont’d)

• Example: Shifting 64-bit numbers∗ Multiplies a 64-bit value in EDX:EAX by 16

» Rotate versionmov CX,4

shift_left:shl EAX,1rcl EDX,1loop shift_left

» Doubleshift versionshld EDX,EAX,4shl EAX,4

• Division can be done in a similar a way

Page 43: Assembly Branches Loops and Arithmatics

43

Illustrative Examples

• Five examples can be downloaded from the website∗ Conversion of ASCII to binary representation

(BINCHAR.ASM)∗ Conversion of ASCII to hexadecimal by character

manipulation (HEX1CHAR.ASM) ∗ Conversion of ASCII to hexadecimal using the XLAT

instruction (HEX2CHAR.ASM)∗ Conversion of lowercase letters to uppercase by

character manipulation (TOUPPER.ASM)∗ Sum of individual digits of a number

(ADDIGITS.ASM)Last slide