Introduction to Assembly Language COE 205 Computer Organization
Introduction to Assembly Language COE 205 Computer Organization and Assembly Language Computer Engineering Department King Fahd University of Petroleum and Minerals
Presentation Outline v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 2
Constants v Integer Constants ² Examples: – 10, 42 d, 10001101 b, 0 FF 3 Ah, 777 o ² Radix: b = binary, d = decimal, h = hexadecimal, and o = octal ² If no radix is given, the integer constant is decimal ² A hexadecimal beginning with a letter must have a leading 0 v Character and String Constants ² Enclose character or string in single or double quotes ² Examples: 'A', "d", 'ABC', "ABC", '4096' ² Embedded quotes: "single quote ' inside", 'double quote " inside' ² Each ASCII character occupies a single byte Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 3
Assembly Language Statements v Three types of statements in assembly language ² Typically, one statement should appear on a line 1. Executable Instructions ² Generate machine code for the processor to execute at runtime ² Instructions tell the processor what to do 2. Assembler Directives ² Provide information to the assembler while translating a program ² Used to define data, select memory model, etc. ² Non-executable: directives are not part of instruction set 3. Macros ² Shorthand notation for a group of statements ² Sequence of instructions, directives, or other macros Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 4
Instructions v Assembly language instructions have the format: [label: ] mnemonic [operands] [; comment] v Instruction Label (optional) ² Marks the address of an instruction, must have a colon : ² Used to transfer program execution to a labeled instruction v Mnemonic ² Identifies the operation (e. g. MOV, ADD, SUB, JMP, CALL) v Operands ² Specify the data required by the operation ² Executable instructions can have zero to three operands ² Operands can be registers, memory variables, or constants Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 5
Instruction Examples v No operands stc ; set carry flag v One operand inc eax ; increment register eax call Clrscr ; call procedure Clrscr jmp ; jump to instruction with label L 1 v Two operands add ebx, ecx ; register ebx = ebx + ecx sub var 1, 25 ; memory variable var 1 = var 1 - 25 v Three operands imul eax, ebx, 5 ; register eax = ebx * 5 Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 6
Comments v Comments are very important! ² Explain the program's purpose ² When it was written, revised, and by whom ² Explain data used in the program ² Explain instruction sequences and algorithms used ² Application-specific explanations v Single-line comments ² Begin with a semicolon ; and terminate at end of line v Multi-line comments ² Begin with COMMENT directive and a chosen character ² End with the same chosen character Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 7
Next. . . v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 8
Flat Memory Program Template TITLE Flat Memory Program Template ; Program Description: ; Author: ; Modified by: (Template. asm) Creation Date: Modification Date: . 686. MODEL FLAT, STDCALL. STACK INCLUDE Irvine 32. inc. DATA ; (insert variables here). CODE main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedures here) END main Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 9
TITLE and. MODEL Directives v TITLE line (optional) ² Contains a brief heading of the program and the disk file name v. MODEL directive ² Specifies the memory configuration ² For our purposes, the FLAT memory model will be used § Linear 32 -bit address space (no segmentation) ² STDCALL directive tells the assembler to use … § Standard conventions for names and procedure calls v. 686 processor directive ² Used before the. MODEL directive ² Program can use instructions of Pentium P 6 architecture ² At least the. 386 directive should be used with the FLAT model Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 10
. STACK, . DATA, &. CODE Directives v. STACK directive ² Tells the assembler to define a runtime stack for the program ² The size of the stack can be optionally specified by this directive ² The runtime stack is required for procedure calls v. DATA directive ² Defines an area in memory for the program data ² The program's variables should be defined under this directive ² Assembler will allocate and initialize the storage of variables v. CODE directive ² Defines the code section of a program containing instructions ² Assembler will place the instructions in the code area in memory Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 11
INCLUDE, PROC, ENDP, and END v INCLUDE directive ² Causes the assembler to include code from another file ² We will include Irvine 32. inc provided by the author Kip Irvine § Declares procedures implemented in the Irvine 32. library § To use this library, you should link Irvine 32. lib to your programs v PROC and ENDP directives ² Used to define procedures ² As a convention, we will define main as the first procedure ² Additional procedures can be defined after main v END directive ² Marks the end of a program ² Identifies the name (main) of the program’s startup procedure Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 12
Next. . . v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 13
Adding and Subtracting Integers TITLE Add and Subtract (Add. Sub. asm) ; This program adds and subtracts 32 -bit integers. . 686. MODEL FLAT, STDCALL. STACK INCLUDE Irvine 32. inc. CODE main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h call Dump. Regs exit main ENDP END main Introduction to Assembly Language ; ; EAX = 10000 h EAX = 50000 h EAX = 30000 h display registers COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 14
Example of Console Output Procedure Dump. Regs is defined in Irvine 32. library It produces the following console output, showing registers and flags: EAX=00030000 EBX=7 FFDF 000 ECX=00000101 EDX=FFFF ESI=0000 EDI=0000 EBP=0012 FFF 0 ESP=0012 FFC 4 EIP=00401024 EFL=00000206 CF=0 Introduction to Assembly Language SF=0 ZF=0 COE 205 – Computer Organization and Assembly Language – KFUPM OF=0 © Muhamed Mudawar – slide 15
Suggested Coding Standards v Some approaches to capitalization ² Capitalize nothing ² Capitalize everything ² Capitalize all reserved words, mnemonics and register names ² Capitalize only directives and operators ² MASM is NOT case sensitive: does not matter what case is used v Other suggestions ² Use meaningful identifier names ² Use blank lines between procedures ² Use indentation and spacing to align instructions and comments § Use tabs to indent instructions, but do not indent labels § Align the comments that appear after the instructions Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 16
Understanding Program Termination v The exit at the end of main procedure is a macro ² Defined in Irvine 32. inc ² Expanded into a call to Exit. Process that terminates the program ² Exit. Process function is defined in the kernel 32 library ² We can replace exit with the following: push 0 ; push parameter 0 on stack call Exit. Process ; to terminate program ² You can also replace exit with: INVOKE Exit. Process, 0 v PROTO directive (Prototypes) ² Declares a procedure used by a program and defined elsewhere Exit. Process PROTO, Exit. Code: DWORD ² Specifies the parameters and types of a given procedure Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 17
Modified Program TITLE Add and Subtract (Add. Sub. Alt. asm) ; This program adds and subtracts 32 -bit integers. 686. MODEL flat, stdcall. STACK 4096 ; No need to include Irvine 32. inc Exit. Process PROTO, dw. Exit. Code: DWORD. code main PROC mov eax, 10000 h add eax, 40000 h sub eax, 20000 h push 0 call Exit. Process main ENDP END main Introduction to Assembly Language ; EAX = 10000 h ; EAX = 50000 h ; EAX = 30000 h ; to terminate program COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 18
Next. . . v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 19
Assemble-Link-Debug Cycle v Editor ² Write new (. asm) programs Edit ² Make changes to existing ones prog. asm v Assembler: ML. exe program ² Translate (. asm) file into object (. obj) file in machine language ² Can produce a listing (. lst) file that shows the work of assembler Assemble library. lib prog. obj v Linker: LINK 32. exe program Link ² Combine object (. obj) files with link library (. lib) files ² Produce executable (. exe) file prog. exe Debug ² Can produce optional (. map) file Introduction to Assembly Language prog. lst COE 205 – Computer Organization and Assembly Language – KFUPM prog. map Run © Muhamed Mudawar – slide 20
Assemble-Link-Debug Cycle – cont'd v MAKE 32. bat Edit ² Batch command file ² Assemble and link in one step prog. asm v Debugger: WINDBG. exe Assemble ² Trace program execution § Either step-by-step, or library. lib prog. obj prog. lst § Use breakpoints ² View Link § Source (. asm) code prog. exe § Registers § Memory by name & by address § Modify register & memory content Debug prog. map Run ² Discover errors and go back to the editor to fix the program bugs Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 21
Listing File v Use it to see how your program is assembled v Contains Object & source code in a listing file ² Source code ² Object code ² Relative addresses ² Segment names ² Symbols 00000000 00000005 0000000 A 0000000 F 00000011 00000016 B 8 00060000 05 00080000 2 D 00020000 6 A 00 E 8 0000 E § Variables . code main PROC mov eax, 60000 h add eax, 80000 h sub eax, 20000 h push 0 call Exit. Process main ENDP END main § Procedures § Constants Introduction to Assembly Language Relative Addresses object code (hexadecimal) COE 205 – Computer Organization and Assembly Language – KFUPM source code © Muhamed Mudawar – slide 22
Next. . . v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 23
Intrinsic Data Types v BYTE, SBYTE v REAL 4 ² 8 -bit unsigned integer ² IEEE single-precision float ² 8 -bit signed integer ² Occupies 4 bytes v WORD, SWORD v REAL 8 ² 16 -bit unsigned integer ² IEEE double-precision ² 16 -bit signed integer ² Occupies 8 bytes v DWORD, SDWORD v REAL 10 ² 32 -bit unsigned integer ² IEEE extended-precision ² 32 -bit signed integer ² Occupies 10 bytes v QWORD, TBYTE ² 64 -bit integer ² 80 -bit integer Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 24
Data Definition Statement v Sets aside storage in memory for a variable v May optionally assign a name (label) to the data v Syntax: [name] directive initializer [, initializer]. . . val 1 BYTE 10 v All initializers become binary data in memory Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 25
Defining BYTE and SBYTE Data Each of the following defines a single byte of storage: value 1 BYTE 'A' ; character constant value 2 BYTE 0 ; smallest unsigned byte value 3 BYTE 255 ; largest unsigned byte value 4 SBYTE -128 ; smallest signed byte value 5 SBYTE +127 ; largest signed byte value 6 BYTE ? ; uninitialized byte • MASM does not prevent you from initializing a BYTE with a negative value, but it's considered poor style. • If you declare a SBYTE variable, the Microsoft debugger will automatically display its value in decimal with a leading sign. Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 26
Defining Byte Arrays Examples that use multiple initializers list 1 BYTE 10, 20, 30, 40 list 2 BYTE 10, 20, 30, 40 BYTE 50, 60, 70, 80 BYTE 81, 82, 83, 84 list 3 BYTE ? , 32, 41 h, 0010 b list 4 BYTE 0 Ah, 20 h, 'A', 22 h Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 27
Defining Strings v A string is implemented as an array of characters ² For convenience, it is usually enclosed in quotation marks ² It is often terminated with a NULL char (byte value = 0) v Examples: str 1 BYTE "Enter your name", 0 str 2 BYTE 'Error: halting program', 0 str 3 BYTE 'A', 'E', 'I', 'O', 'U' greeting BYTE "Welcome to the Encryption " BYTE "Demo Program", 0 Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 28
Defining Strings – cont'd v To continue a single string across multiple lines, end each line with a comma menu BYTE "Checking Account", 0 dh, 0 ah, "1. Create a new account", 0 dh, 0 ah, "2. Open an existing account", 0 dh, 0 ah, "3. Credit the account", 0 dh, 0 ah, "4. Debit the account", 0 dh, 0 ah, "5. Exit", 0 ah, "Choice> ", 0 v End-of-line character sequence: ² 0 Dh = 13 = carriage return ² 0 Ah = 10 = line feed Introduction to Assembly Language Idea: Define all strings used by your program in the same area of the data segment COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 29
Using the DUP Operator v Use DUP to allocate space for an array or string ² Advantage: more compact than using a list of initializers v Syntax counter DUP ( argument ) Counter and argument must be constants expressions v The DUP operator may also be nested var 1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero var 2 BYTE 20 DUP(? ) ; 20 bytes, all uninitialized var 3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACK" var 4 BYTE 10, 3 DUP(0), 20 ; 5 bytes: 10, 0, 20 var 5 BYTE 2 DUP(5 DUP('*'), 5 DUP('!')) ; '*****!!!!!' Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 30
Defining 16 -bit and 32 -bit Data v Define storage for 16 -bit and 32 -bit integers ² Signed and Unsigned ² Single or multiple initial values word 1 word 2 word 3 array 1 array 2 dword 1 value dword 2 array 3 array 4 WORD SWORD DWORD 65535 – 32768 "AB" 1, 2, 3, 4, 5 5 DUP(? ) 0 ffffh ; ; ; largest unsigned 16 -bit value smallest signed 16 -bit value two characters fit in a WORD array of 5 unsigned words array of 5 signed words largest unsigned 32 -bit SDWORD – 2147483648 ; smallest signed 32 -bit value DWORD 20 DUP(? ) ; 20 unsigned double words SDWORD – 3, – 2, – 1, 0, 1 ; 5 signed double words Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 31
QWORD, TBYTE, and REAL Data v QWORD and TBYTE ² Define storage for 64 -bit and 80 -bit integers ² Signed and Unsigned v REAL 4, REAL 8, and REAL 10 ² Defining storage for 32 -bit, 64 -bit, and 80 -bit floating-point data quad 1 val 1 r. Val 2 r. Val 3 array QWORD TBYTE REAL 4 REAL 8 REAL 10 REAL 4 Introduction to Assembly Language 12345678 h 100000123456789 Ah -2. 1 3. 2 E-260 4. 6 E+4096 20 DUP(0. 0) COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 32
Symbol Table v Assembler builds a symbol table ² So we can refer to the allocated storage space by name ² Assembler keeps track of each name and its offset ² Offset of a variable is relative to the address of the first variable v Example Symbol Table. DATA Name Offset value WORD 0 value 0 sum DWORD 0 sum 2 marks WORD 10 DUP (? ) msg BYTE 'The grade is: ', 0 char 1 BYTE ? Introduction to Assembly Language char 1 marks 6 msg 26 40 COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 33
Byte Ordering and Endianness v Processors can order bytes within a word in two ways v Little Endian Byte Ordering ² Memory address = Address of least significant byte ² Examples: Intel 80 x 86 MSB LSB Byte 3 Byte 2 Byte 1 Byte 0 a+1 a+2 a+3 address a. . . Byte 0 Byte 1 Byte 2 Byte 3 32 -bit Register . . . Memory v Big Endian Byte Ordering ² Memory address = Address of most significant byte ² Examples: MIPS, Motorola 68 k, SPARC MSB LSB Byte 3 Byte 2 Byte 1 Byte 0 32 -bit Register Introduction to Assembly Language a+1 a+2 a+3 address a. . . Byte 3 Byte 2 Byte 1 Byte 0. . . Memory COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 34
Adding Variables to Add. Sub TITLE Add and Subtract, Version 2 (Add. Sub 2. asm). 686. MODEL FLAT, STDCALL. STACK INCLUDE Irvine 32. inc. DATA val 1 DWORD 10000 h val 2 DWORD 40000 h val 3 DWORD 20000 h result DWORD ? . CODE main PROC mov eax, val 1 ; start with 10000 h add eax, val 2 ; add 40000 h sub eax, val 3 ; subtract 20000 h mov result, eax ; store the result (30000 h) call Dump. Regs ; display the registers exit main ENDP END main Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 35
Next. . . v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 36
Defining Symbolic Constants v Symbolic Constant ² Just a name used in the assembly language program ² Processed by the assembler pure text substitution ² Assembler does NOT allocate memory for symbolic constants v Assembler provides three directives: ² = directive ² EQU directive ² TEXTEQU directive v Defining constants has two advantages: ² Improves program readability ² Helps in software maintenance: changes are done in one place Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 37
Equal-Sign Directive v Name = Expression ² Name is called a symbolic constant ² Expression is an integer constant expression v Good programming style to use symbols COUNT = 500. . . mov eax, COUNT. . . COUNT = 600. . . mov ebx, COUNT ; NOT a variable (NO memory allocation) ; mov eax, 500 ; Processed by the assembler ; mov ebx, 600 v Name can be redefined in the program Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 38
EQU Directive v Three Formats: Name EQU Expression Integer constant expression Name EQU Symbol Existing symbol name Name EQU <text> Any text may appear within < …> SIZE EQU 10*10 ; Integer constant expression PI EQU <3. 1416> ; Real symbolic constant Press. Key EQU <"Press any key to continue. . . ", 0>. DATA prompt BYTE Press. Key v No Redefinition: Name cannot be redefined with EQU Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 39
TEXTEQU Directive v TEXTEQU creates a text macro. Three Formats: Name TEXTEQU <text> assign any text to name Name TEXTEQU textmacro assign existing text macro Name TEXTEQU %const. Expr constant integer expression v Name can be redefined at any time (unlike EQU) ROWSIZE COUNT MOVAL Cont. Msg. DATA prompt. CODE MOVAL = 5 TEXTEQU %(ROWSIZE * 2) ; evaluates to 10 <mov al, COUNT> <"Do you wish to continue (Y/N)? "> BYTE Cont. Msg Introduction to Assembly Language ; generates: mov al, 10 COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 40
Next. . . v Basic Elements of Assembly Language v Flat Memory Program Template v Example: Adding and Subtracting Integers v Assembling, Linking, and Debugging Programs v Defining Data v Defining Symbolic Constants v Data-Related Operators and Directives Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 41
OFFSET Operator v OFFSET = address of a variable within its segment ² In FLAT memory, one address space is used for code and data ² OFFSET = linear address of a variable (32 -bit number). DATA b. Val w. Val d. Val 2 BYTE WORD DWORD . CODE mov esi, ? ? OFFSET Introduction to Assembly Language ; Assume b. Val is at 00404000 h b. Val w. Val d. Val 2 ; ; ESI ESI = = 00404000 h 00404001 h 00404003 h 00404007 h COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 42
ALIGN Directive v ALIGN directive aligns a variable in memory v Syntax: ALIGN bound ² Where bound can be 1, 2, 4, or 16 v Address of a variable should be a multiple of bound v Assembler inserts empty bytes to enforce alignment. DATA b 1 BYTE ALIGN 2 w 1 WORD w 2 WORD ALIGN 4 d 1 DWORD d 2 DWORD ? ? ? Introduction to Assembly Language ; ; ; ; Assume that Address of b 1 = Skip one byte Address of w 1 = Address of w 2 = Skip two bytes Address of d 1 = Address of d 2 = 00404000 h 00404002 h 00404004 h 00404008 h 0040400 Ch COE 205 – Computer Organization and Assembly Language – KFUPM 40400 C d 2 404008 d 1 w 2 404004 w 1 404000 b 1 © Muhamed Mudawar – slide 43
TYPE Operator v TYPE operator ² Size, in bytes, of a single element of a data declaration . DATA var 1 BYTE ? var 2 WORD ? var 3 DWORD ? var 4 QWORD ? . CODE mov eax, Introduction to Assembly Language TYPE var 1 var 2 var 3 var 4 ; ; eax eax = = 1 2 4 8 COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 44
LENGTHOF Operator v LENGTHOF operator ² Counts the number of elements in a single data declaration. DATA array 1 array 2 array 3 digit. Str. code mov ecx, Introduction to Assembly Language WORD DWORD BYTE LENGTHOF 30 DUP(? ), 0, 0 5 DUP(3 DUP(? )) 1, 2, 3, 4 "12345678", 0 array 1 array 2 array 3 digit. Str ; ; COE 205 – Computer Organization and Assembly Language – KFUPM ecx ecx = = 32 15 4 9 © Muhamed Mudawar – slide 45
SIZEOF Operator v SIZEOF operator ² Counts the number of bytes in a data declaration ² Equivalent to multiplying LENGTHOF by TYPE. DATA array 1 array 2 array 3 digit. Str. CODE mov ecx, Introduction to Assembly Language WORD DWORD BYTE SIZEOF 30 DUP(? ), 0, 0 5 DUP(3 DUP(? )) 1, 2, 3, 4 "12345678", 0 array 1 array 2 array 3 digit. Str COE 205 – Computer Organization and Assembly Language – KFUPM ; ; ecx ecx = = 64 30 16 9 © Muhamed Mudawar – slide 46
Multiple Line Declarations A data declaration spans multiple lines if each line (except the last) ends with a comma In the following example, array identifies the first line WORD declaration only The LENGTHOF and SIZEOF operators include all lines belonging to the declaration Compare the values returned by LENGTHOF and SIZEOF here to those on the left . DATA array WORD 10, 20, 30, 40, 50, 60 . DATA array . CODE mov eax, LENGTHOF array ; 6 mov ebx, SIZEOF array ; 12 . CODE mov eax, LENGTHOF array ; 2 mov ebx, SIZEOF array ; 4 Introduction to Assembly Language WORD 10, 20 WORD 30, 40 WORD 50, 60 COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 47
PTR Operator v PTR Provides the flexibility to access part of a variable v Can also be used to combine elements of a smaller type v Syntax: Type PTR (Overrides default type of a variable). DATA dval DWORD 12345678 h array BYTE 00 h, 10 h, 20 h, 30 h. CODE mov al, mov ax, mov eax, dval BYTE PTR dval WORD PTR dval array DWORD PTR array Introduction to Assembly Language ; ; ; dval array 78 56 34 12 00 10 20 30 error – why? al = 78 h error – why? ax = 5678 h error – why? eax = 30201000 h COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 48
LABEL Directive v Assigns an alternate name and type to a memory location v LABEL does not allocate any storage of its own v Removes the need for the PTR operator v Format: Name LABEL Type. DATA dval LABEL DWORD wval LABEL WORD blist BYTE 00 h, 10 h, 00 h, 20 h. CODE ; eax = 20001000 h mov eax, dval ; cx = 1000 h mov cx, wval ; dl = 00 h mov dl, blist Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM blist 00 10 00 20 wval dval © Muhamed Mudawar – slide 49
Summary v Instruction executed at runtime v Directive interpreted by the assembler v. STACK, . DATA, and. CODE ² Define the code, data, and stack sections of a program v Edit-Assemble-Link-Debug Cycle v Data Definition ² BYTE, WORD, DWORD, QWORD, etc. ² DUP operator v Symbolic Constant ² =, EQU, and TEXTEQU directives v Data-Related Operators ² OFFSET, ALIGN, TYPE, LENGTHOF, SIZEOF, PTR, and LABEL Introduction to Assembly Language COE 205 – Computer Organization and Assembly Language – KFUPM © Muhamed Mudawar – slide 50
- Slides: 50