Data-Related Operators and Directives

Slides:



Advertisements
Similar presentations
Introduction to Assembly Language
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
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.
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.
Target Processor Directives , When using.386, the program can only run on 386 and above processors.
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.
Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
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.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic (c) Pearson Education, All rights.
Coding.
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.
CS2422 Assembly Language & System Programming September 26, 2006.
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
Chapter 4: Data Transfers, Addressing, and Arithmetic.
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.
Assembly Language for x86 Processors 6th Edition Chapter 4: Data-Related Operators and Directives, Addressing Modes (c) Pearson Education, All rights.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
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.
Assembly Language for x86 Processors 6th Edition
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Assembly Language for Intel-Based Computers Chapter 4: Data Transfers, Addressing, and Arithmetic Kip Irvine.
Fall 2012 Chapter 4: Data Transfers, Addressing, and Arithmetic.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
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.
CS2422 Assembly Language and System Programming 0 Week 7 & 8 Intro. To Assembly Programming.
Addressing Modes Dr. Hadi Hassan.  Two Basic Questions  Where are the operands?  How memory addresses are computed?  Intel IA-32 supports 3 fundamental.
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.
Chapter 8 String Operations. 8.1 Using String Instructions.
Agenda Lab 4 cont: Data-Related Operators and Directives Boolean and comparison instructions Conditional JMP LOOP Conditional LOOP MASM directives Hands.
Stack Operations Dr. Hadi AL Saadi.
Assembly Language for Intel-Based Computers, 4th Edition
Assembly language.
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Computer Organization And Assembly Language
Chapter 9.
Assembly Language for Intel-Based Computers, 5th Edition
Intel x86 Assembly Fundamentals
Assembly Language for x86 Processors 7th Edition
Assembly IA-32.
Assembly Language Lab (4).
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for x86 Processors 6th Edition
Computer Organization and Assembly Language
Assembly Language for x86 Processors 6th Edition
Data Transfers, Addressing, and Arithmetic
Data Transfer, Addressing and Arithmetic
Stack Frames and Advanced Procedures
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Basic Instructions.
Data Transfers, Addressing, and Arithmetic
Intel x86 Assembly Fundamentals
Data Transfer, Addressing and Arithmetic
Hardware & Software Architecture
More on operators and procedures in the Linked
Computer Architecture and System Programming Laboratory
Presentation transcript:

Data-Related Operators and Directives Week 10 Data-Related Operators and Directives

Interpreted by assembler What's Next Data Transfer Instructions Addition and Subtraction Data-Related Operators and Directives OFFSET Operator PTR Operator TYPE Operator LENGTHOF Operator SIZEOF Operator LABEL Directive Indirect Addressing JMP and LOOP Instructions Interpreted by assembler 1

OFFSET Operator OFFSET is to get the memory address of variables during program execution It returns the distance in bytes of a label from the beginning of its enclosing segment Protected mode: 32 bits Real mode: 16 bits For Example: If your computer is in real mode, software communicates directly with the computer's ports and devices. However, If your computer is in protected mode, the system's ports and devices are protected from the applications that use them. The software thinks it's sending data to a port, but it's a virtual port. The OS is grabbing the data stream and managing it, to ensure that all applications have equal access and to ensure that data from each application is appropriately preserved. The protected-mode programs that we write only have a single segment (we use the flat memory model) 2

OFFSET Examples Let's assume that the data segment begins at 00404000h: .data bVal BYTE ? wVal WORD ? dVal DWORD ? dVal2 DWORD ? .code mov esi,OFFSET bVal ; ESI = 00404000 mov esi,OFFSET wVal ; ESI = 00404001 mov esi,OFFSET dVal ; ESI = 00404003 mov esi,OFFSET dVal2 ; ESI = 00404007

OFFSET Example .data bVal byte 1 wVal word 2 dVal dword 3 .code main PROC mov al, bval mov bx, wVal mov ecx, dVal mov edx, dVal2 call DumpRegs mov eax, offset bval mov ebx, offset wVal mov ecx, offset dVal mov edx, offset dVal2 exit main ENDP 4

OFFSET Example Let's assume that the data segment begins at 00404000h Result of execution: …  EAX=75944801  EBX=7FFD0002  ECX=00000003  EDX=00000004 ESI=00000000  EDI=00000000  EBP=0012FF94  ESP=0012FF8C EIP=0040102D  EFL=00000246  CF=0  SF=0  ZF=1  OF=0   EAX=00404000  EBX=00404001  ECX=00404003  EDX=00404007 EIP=00401046  EFL=00000246  CF=0  SF=0  ZF=1  OF=0 … 5

PTR Operator Overrides default type of a label (variable) and provides the flexibility to access part of a variable Recall that little endian order is used when storing data in memory (see next slides) .data myDouble DWORD 12345678h .code mov ax,myDouble ; error – why? mov ax,WORD PTR myDouble ; loads 5678h mov WORD PTR myDouble,4321h ; saves 4321h

Little Endian Order Little endian order refers to the way Intel stores integers in memory. Multi-byte integers are stored in reverse order, with the least significant byte stored at the lowest address For example, the doubleword 12345678h would be stored as: When integers are loaded from memory into registers, the bytes are automatically re-reversed into their correct positions.

PTR Operator Examples .data myDouble DWORD 12345678h mov al,BYTE PTR myDouble ; AL = 78h mov al,BYTE PTR [myDouble+1] ; AL = 56h mov al,BYTE PTR [myDouble+2] ; AL = 34h mov ax,WORD PTR myDouble ; AX = 5678h mov ax,WORD PTR [myDouble+2] ; AX = 1234h

PTR Operator (cont) PTR can also be used to combine elements of a smaller data type and move them into a larger operand The processor will automatically reverse the bytes .data myBytes BYTE 12h,34h,56h,78h .code mov ax,WORD PTR [myBytes] ; AX = 3412h mov ax,WORD PTR [myBytes+2] ; AX = 7856h mov eax,DWORD PTR myBytes ; EAX = 78563412h

Your Turn . . . Write down value of each destination operand: .data varB BYTE 65h,31h,02h,05h varW WORD 6543h,1202h varD DWORD 12345678h .code mov ax,WORD PTR [varB+2] ; a. mov bl,BYTE PTR varD ; b. mov bl,BYTE PTR [varW+2] ; c. mov ax,WORD PTR [varD+2] ; d. mov eax,DWORD PTR varW ; e. 0502h 78h 02h 1234h 12026543h

TYPE Operator The TYPE operator returns the size, in bytes, of a single element of a data declaration .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 Operator The LENGTHOF operator counts the number of elements in a single data declaration .data LENGTHOF 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 "12345678",0 ; 9 .code mov ecx,LENGTHOF array1 ; 32

SIZEOF Operator SIZEOF returns a value that is equivalent to multiplying LENGTHOF by TYPE. .data SIZEOF 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 "12345678",0 ; 9 .code mov ecx,SIZEOF array1 ; 64

Spanning Multiple Lines (1 of 2) A data declaration spans multiple lines if each line (except the last) ends with a comma. The LENGTHOF and SIZEOF operators include all lines belonging to the declaration: .data array WORD 10,20, 30,40, 50,60 .code mov eax,LENGTHOF array ; 6 mov ebx,SIZEOF array ; 12

Spanning Multiple Lines (2 of 2) In the following example, array identifies only the first WORD declaration. Compare the values returned by LENGTHOF and SIZEOF here to those in the previous slide: .data array WORD 10,20 WORD 30,40 WORD 50,60 .code mov eax,LENGTHOF array ; 2 mov ebx,SIZEOF array ; 4

Relationships Between Assembly Lang. Concept