Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions.

Slides:



Advertisements
Similar presentations
Registers of the 8086/ /2002 JNM.
Advertisements

Department of Computer Science and Software Engineering
Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label.
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
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, 5 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
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.
Basic Instructions Addressing Modes
Flow Control Instructions
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.
Data Transfers, Addressing, and Arithmetic
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate 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.
Intel x86 Assembly Fundamentals Computer Organization and Assembly Languages Yung-Yu Chuang with slides by Kip Irvine.
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.
Faculty of Engineering, Electrical Department,
Basic Instructions Addressing Modes
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
Arithmetic Flags and Instructions
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.
Click to add Title Comunicación y Gerencia Click To add Subtitle Click to add Text Fundamentals of Assembly Language.
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.
Assembly Language for x86 Processors 6th Edition
Review of Assembly language. Recalling main concepts.
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Assembly Language for Intel-Based Computers Chapter 4: Data Transfers, Addressing, and Arithmetic Kip Irvine.
Fall 2012 Chapter 4: Data Transfers, Addressing, and Arithmetic.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Assembly Language for x86 Processors 7th Edition
CSC 221 Computer Organization and Assembly Language Lecture 11: Addressing Modes in Assembly.
1 Using the Assembler Chapter – 4(A). 2 Exchanging Two Variables title Exchange Two Variables (Exchange.asm).model small.stack 100h.data value1 db 0Ah.
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
Homework Reading Labs PAL, pp
Microprocessor and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Assembly IA-32.
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
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Basic Instructions.
Data Transfers, Addressing, and Arithmetic
Intel x86 Assembly Fundamentals
Computer Architecture and System Programming Laboratory
Chapter 6 –Symbolic Instruction and Addressing
Data Transfer, Addressing and Arithmetic
Presentation transcript:

Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions

Data Transfer Instructions MOV is for moving data between:  Memory  Register  Immediate (constant) Almost all combinations, except:  Memory to Memory!

MOV Instruction.data count BYTE 100 wVal WORD 2.code mov bl,count mov ax,wVal mov count,al mov al,wVal; error mov ax,count; error mov eax,count; error Move from source to destination. Syntax: MOV destination,source No more than one memory operand permitted CS, EIP, and IP cannot be the destination No immediate to segment moves

Your turn....data bVal BYTE 100 bVal2 BYTE ? wVal WORD 2 dVal DWORD 5.code mov ds,45; a. mov esi,wVal; b. mov eip,dVal; c. mov 25,bVal; d. mov bVal2,bVal; e. Explain why each of the following MOV statements are invalid:

Memory to Memory? Must go through a register ….data Var1 WORD? Var2 WORD ?.code MOV AX, var1 MOV var2, AX

MOV Instruction Format

Instruction Operand Notation

Zero or Sign Extension What happens to ECX if – 1 is moved from CX?  Are the higher 16 bits of ECX all 0?  What number does ECX represent now? The solution: MOVZX and MOVSX  MOVZX always fills higher bits with 0.  MOVSX fills higher bits by “ sign extension ”.

Zero Extension mov bl, b movzx ax,bl ; zero-extension When you copy a smaller value into a larger destination, the MOVZX instruction fills (extends) the upper half of the destination with zeros. The destination must be a register.

MOVZX Instruction Format

Sign Extension mov bl, b movsx ax,bl ; sign extension The MOVSX instruction fills the upper half of the destination with a copy of the source operand's sign bit. The destination must be a register.

MOVSX Instruction Format

XCHG XCHG for exchange data between:  Register, register  Register, memory  Memory, register (again, no memory to memory)

Direct-Offset Operands Adding a displacement (or offset) to a variable name: arrayB BYTE 10h, 20h, 30, 40h, 50h … MOV AL, arrayB; AL=10h MOV AL, [arrayB+1]; AL=20h MOV AL, arrayB+1; Is it valid?

Your turn... Write a program that rearranges the values of three doubleword values in the following array as: 3, 1, 2..data arrayD DWORD 1,2,3 Step 2: Exchange EAX with the third array value and copy the value in EAX to the first array position. Step1: copy the first value into EAX and exchange it with the value in the second position. mov eax,arrayD xchg eax,[arrayD+4] xchg eax,[arrayD+8] mov arrayD,eax

Evaluate this.data myBytes BYTE 80h,66h,0A5h How about the following code. Is anything missing? movzx ax,myBytes mov bl,[myBytes+1] add ax,bx mov bl,[myBytes+2] add ax,bx; AX = sum Yes: Move zero to BX before the MOVZX instruction.

Addition and Subtraction ADD X, Y X := X + Y SUB X, Y X := X – Y

INC, DEC, NEG INC X X := X + 1 or X++ DEC X X := X – 1 or X-- NEG X X := –X

Expression Example: X=(A + B) * (D – E) MOVEAX, A ADDEAX, B MOVECX, D SUBECX, E IMULEAX, ECX MOVX, EAX

Flags Affected Flags (register) tell us whether any of the following conditions occur:  Overflow,  Carry,  Zero, Sign … etc. Used for decision in branch.  Loop (discussed next)  If … then … else

Zero and Sign Zero Flag ZF=1 if the instruction produce 0. MOV CX, 1 SUB CX, 1; CX=0, ZF=1 Sign Flag SF=1 if the instruction produce a negative number. MOV CX, 0 SUB CX, 1; CX=-1, SF=1 ADD CX, 2; CX=1, SF=0

