Data Transfers, Addressing, and Arithmetic

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Assembly Language for Intel-Based Computers, 5th Edition
1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Intrinsic Data Types (1 of 2) BYTE, SBYTE 8-bit unsigned.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
CS2422 Assembly Language & System Programming October 3, 2006.
Assembly Language for Intel-Based Computers
Basic Instructions Addressing Modes
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
CS2422 Assembly Language & System Programming September 28, 2006.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
Prof. Muhammad Saeed III. 1/27/2015Computer Architecture & Assembly Language2 Assembly Language Instructions.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Chapter 4: Data Transfers, Addressing, and Arithmetic.
CSC 221 Computer Organization and Assembly Language
Sahar Mosleh California State University San MarcosPage 1 JMP and Loops Memory Operand Move Instruction Array Data Related Operation and Directives.
Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.
Assembly Language for x86 Processors 6th Edition Chapter 4: Data-Related Operators and Directives, Addressing Modes (c) Pearson Education, All rights.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Basic Instructions Addressing Modes
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic Lecture 15: ADD, SUB, NEG and how they.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
In Class Program Write, assemble and test a program: –Use the DB directive to define the following list of numbers and name it array: 31h, 32h, 33h, 34h.
Assembly Language for x86 Processors 6th Edition
Assembly Language for Intel-Based Computers Chapter 4: Data Transfers, Addressing, and Arithmetic Kip Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Fall 2012 Chapter 4: Data Transfers, Addressing, and Arithmetic.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Assembly Language for x86 Processors 7th Edition
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
CSC 221 Computer Organization and Assembly Language Lecture 11: Addressing Modes in Assembly.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
3/10/2003 Lecture 9: Procedures Assembly Language for Intel-Based Computers 4th edition Kip R. Irvine.
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Data Transfers, Addressing, and Arithmetic
Microprocessor and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Assembly Language Lab (4).
Assembly Language for x86 Processors
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for x86 Processors 6th Edition
Assembly Language for x86 Processors 6th Edition
Data-Related Operators and Directives
Data Transfers, Addressing, and Arithmetic
Data Transfer, Addressing and Arithmetic
Basic Instructions.
Data Transfers, Addressing, and Arithmetic
Intel x86 Assembly Fundamentals
Data Transfer, Addressing and Arithmetic
Chapter 8: Instruction Set 8086 CPU Architecture
Presentation transcript:

Data Transfers, Addressing, and Arithmetic Assembly Language Data Transfers, Addressing, and Arithmetic

Data Transfer Instructions Operand Types Immediate Register memory

Instruction Operand Notation Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Direct Memory Operands For example .data var1 BYTE 10h .code mov al, var1 mov al, [var1] ; alternative notation

MOV Instruction Format: MOV destination, source Rules Both operands must be the same size Both operands cannot be memory operands CS, EIP, and IP cannot be destination operands

MOV Instruction General variants MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm

MOV Instruction Segment registers in real mode MOV r/m16, sreg MOV sreg, r/m16

MOVZX Instruction Move with zero-extend Variants MOVZX r32, r/m8

MOVSX Instruction Move with sign-extend Variants MOVSX r32, r/m8

LAHF SAHF Load the low byte of the EFLAGS register into AH Includes Sign, Zero, Auxiliary Carry, Parity, and Carry. SAHF Store AH into the low byte of the EFLAGS register

XCHG Instruction Rules Variants Both operands must be the same size Both operands cannot be memory operands CS, EIP, and IP cannot be destination operands Variants XCHG reg, reg XCHG reg, mem XCHG mem, reg

Direct-Offset Operands For example mov al, [arrayB+2] mov al, array+2 ; Alternative notation

Example: Data Transfer TITLE Data Transfer Examples (Moves.asm) ; Chapter 4 example. Demonstration of MOV and ; XCHG with direct and direct-offset operands. ; Last update: 06/01/2006. By K. R. Irvine. INCLUDE Irvine32.inc .data val1 WORD 1000h val2 WORD 2000h arrayB BYTE 10h,20h,30h,40h,50h arrayW WORD 100h,200h,300h arrayD DWORD 10000h,20000h

.code main PROC ; MOVZX mov bx,0A69Bh movzx eax,bx ; EAX = 0000A69Bh movzx edx,bl ; EDX = 0000009Bh movzx cx,bl ; CX = 009Bh ; MOVSX mov bx,0A69Bh movsx eax,bx ; EAX = FFFFA69Bh movsx edx,bl ; EDX = FFFFFF9Bh mov bl,7Bh movsx cx,bl ; CX = 007Bh

; Memory-to-memory exchange: mov ax,val1 ; AX = 1000h xchg ax,val2 ; AX = 2000h, val2 = 1000h mov val1,ax ; val1 = 2000h ; Direct-Offset Addressing (byte array): mov al,arrayB ; AL = 10h mov al,[arrayB+1] ; AL = 20h mov al,[arrayB+2] ; AL = 30h

; Direct-Offset Addressing (word array): mov ax,arrayW ; AX = 100h mov ax,[arrayW+2] ; AX = 200h ; Direct-Offset Addressing (doubleword array): mov eax,arrayD ; EAX = 10000h mov eax,[arrayD+4] ; EAX = 20000h mov eax,[arrayD+TYPE arrayD] ; EAX = 20000h exit main ENDP END main

Addition and Subtraction INC Instruction Increment by 1 Syntax INC reg/mem DEC Instruction Decrement by 1 DEC reg/mem

INC and DEC Instruction Overflow, Sign, Zero, Auxiliary Carry, and Parity flags are changed according to the value of the destination operand Do not affect the Carry flag

ADD Instruction Syntax General variants (like MOV) ADD dest, source MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm

SUB Instruction Syntax General variants (like MOV) SUB dest, source MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm

ADD and SUB Instructions Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags are changed according to the value of the destination operand

NEG Instruction Convert the number to its two’s complement Syntax NEG reg NEG mem Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags are changed according to the value of the destination operand

Flags Affected by Arithmetic The ALU has a number of status flags that reflect the outcome of arithmetic (and bitwise) operations based on the contents of the destination operand Essential flags: Zero flag – set when destination equals zero Sign flag – set when destination is negative Carry flag – set when unsigned value is out of range Overflow flag – set when signed value is out of range The MOV instruction never affects the flags. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Zero Flag (ZF) The Zero flag is set when the result of an operation produces zero in the destination operand. mov cx,1 sub cx,1 ; CX = 0, ZF = 1 mov ax,0FFFFh inc ax ; AX = 0, ZF = 1 inc ax ; AX = 1, ZF = 0 Remember... A flag is set when it equals 1. A flag is clear when it equals 0. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Sign Flag (SF) The Sign flag is set when the destination operand is negative. The flag is clear when the destination is positive. mov cx,0 sub cx,1 ; CX = -1, SF = 1 add cx,2 ; CX = 1, SF = 0 The sign flag is a copy of the destination's highest bit: mov al,0 sub al,1 ; AL = 11111111b, SF = 1 add al,2 ; AL = 00000001b, SF = 0 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Signed and Unsigned Integers A Hardware Viewpoint All CPU instructions operate exactly the same on signed and unsigned integers The CPU cannot distinguish between signed and unsigned integers YOU, the programmer, are solely responsible for using the correct data type with each instruction Added Slide. Gerald Cahill, Antelope Valley College Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Overflow and Carry Flags A Hardware Viewpoint How the ADD instruction modifies OF and CF: OF = (carry out of the MSB) XOR (carry into the MSB) CF = (carry out of the MSB) How the SUB instruction modifies OF and CF: NEG the source and ADD it to the destination CF = INVERT (carry out of the MSB) MSB = Most Significant Bit (high-order bit) XOR = eXclusive-OR operation NEG = Negate (same as SUB 0,operand ) Added Slide. Gerald Cahill, Antelope Valley College Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Carry Flag (CF) The Carry flag is set when the result of an operation generates an unsigned value that is out of range (too big or too small for the destination operand). mov al,0FFh add al,1 ; CF = 1, AL = 00 ; Try to go below zero: mov al,0 sub al,1 ; CF = 1, AL = FF The INC and DEC instructions do not affect the Carry flag. Applying NEG to a nonzero operand always sets the Carry flag. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Your turn . . . For each of the following marked entries, show the values of the destination operand and the Sign, Zero, and Carry flags: mov ax,00FFh add ax,1 ; AX= SF= ZF= CF= sub ax,1 ; AX= SF= ZF= CF= add al,1 ; AL= SF= ZF= CF= mov bh,6Ch add bh,95h ; BH= SF= ZF= CF= mov al,2 sub al,3 ; AL= SF= ZF= CF= 0100h 0 0 0 00FFh 0 0 0 00h 0 1 1 01h 0 0 1 FFh 1 0 1 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Overflow Flag (OF) The Overflow flag is set when the signed result of an operation is invalid or out of range. ; Example 1 mov al,+127 add al,1 ; OF = 1, AL = ?? ; Example 2 mov al,7Fh ; OF = 1, AL = 80h add al,1 The two examples are identical at the binary level because 7Fh equals +127. To determine the value of the destination operand, it is often easier to calculate in hexadecimal. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

