1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

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

COMP 2003: Assembly Language and Digital Logic
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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.
Lect 3: Instruction Set and Addressing Modes. 386 Instruction Set (3.4) –Basic Instruction Set : 8086/8088 instruction set –Extended Instruction Set :
1 Using the Assembler Chapter 4 n Operators and Expressions n JMP and LOOP Instructions n Indirect Addressing n Using a Link Library.
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Target Processor Directives , When using.386, the program can only run on 386 and above processors.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Irvine: Assembly Language for Intel-Based Computers (1999) Symbolic Constants Equal-sign Directive EQU Directive TEXTEQU Directive.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
CS2422 Assembly Language & System Programming October 3, 2006.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
Basic Instructions Addressing Modes
CS2422 Assembly Language & System Programming September 28, 2006.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Microprocessor Systems Design I
Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions.
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
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
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,
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
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.
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.
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.
Review of Assembly language. Recalling main concepts.
Assembly Language for Intel-Based Computers Chapter 4: Data Transfers, Addressing, and Arithmetic Kip Irvine.
Fall 2012 Chapter 4: Data Transfers, Addressing, and Arithmetic.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
CSC 221 Computer Organization and Assembly Language Lecture 11: Addressing Modes in Assembly.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Intel MP Organization. Registers - storage locations found inside the processor for temporary storage of data 1- Data Registers (16-bit) AX, BX, CX, DX.
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.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
Data Transfers, Addressing, and Arithmetic
Microprocessor Systems Design I
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Assembly IA-32.
Assembly Language Lab (4).
ADDRESSING MODES.
Assembly Language for Intel-Based Computers, 4th Edition
Symbolic Instruction and Addressing
Chapter 4: Instructions
BIC 10503: COMPUTER ARCHITECTURE
Data-Related Operators and Directives
Data Transfers, Addressing, and Arithmetic
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
The Microprocessor & Its Architecture
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Basic Instructions.
University of Gujrat Department of Computer Science
Computer Architecture CST 250
Chapter 6 –Symbolic Instruction and Addressing
CSC 497/583 Advanced Topics in Computer Security
Chapter 8: Instruction Set 8086 CPU Architecture
(Array and Addressing Modes)
Presentation transcript:

1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine

Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

Data Transfer Instructions n Operand Types  Immediate operands (imm): The data is a constant  Register operands (reg): The data item is in a register --- Very fast. Efficient  Memory operands (mem): The data item is in memory ---Slower. SEE pp for details

Data Transfer Instructions n MOV Instruction  Copies data  General format MOV destination, source  Formats - legal operands MOV reg, reg MOV reg, mem MOV mem, reg MOV reg, immed MOV mem, immed  immed means a constant

 Source and destination must have the same size  "reg" can be any register except  Does not change flag  Note: Memory to memory moves are not allowed Data Transfer Instructions n MOV Instruction Rules  IP  Segment registers use special rules - to be used only when the program runs in real mode - CS cannot be a target operand - immediate values are not allowed

Data Transfer Instructions n MOV Instruction Examples.data bVal db 20 wVal dw 0AAAAh dVal DWORD 0FEDCBA09h.code … mov AL, 10 mov EAX, 10 mov BX, wVal mov dVal, ECX ;.386 required mov CX, bVal mov DH, 1234 ; illegal Immediate operands do not have a length attribute

Data Transfer Instructions n Zero/Sign Extension of Integers  MOVZX Instruction  MOVSX Instruction  Copies the contents of a source operand into a destination operand and zero-extends the value to either 16 or 32 bits  Only used with unsigned integers  Copies the contents of a source operand into a destination operand and sign-extends the value to either 16 or 32 bits  Only used with signed integers

Data Transfer Instructions n Zero/Sign Extension of Integers Examples.data bVal db 20 wVal SWORD 8AAAh dVal DWORD 0FEDCBA09h.code … mov AL, bVal mov BX, wVal movzx ECX, AL movSX EDX, wVal movSX,EAX, 1234 ECX=? EDX=? Anything wrong?

Data Transfer Instructions n XCHG Instruction  XCHG exchanges the contents of source and destingation  Format xchg reg, reg xchg reg, mem xchg mem, reg  Examples: xchg AX, BX xchg AH, bData xchg wData, CX  mov's rules apply, does not change flags

Data Transfer Instructions n LAHF and SAHF Instructions  LAHF  SAHF  Copies the low byte of the EFLAGS register into AH  Examples  Copies AH into the low byte of the EFLAGS register.data saveflag BYTE ?.code lahf mov saveflag, ah

