1 Chapter 3 Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Revised 3/11/05.

Slides:



Advertisements
Similar presentations
Introduction to Assembly Language
Advertisements

Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
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 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
1 Lecture 3: Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
CS2422 Assembly Language & System Programming September 26, 2006.
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Chapter 2 Software Tools and Assembly Language Syntax.
Fundamentals of Assembly language
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 3: Assembly Language Fundamentals
Assembly Fundamentals Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/13 with slides by Kip Irvine.
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
CoE3DJ4 Digital Systems Design
ASSEMBLY LANGUAGE. Assembler and Compiler Pascal A Program Compiler Version A Assembly Language Versiion A Machine Code Actual version that will be executed.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
ZONG Wen Department of Computer Science and Engineering The Chinese University of Hong Kong
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Debug and Assembler By, B.R.Chandavarkar Lect. COMP Department NITK, Surathkal.
Introduction to 8086 Assembly Language Assembly Language Programming University of Akron Dr. Tim Margush.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter Five–80x86 Assembly program development Principles of Microcomputers 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 1.
Assembly Language programming
Review of Assembly language. Recalling main concepts.
Lecture 2 Chapter 4 –Requirements for coding in Assembly Language 1.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
2/27/2003 Lecture 6: Assembly Language Fundamentals Assembly Language for Intel-Based Computers 4th edition Kip R. Irvine.
Assembly Language for x86 Processors 6th Edition
Assembly language programming
Format of Assembly language
Assembly Lab 3.
Assembly Language programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
x86 Assembly Language Fundamentals
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Assembly Language Fundamentals
Additional Assembly Programming Concepts
Microprocessor Systems Design I
Microprocessor and Assembly Language
INTRODUCTION ABOUT ASSEMBLY
Assembly Language for Intel-Based Computers, 5th Edition
Java Programming: From Problem Analysis to Program Design, 4e
Defining Types of data expression Dn [name] expression Dn [name]
Chapter 2: Basic Elements of Java
Assembly Language for Intel-Based Computers
Chapter 4 –Requirements for coding in Assembly Language
INTRODUCTION ABOUT ASSEMBLY
Chapter 4 –Requirements for coding in Assembly Language
Requirements for coding in Assembly Language
Assembler Directives end label end of program, label is entry point
Computer Architecture and System Programming Laboratory
Introduction to C Programming
Computer Organization and Assembly Language
Hardware & Software Architecture
Review of Java Fundamentals
Introduction to 8086 Assembly Language
Presentation transcript:

1 Chapter 3 Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Revised 3/11/05

2 Chapter 3 - Assembly Language Fundamentals u 3.2 Sample Program (See listing) u 3.1 Basic Elements of Assembly Language u 3.3 Assembling, Linking, and Running u 3.4 Defining Data u 3.5 Symbolic Constants u 3.6 Real-Adddress Mode Programming (omit for time being)

3 3.1 Basic Elements of Assembly Integer constants –-365 number is in decimal (the default for MASM) –99ddecimal –1001bbinary –47qoctal (or 47o) –2AFhhex –0AFhhex (hex numbers cannot begin with a letter) –MASM is normally case insensitive so 0AFh and 0afH and 0AFH are the same

4 3.1 Basic Elements of Assembly Arithmeic Operators and Expressions u + - * / MOD ( ) can be used in arithmetic expressions u These operators can only be used in constant expressions evaluated at assembly time u Examples – 98 10*20 -5 * 20 / – 17/3 (17/3 = 5)

5 Basic Elements of Assembly Operator Precedence u Highest ( ) u + - (unary) u * / MOD u Lowest + - (binary) u * 4 MOD 20 / MOD 20 / / Evaluate 24 – 12 / – MOD 5 * 2 Write an expression for

6 3.1 Basic Elements of Assembly Character and String Constants u ASCII characters and constants are enclosed in single or double quotes  Examples: "A" or 'A' 'xyz' or "XYZ" "I am learning assembly" "Don't do it"  "12345" is a string (5 bytes) is a number (2 or 4 bytes)