A Rule of Thumb When adding two integers, remember that the Overflow flag is only set when . . . Two positive operands are added and their sum is negative Two negative operands are added and their sum is positive What will be the values of the Overflow flag? mov al,80h add al,92h ; OF = mov al,-2 add al,+127 ; OF = 1 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Your turn . . . What will be the values of the given flags after each operation? mov al,-128 neg al ; CF = OF = mov ax,8000h add ax,2 ; CF = OF = mov ax,0 sub ax,2 ; CF = OF = mov al,-5 sub al,+125 ; OF = 1 1 0 0 1 0 1 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Example: Addition and Subtraction TITLE Addition and Subtraction (AddSub3.asm) ; Chapter 4 example. Demonstration of ADD, SUB, ; INC, DEC, and NEG instructions, and how ; they affect the CPU status flags. ; Last update: 06/01/2006. By K. R. Irvine. INCLUDE Irvine32.inc .data Rval SDWORD ? Xval SDWORD 26 Yval SDWORD 30 Zval SDWORD 40

.code main PROC ; INC and DEC mov ax,1000h inc ax ; 1001h dec ax ; 1000h ; Expression: Rval = -Xval + (Yval - Zval) mov eax,Xval neg eax ; -26 mov ebx,Yval sub ebx,Zval ; -10 add eax,ebx mov Rval,eax ; -36

; Zero flag example: mov cx,1 sub cx,1 ; ZF = 1 mov ax,0FFFFh inc ax ; ZF = 1 ; Sign flag example: mov cx,0 sub cx,1 ; SF = 1 mov ax,7FFFh add ax,2 ; SF = 1

; Carry flag example: mov al,0FFh add al,1 ; CF = 1, AL = 00 ; Overflow flag example: mov al,+127 add al,1 ; OF = 1 mov al,-128 sub al,1 ; OF = 1 exit main ENDP END main

Data-Related Operators and Directives OFFSET Returns the offset of a data label For example mov esi, OFFSET bVal mov esi, OFFSET myArray+4

ALIGN Aligns a variable on a byte, word, doubleword, or paragraph boundary Syntax ALIGN bound bound can be 1, 2, 4, or 16. For example bVal BYTE ? ALIGN 2

PTR Override the declared size of an operand For example mov ax, WORD PTR myDouble mov ax, WORD PTR [myDouble+2] mov bl, BYTE PTR myDouble