Carry (Unsigned Arithmetic) 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). Example: MOV AL, 0FFh ADD AL, 1; CF = 1, AL=00 MOVAX, 00FFh ADDAX, 1; CF = 0, AX=0100h

Overflow (Signed Arithmetic) The Overflow flag is set when the signed result of an operation is invalid or out of range. Example: MOV AL, +127 ADD AL, 1; OF = 1 MOVAL, -128 SUBAL, 1; OF = 1

Detecting Carry Detecting Carry is easy.  Adding two N-bit numbers result in an (N+1)-bit number. Example: CF is ignored for signed arithmetic. For example, the above is 4 + (-1) in decimal

Detecting Overflow Carry isn ’ t meaningful for signed arithmetic. For example, adding any two negative numbers always produces carry. Detecting Overflow:  Compare CF and the bit carried into MSB (Most Significant Bit).

Overflow in Positive Numbers Carry never happens. Overflow occurs if MSB becomes (127) (1) (127) (1) Observation:  MSB=1 indicates a negative number.  But, we ’ re adding two positive numbers … ?!

Overflow in Negative Numbers Carry always happens. Overflow occurs if MSB becomes (-128) (-1) (-1) (-1) Observation:  MSB=0 indicates a positive number.  But, we ’ re adding two negative numbers … ?!

Detecting Overflow Overflow: CF  MSB ?  Doesn ’ t work if adding a positive number to a negative number (or vice versa)! Overflow: (CF  MSB) and not the case of (positive+negavive) positive+negavive:  Overflow never happens.  Carry happens when carry-in to MSB Overflow: CF  (carry-in to MSB)

Flags Affect in ADD, SUB Carry: unsigned arithmetic out of range Overflow: signed arithmetic out of range Sign: result is negative Zero: result is zero Auxiliary Carry: carry from bit 3 to bit 4 Parity: sum of 1 bits is an even number

LAHF/SAHF LAHF: load the low byte of EFLAGS register into AH. SAHF: store the low byte of EFLAGS register into AH.

Data Related Operators Who are they?  OFFSET, PTR, TYPE, LENGTHOF, SIZEOF They are only understood by the assembler. They are not instructions!

Operand Sizes Operands may have the size of 1 byte, 2 bytes, or 4 bytes. Most of time, we can tell the size from the register names or the variable definition. For examples: Var1BYTE “Hello” MOV ECX, 13 MOV AL, Var1

PTR But sometimes we want to override the default. myDoubleDWORD h MOV AL, myDouble ; error MOV AL, BYTE PTR myDouble MOV AX, WORD PTR myDouble MOV AX, WORD PTR [myDouble+2] MOV EAX, myDouble

OFFSET OFFSET returns the distance in bytes, of a label from the beginning of its enclosing segment Assume that the data segment begins at h:.data bVal BYTE ? wVal WORD ? dVal DWORD ? dVal2 DWORD ?.code mov esi,OFFSET bVal ; ESI = mov esi,OFFSET wVal ; ESI = mov esi,OFFSET dVal ; ESI = mov esi,OFFSET dVal2 ; ESI =

TYPE TYPE returns the size (in bytes) of each element..data var1 BYTE ? var2 WORD ? var3 DWORD ? var4 QWORD ?.code mov eax,TYPE var1; 1 mov eax,TYPE var2; 2 mov eax,TYPE var3; 4 mov eax,TYPE var4; 8

LENGTHOF LENGTHOF returns the number of elements..data byte1 BYTE 10,20,30; 3 array1 WORD 30 DUP(?),0,0; 32 array2 WORD 5 DUP(3 DUP(?)); 15 array3 DWORD 1,2,3,4; 4 digitStr BYTE " ",0; 9.code mov ecx,LENGTHOF array1; 32

SIZEOF SIZEOF returns the size of the variable (the whole array).  SIZEOF = LENGTHOF * TYPE.dataSIZEOF byte1 BYTE 10,20,30; 3 array1 WORD 30 DUP(?),0,0; 64 array2 WORD 5 DUP(3 DUP(?)); 30 array3 DWORD 1,2,3,4; 16 digitStr BYTE " ",0; 9.code mov ecx,SIZEOF array1; 64

Indirect Operands 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 inc esi mov al,[esi]; AL = 30h

Array Sum Example.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; increment ESI by 2 add ax,[esi]; AX = sum of the array

Indexed Operands 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]

Pointers.data arrayW WORD 1000h,2000h,3000h ptrW DWORD arrayW.code mov esi,ptrW mov ax,[esi]; AX = 1000h

Implementation of Loops JMP instruction: Unconditional Branch. LOOP instruction:  Step 1: Set ECX to n for a loop of n iterations.  Step 2: Use LOOP instruction at the end of loop.  Hidden action: DEC ECX

Example: Summation For I := 10 downto 1 {Sum := Sum+I} MOVECX, 10 MOVEAX, 0 L1:ADDEAX, ECX LOOP L1

Your turn What will be the final value of AX? mov ax,6 mov ecx,4 L1: inc ax loop L1 How many times will the loop execute? mov ecx,0 X2: inc ax loop X2 10 4,294,967,296 (=2 32 )

Copying a String.data source BYTE "This is the source string",0 target BYTE SIZEOF source DUP(0),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

Nested Loop.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