7 3.1 Basic Elements of Assembly Character and String Constants Problems u How many bytes in “CSCE 380” ‘Assembly language’ “He hasn’t come” ‘2/7/05’ 2005

8 3.1 Basic Elements of Assembly Statements u General form of instructions and data declarations u [label:] [mnemonic] [operand(s) ][; comment] [name] directive initializer [, initializer] …[; comment] u where [] indicate “sometimes may be omitted” u Examples: Value DWORD 13 ; Declare variable Value mov AX, x ; Copy x into AX Calc: add EBX, Value ; Add Value to EBX u Maximum length: 128 characters per line u Continuation character: \ Example: Matrix DWORD 1, 2, 3, \ 4, 5, 6

9 3.1 Basic Elements of Assembly Statements: Instructions u Converted into machine language at assembly time u Executed during run time  Sample statements mov EAX, x ; z = x y; add EAX, 10 sub EAX, y Store: mov z, EAX inc counter; counter++;

Basic Elements of Assembly Statements: Directives u Tell the assembler to declare variables or how you want the code generated or listing formatted u Examples: –TITLE This is the program title on printed listings … – X BYTE 10 –.code – main PROC

Basic Elements of Assembly Names and Labels u Identifies a label, variable, symbol or keyword u May contain letters, digits, "?", "_", or "$" u Names should not start with digits or u Case insensitive u Max of 247 characters u Examples (Legal): x, counter, Bill, Y2K, my_balance u Examples (Illegal): 12x, interest%, my balance, add

Basic Elements of Assembly Labels u Labels are used as markers for program jumps  Example: mov BX, X cmp BX, 0 jl Error ; jump if X < 0... Error: mov BX, Y

Basic Elements of Assembly Keywords and Directives u Keywords have predefined meanings and cannot be used for anything else u Directives tell the assembler what or how to do something u Examples of keywords: end, proc, EAX, add, sub, mov, DWORD u Example: The following is illegal Mov BYTE 21 ; Define Mov as a variable u MultiEd colors keywords

Assembling, Linking, and debugging Assembling and Running Programs Source file (.asm) Text editor Assembler Object file (.obj) Listing file (.lst) Linker Link library (.lib) Executable program (.exe) Map file (.map) Dos Loader Output make32

Assembling, Linking, and debugging Assembling and Linking u Two staged process: –Assemble: Converts assembly language into machine language. Uses source file, outputs object file and optionally the list file –Link: Connects (links) the different.obj and library files together to create the executable file. Uses the object file(s), and optionally, a library. Outputs executable file and optionally map file u Both done with make32.bat file

Assembling, Linking, and debugging Link library u Link libraries contain pre-assembled code u Widely used in high level languages for built-in functions such as "cout" in C+ + and imported methods from the Java library.

Assembling, Linking, and debugging MASM and make32 u make32 combines assembling and linking u Example make32 simple.asm u ml (Microsoft assembler and Linker) assembler –-Zi Include debug information for debuggers –-c Compile only, do not link –-Fl Produce listing file simple.lst –-coff Create 386 compatible.obj file –Options are CASE SENSITIVE! –You can use "-" or "/" for options – us/vcmasm/html/vcoriMicrosoftAssemblerMacroLanguage.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en- us/vcmasm/html/vcoriMicrosoftAssemblerMacroLanguage.asp

Assembling, Linking, and debugging MASM and make32 (cont.) u Link32 – 32 bit linker –Automatically includes Irvine32.lib u The ml linker only works for 16 bit code u See Appendix A

Data Allocation directives Defining variables u Symbolic name for a location in memory u During assembly, the value of a variable name is its offset from the beginning of the segment! u Examples:.data His Name BYTE "Joseph Brown" Cost DWORD 14 Price DWORD 7 HisName = 0 (it is at the beginning of the data segment) Cost = 12 (it comes right after 12 bytes in Name) Price = 16 (Cost is 4 bytes long)