TYPE Returns the size, in bytes, of a single element of a variable. LENGTHOF Counts the number of elements in an array, defined by the values appearing on the same line as its label. SIZEOF Returns a value that is equavalent to multiplying LENGTHOF by TYPE.

LABEL Insert a label and give it a size attribute without allocating any storage. For example .data val16 LABEL WORD val32 DWORD 12345678h

Example: TYPE, LENGTHOF TITLE Operators (Operator.asm) ; Demonstrates the TYPE, LENGTHOF, and SIZEOF operators ; Last update: 06/01/2006. By K. R. Irvine. INCLUDE Irvine32.inc .data byte1 BYTE 10,20,30 array1 WORD 30 DUP(?),0,0 array2 WORD 5 DUP(3 DUP(?)) array3 DWORD 1,2,3,4 digitStr BYTE '12345678',0 myArray BYTE 10,20,30,40,50, 60,70,80,90,100

; You can examine the following constant values ; by looking in the listing file (Operator.lst): ;--------------------------------------------- X = LENGTHOF byte1 ; 3 X = LENGTHOF array1 ; 30 + 2 X = LENGTHOF array2 ; 5 * 3 X = LENGTHOF array3 ; 4 X = LENGTHOF digitStr ; 9 X = LENGTHOF myArray ; 10 X = SIZEOF byte1 ; 1 * 3 X = SIZEOF array1 ; 2 * (30 + 2) X = SIZEOF array2 ; 2 * (5 * 3) X = SIZEOF array3 ; 4 * 4 X = SIZEOF digitStr ; 1 * 9

.code main PROC exit main ENDP END main

Exercise 4.7, 4. Overflow Flag Write a program that uses addition and subtraction to set and clear the Overflow flag. After each addition or subtraction instruction, insert the call DumpRegs statement to display the registers and flags. Using comments, explain how (and why) the Overflow flag was affected by each instruction. Optional: include an ADD instruction that sets both the Carry and Overflow flags. Reference: Page 92.

Example TITLE Chapter 4 Exercise 2 INCLUDE Irvine32.inc .data .code main PROC ; INC does not affect the carry flag: mov al,254 add al,1 ; AL=255, CF=0 call DumpRegs inc al ; AL=0, CF=0 (unchanged)

; DEC does not affect the carry flag: mov al,1 sub al,1 ; AL=0, CF=0 call DumpRegs dec al ; AL=255, CF=0 (unchanged) exit main ENDP END main

Indirect Addressing Indirect Operands Array Sum Example Indexed Operands Pointers Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Indirect Operands (1 of 2) An indirect operand holds the address of a variable, usually an array or string. It can be dereferenced (just like a pointer). .data val1 BYTE 10h,20h,30h .code mov esi,OFFSET val1 mov al,[esi] ; dereference ESI (AL = 10h) inc esi mov al,[esi] ; AL = 20h mov al,[esi] ; AL = 30h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Indirect Operands (2 of 2) Use PTR to clarify the size attribute of a memory operand. .data myCount WORD 0 .code mov esi,OFFSET myCount inc [esi] ; error: ambiguous inc WORD PTR [esi] ; ok Should PTR be used here? add [esi],20 yes, because [esi] could point to a byte, word, or doubleword Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Array Sum Example Indirect operands are ideal for traversing an array. Note that the register in brackets must be incremented by a value that matches the array type. .data arrayW WORD 1000h,2000h,3000h .code mov esi,OFFSET arrayW mov ax,[esi] add esi,2 ; or: add esi,TYPE arrayW add ax,[esi] add esi,2 add ax,[esi] ; AX = sum of the array ToDo: Modify this example for an array of doublewords. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Indexed Operands An indexed operand adds a constant to a register to generate an effective address. There are two notational forms: [label + reg] label[reg] .data arrayW WORD 1000h,2000h,3000h .code mov esi,0 mov ax,[arrayW + esi] ; AX = 1000h mov ax,arrayW[esi] ; alternate format add esi,2 add ax,[arrayW + esi] etc. ToDo: Modify this example for an array of doublewords. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Index Scaling* You can scale an indirect or indexed operand to the offset of an array element. This is done by multiplying the index by the array's TYPE: .data arrayB BYTE 0,1,2,3,4,5 arrayW WORD 0,1,2,3,4,5 arrayD DWORD 0,1,2,3,4,5 .code mov esi,4 mov al,arrayB[esi*TYPE arrayB] ; 04 mov bx,arrayW[esi*TYPE arrayW] ; 0004 mov edx,arrayD[esi*TYPE arrayD] ; 00000004 * Not in the book Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Pointers You can declare a pointer variable that contains the offset of another variable. .data arrayW WORD 1000h,2000h,3000h ptrW DWORD arrayW .code mov esi,ptrW mov ax,[esi] ; AX = 1000h Alternate format: ptrW DWORD OFFSET arrayW Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

JMP and LOOP Instructions JMP Instruction LOOP Instruction LOOP Example Summing an Integer Array Copying a String Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

JMP Instruction JMP is an unconditional jump to a label that is usually within the same procedure. Syntax: JMP target Logic: EIP  target Example: top: . jmp top A jump outside the current procedure must be to a special type of label called a global label (see Section 5.5.2.3 for details). Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

LOOP Instruction The LOOP instruction creates a counting loop Syntax: LOOP target Logic: ECX  ECX – 1 if ECX != 0, jump to target Implementation: The assembler calculates the distance, in bytes, between the offset of the following instruction and the offset of the target label. It is called the relative offset. The relative offset is added to EIP. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

LOOP Example The following loop calculates the sum of the integers 5 + 4 + 3 +2 + 1: offset machine code source code 00000000 66 B8 0000 mov ax,0 00000004 B9 00000005 mov ecx,5 00000009 66 03 C1 L1: add ax,cx 0000000C E2 FB loop L1 0000000E When LOOP is assembled, the current location = 0000000E (offset of the next instruction). –5 (FBh) is added to the the current location, causing a jump to location 00000009: 00000009  0000000E + FB Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Your turn . . . If the relative offset is encoded in a single signed byte, (a) what is the largest possible backward jump? (b) what is the largest possible forward jump? -128 +127 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Your turn . . . What will be the final value of AX? 10 mov ax,6 mov ecx,4 L1: inc ax loop L1 What will be the final value of AX? 10 How many times will the loop execute? mov ecx,0 X2: inc ax loop X2 4,294,967,296 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Nested Loop If you need to code a loop within a loop, you must save the outer loop counter's ECX value. In the following example, the outer loop executes 100 times, and the inner loop 20 times. .data count DWORD ? .code mov ecx,100 ; set outer loop count L1: mov count,ecx ; save outer loop count mov ecx,20 ; set inner loop count L2: . . loop L2 ; repeat the inner loop mov ecx,count ; restore outer loop count loop L1 ; repeat the outer loop Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Summing an Integer Array The following code calculates the sum of an array of 16-bit integers. .data intarray WORD 100h,200h,300h,400h .code mov edi,OFFSET intarray ; address of intarray mov ecx,LENGTHOF intarray ; loop counter mov ax,0 ; zero the accumulator L1: add ax,[edi] ; add an integer add edi,TYPE intarray ; point to next integer loop L1 ; repeat until ECX = 0 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

Copying a String The following code copies a string from source to target: .data source BYTE "This is the source string",0 target BYTE SIZEOF source DUP(0) .code mov esi,0 ; index register mov ecx,SIZEOF source ; loop counter L1: mov al,source[esi] ; get char from source mov target[esi],al ; store it in the target inc esi ; move to next character loop L1 ; repeat for entire string good use of SIZEOF Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.