Data Transfer Instructions n Operands with displacement  Recall: to the assembler, variables names are memory offsets - number values that it can calculate with!  Example. data List word 10, 11, 12.code... mov AX, List + 2 mov [List + 4], AX With a bracket or without are both ok 0 List 1 2 List List+4 5 0Ah 00 0Bh 00 0Ch 00 Data segment (initial)

Data Transfer Instructions n Direct-offset Operands  Really just a variation of direct addressing  Include + or - or even [] after variable name  Example Msg db "abcdef"... mov AL, Msg ; AL = __ mov AL, Msg+0 ; AL = __ mov BL, Msg+1 ; BL = __ mov CL, Msg[4]; CL = __

 Example.386.model flat.data List dd 10h, 20h, 30h, 40h X dd 2h Y dd 1234h … mov EBX, List[8]; EBX = mov EAX, List+4 ; EAX = mov ECX, X-2 ; ECX = Data Transfer Instructions n Direct-offset Operands

Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

n INC and DEC Addition and Subtraction  Used to add or subtract 1  Format inc destination dec destination where destination is a register or memory  Examples inc AX ; increment AX dec bVal ; decrement bVal  Changes flags except carry flag

n ADD and SUB Addition and Subtraction  Used to add or subtract add destination, source Adds the source to the destination sub destination, source Subtracts the source from the destination  All of "mov"s rules apply (e.g. no memory to memory operations allowed).  Status flags are affected

n Examples for ADD and SUB Addition and Subtraction Compile x = a + b - 10 a= 10 b= 5... mov AX, a add AX, b sub AX, 10 mov x, AX ; x = __

n NEG Instruction Addition and Subtraction  Convert a number to its 2’s complment  Format neg reg neg mem  Examples neg AX neg bVal  Status flags are affected

n Flags Addition and Subtraction  Several flags are set after the arithmetic operations ADD, SUB, INC, and DEC  The CPU does not know if the calculations are signed or unsigned so both sets of flags are set  Zero flag is set to 1 if the result of the calculation is 0, cleared to 0 if the result is nonzero

n Flags Addition and Subtraction  The negative flag is set to the leading bit of the result  Unsigned arithmetic: The carry flag is set if the result is too large or too small as a unsigned number  Signed arithmetic: The overflow flag is set if the result is too large or too small as a signed number

mov AX, 10 ; AX = __, flags unchanged add AX, 20 ; AX = __, Z=0, S=0, C=0, O=0 sub AX, 31 ; AX = __, Z=_, S=_, C=_, O=_ inc AX ; AX = __, Z=_, S=_, C=_, O=_ add AX, ; AX= ____, Z=_, S=_, C=_, O=_ n Flag Examples Addition and Subtraction

n Flags Addition and Subtraction  The addition test for OVERFLOW  Two positive operands were added and their sum is negative  Two negative operands were added and their sum is positive  NEG – may produce an invalid result if the destination operand cannot be stored correctly Mov al, -128 neg al

Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

Data-related Operators n OFFSET Operator  Return the offset of a data label. The offset represents the distance, in bytes, of the label from the beginning of the data segment  An offset is 32-bit for the protected mode and 16-bit for the real mode

Data-related Operators n OFFSET Operator  Example.data List word 10h, 20h, 30h, 40h X db 2h Y dword 1234h … mov ESI, offset List[8]; ESI= mov ESI, offset X ; ESI= mov ESI, Y ; ESI = Assuming the List were located at offset h

Data-related Operators n PTR Operator  PTR – override the default size of an operand  Example code: Y db FFh X dw 20, 13 … inc byte ptr Y mov ax, Y ; ax= Inc word ptr Y mov ax, Y ; ax = ?

Data-related Operators n TYPE Operator  It returns the size, in bytes, of a variable:. data var2 DW 1, 2, 3 var4 DD 4.code mov BX, TYPE var2 ;BX = 2 mov BX, TYPE var4 ;BX= 4 Handy for array processing. Ex: If SI points to an element of var2, then to make SI point to the next element, we can simply write: add SI, TYPE var2

Data-related Operators n LENGTHOF Operator  Counts the number of elements in array, defined by the values appearing on the same line as its label..data var2 DB 1, 2, 3 DB 4, 5, 6 var4 DD 5 DUP(3 DUP(?)), 10, 20, 30.code mov BX, lengthof var2 ;BX = mov AX, lengthof var4 ;AX=

Data-related Operators n SIZEOF Operator  Returns a value that is equivalent to multiplying LEGNTHOF by TYPE..data var2 DB 1, 2, 3 DB 4, 5, 6 var4 DD 5 DUP(3 DUP(?)), 10, 20, 30.code mov BX, sizeof var2 ;BX = mov AX, sizeof var4 ;AX=