Data Allocation directives Data Allocation Directives u DB, BYTE Define byte1 u DW, WORD Define word2 SWORD Define signed word u DD, DWORD Define doubleword4 SDWORD Define signed double word u DQ, QWORD Define quadword8 u DT, TBYTE Define tenbytes10

Data Allocation directives Little and Big Endian u Little Endian: Least significant bytes are stored first. u Big Endian: Most significant bytes are stored first. u Example: W: DWORD h Little endian Big endian W W u Intel uses little endian Memory

Data Allocation directives Data Allocations u General format [name] type initialvalue [, initialvalue] … u Initial value can be left undefined - use ?  Example:.data X BYTE 2, 24h Y WORD 18h, 126h Z BYTE ? 0 X 1 X+1 2 Y 3 4 Y Z Data segment ??

Data Allocation directives Characters and Strings u Characters can be represented by the character or by their ASCII value. u Values: Signed -128 to 127, Unsigned 0 to 255  Example: - 5 ways to store 'A': FiveAs BYTE 'A',"A",65,41h, b u Characters in strings are stored in order, one character per byte

Data Allocation directives DUP Operator u Used to assign multiple copies of the value  Example WORD 10 DUP (0) ; 10 zero words WORD 20 DUP (?) ; 20 unitialized words BYTE 30 DUP (' '); 30 blanks (bytes)  Example Matrix WORD 2 DUP(1, 2 DUP (2, 3)) generates: Matrix (20 bytes)

Data Allocation Memory allocation problems u Determine the contents of the.data segment..data X BYTE 2 DUP (3) Y BYTE “CATS” Z DWORD ‘DOGS’.code …. MOV EAX, 0 MOV AL, Y+1 u What memory location is Y stored in? u What memory location is Y+1 stored in? u What is the value of EAX?

Data Allocation directives Pointers  Consider MyString BYTE "This is a string" pMyString DWORD MyString u pMyString is a four byte pointer to MyString. It contains the offset of MyString within the data segment.

Symbolic constants Symbolic constants u Allow naming constants u Similar to C+ + code const int numElements = 20; const int numWords = 2 * numElements; or in Java public static final int numVal = 40; u Highly recommended! u They are not assigned storage locations in assembly u They can be defined anywhere in the program

Symbolic constants Equal-sign Directive  Examples numValues = 25 min = 5 max = min  Similar to C++ code const int numValues = 25; const int min = 5; const int max = min + 100; u IMPORTANT: The values of constants are substituted for the constant name during assembly time. They a NOT evaluated during execution time.

Symbolic constants Equal-sign directive  Examples: Cost = 10 mov AX, Cost This is the same as u mov AX, 10 u Equal sign directives can be used to define bytes, words, (and doublewords if the.386 directive is used).

Symbolic constants Equal-sign Directive - Redefinition u Equal-sign directives can be reassigned Example: C ost = Cost = Cost = Cost + 10

Symbolic constants EQU Directive u Can be used to define bytes and words (and doublewords if.386 is used) u Cannot be redefined  Examples MaxWord EQU NumValues EQU MaxWord/100 CR EQU 0Dh True EQU 1 ; define boolean values False EQU 0 No EQU 'NO'

Symbolic constants TEXTEQU Directive u Used to create a text macro. That is, text is substituted - not the value. u Formats: name TEXTEQU name TEXTEQU textmacro name TEXTEQU %constExpr u Used to define sequence of characters that will be substituted for "name" u Can be redefined

Symbolic constants TEXTEQU Directive  Example ContMsg TEXTEQU CR = 0Dh LF = 0Ah Ending = '$'  Then Msg BYTE ContMsg, CR, LF, Ending assembles as Msg BYTE "Press return", 0Dh, 0Ah, '$'

Symbolic constants TEXTEQU Directive  Example copy TEXTEQU ArithReg TEXTEQU Ten = 10 then copy ArithReg, Ten assembles as mov AX, 10

Symbolic constants Comparison  = and EQU - value substitution TEXTEQU - text substitution  = and TEXTEQU allow redefinition EQU does not  EQU allows floating point substitions = does not  EQU and TEXTEQU allow = does not

36