Data-related Directives n ALIGN Directive  Align a variable on a byte, word, doubleword, or paragraph boundary.  If bound=1, the next variable is aligned on a 1-byte boundary  If bound=2, the next variable is aligned on an even- numbered address  If bound=4, the next address is multiple of 4  The CPU process data stored at even- numbered addresses faster than those at odd-numbered addresses Format: ALIGN bound

Data-related Directives n LABEL Directive  It gives a name and a size to an existing storage location. It does not allocate storage.  It must be used in conjunction with byte, word, dword, qword... 0 bData 1 0A 00 Data segment wData.data bData label byte wData dw 0Ah.code mov AL, wData ; illegal mov AL, bData ; works fine mov AX, wData ; works fine  bData is just an alias for the first byte of the storage location wData

Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

Indirect Addressing n Problems  Add all word elements in an array called List.  Solution: mov AX, 0 add AX, List add AX, List+2... add AX, List+98  What if the array has 1000 elements?  Solution: use a register as a pointer and find ways to manipulate the register’s value

–Example: mov AX, [BX] Indirect Addressing n Notation: [reg]  REG may be EAX, EBX, ECX, EDX, ESI, EBP, and ESP to contain the offset of some data. Move the value whose address is in BX to AX Move the value pointed by BX to AX AX = 600 BX= 25

Indirect Addressing n Examples.data List dw 1, 3, 10, 6, 2, 9, 2, 8, 9 Number = ($ - List)/2.code … ; sum values in list mov AX, 0 ; sum = 0 mov CX, Number ; number of values mov SI, OFFSET List ; ptr to List L3: add AX, [SI] ; add value add SI, 2 ; point to next value loop L3 ; repeat as needed

Indirect Addressing n Based and Indexed Addressing  A displacement (constant) is added to the base or indexed value to get the offset  Notation: Register added to offset variable[reg] Good notation if reg holds [reg+variable] the "subscript". [variable+reg]  Notation: Register added to constant constant[reg] Good notations if the register [reg+constant] holds the offset of the [constant+reg] variable  A register holds the offset and the other holds the “subscript” [reg1+reg2]

Indirect Addressing n Based and Indexed Addressing Examples.data List word … mov AX, List[SI] AX SI4 List List+2 List+4 List+6 List+8 List+10 List+ 12 Memory 100

mov SI, OFFSET List mov AX, 4[SI] AX SI OFFSET List List List+2 List+4 List+6 List+8 List+10 List+12 Memory 100 Indirect Addressing n Based and Indexed Addressing Examples

Indirect Addressing n Based and Indexed Addressing Examples mov BX, OFFSET List mov SI, 4 mov AX, [BX+SI] AX SI 4 List List+2 List+4 List+6 List+8 List+10 List+12 Memory 100 BX OFFSET List +

Array dw 11, 12, 13, 14, 15 dw 21, 22, 23, 24, 25 dw 31, 32, 33, 34, 35 NumCol = 5 … mov BX, NumCol mov SI, 3 mov AX, Array[BX+SI] ; mov AX, Array[BX][SI] Indirect Addressing n Based and Indexed Addressing Examples AX = ?

Indirect Addressing n Pointers MyString db "This is a string" pMyString dw MyString  pMyString is a word pointer to MyString. It contains the offset of MyString within the data segment.

Indirect Addressing n Pointer Examples Array dw 11h, 12h, 13h, 14h, 15h Y dw 21h X dw 3145h, 32h, 33h pArray dword Array pY dword Y … mov esi, pArray mov eax, 3[esi] mov esi, pY mov ebx, 2[esi]

Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

n Transfer of Control  Unconditional branch: The new location is always loaded into the IP. Example: JMP (jump)  Conditional branch: The new location is loaded into the IP only if some condition is satisfied. Example: JZ (jump if zero)

JMP and LOOP Instructions n JMP  Instruction JMP targetLabel  0005 E jmp L [00] db 100h DUP (0) 0108 EB 04 L1: jmp L2 010A db 1,2,3,4 010E B4 01 L2: mov AH, CD 21 int 21h 0112 A R mov Char, AL 0115 EB F7 jmp L2  Jump calculations Old IP A 0117 Offset FFF7 New IP E 010E

JMP and LOOP Instructions n LOOP, LOOPW, and LOOPD  The loop instructions are the easiest way to set up a loop  They use CX or ECX as the counter  Action: decrement CX or ECX. Jump if the new register value is not 0  Offset is one byte long (-128 to +127)  LOOP uses CX if in 16 bit mode, ECX if in 32 bit mode  LOOPW uses CX, LOOPD uses ECX

JMP and LOOP Instructions n Examples – Summing the integer array Array dw 11h, 12h, 13h, 14h, 15h … mov esi, offset Array mov eax, 0 mov ecx, lengthof Array L1: add eax, [esi] add esi, 2 loop L1 … n Exercise – backward copying a string

Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions