Download presentation
Presentation is loading. Please wait.
1
1 Lecture 3: Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine
2
Outline Basic Elements of AssemblyBasic Elements of Assembly Sample ProgramSample Program Assembling, Linking, and RunningAssembling, Linking, and Running Defining DataDefining Data Symbolic ConstantsSymbolic Constants
3
Basic Elements of Assembly n Integer Constants Made up of an optional leading sign, one or more digits, and an optional suffix character (radix) A leading zero is required when a hex constant begins with a letter 6715q ; Octal 0BAF1h ; leading zero required
4
Basic Elements of Assembly n Integer Expressions A mathematical expression involving integer values and arithmetic operators Examples –98 –10*20 –-5 * 20 / 40 + 5
5
Basic Elements of Assembly n Real Number Constants A decimal real constant contains a sign followed by an integer, a decimal point, an integer that expresses a fraction, and an exponent. A encoded real (hex) shows the IEEE standard floating-point representation with the radix r + 4.0 -44.2E+05 4F800000r ; what is the number in decimal?
6
Basic Elements of Assembly n Character and String Constants 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) 12345 is a number (2 or 4 bytes)
7
Basic Elements of Assembly n Identifiers A programmer-chosen name to identify either: a label, a variable, a symbolic constant or a procedure May contain letters, digits, "?", "_", "@", or "$" should not start with digits or "@”, cannot be the same as the reserved word in assembly Case insensitive Max of 247 characters Which is legal /illegal? x, 12x, counter, Bill, Y2K, interest%, add
8
Basic Elements of Assembly n Directives Tell the assembler how you want the code to be generated or listing formatted. Example: title Hello World Program (hello.asm). stack 100h. code set the stack size directive giving title for printed listings starts the code segment
9
Basic Elements of Assembly n Instructions General form of a statement. [label] [mnemonic] [operands] [; comment] where [] indicate "sometimes may be omitted" Label – used as a marker for instructions or data. mov BX, X cmp BX, 0 jl Error ; jump if X < 0... Error: mov BX, Y Code label
10
Basic Elements of Assembly n Instructions Comments. Single-line comments begin with a semicolon Block comments begin with the COMMENT directive and a user-specified symbol ; This program displays “Hello, world!” COMMENT ! This program displays “Hello, world!” This line is also a comment !
11
Outline Basic Elements of AssemblyBasic Elements of Assembly Sample ProgramSample Program Assembling, Linking, and RunningAssembling, Linking, and Running Defining DataDefining Data Symbolic ConstantsSymbolic Constants
12
Sample Program Title Add and Subtract ; This program adds and subtracts 32-bit integer Include Irvine32.inc.model flat.data.code main proc mov eax, 10000h add eax, 40000h sub eax, 20000h call DumpRegs exit main endp end main Instructs the assembler to generate code for a protected mode program starts the data segment where variables are stored starts the code segment Declares the beginning of the procedure which is called main calls the Irvine library procedure to display registers marks the end of the current procedure marks the end of the program to be assembled Calls a predefined MS-Window function
13
n See the program template in pp.76 Sample Program
14
Outline Basic Elements of AssemblyBasic Elements of Assembly Sample ProgramSample Program Assembling, Linking, and RunningAssembling, Linking, and Running Defining DataDefining Data Symbolic ConstantsSymbolic Constants
15
Assembling, Linking and Running 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 ML
16
n Assembling Assembling, Linking and Running 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 Both done with ML command
17
n MASM ML combines assembling and linking Example ml -Zi -Fl -Fm filename.asm -Zi Include debug information for Codeview -Fl Produce listing file simple.lst -Fm Produce map file simple.map Options are CASE SENSITIVE! You can use "-" or "/" for options Assembling, Linking and Running ALL THESE COMMANDS HAVE BEEN INCLUDED IN THE MAKE32.BAT – make32 filename
18
Defining Data n Directives (see pp. 80-81) DB, BYTE8-bit unsigned integer SBYTE8-bit singed integer DW, WORD16-bit unsigned integer SWORD 16-bit signed integer DWORD32-bit integer SDWORD32-bit signed integer REAL432-bit IEEE short real REAL864-bit IEEE long real
19
Defining Data Symbolic name for a location in memory During assembly, the value of a variable name is its offset from the beginning of the segment! Examples: Name byte "Joe Brown" Cost word 14
20
Defining Data n General Format [name] directive initializer [, initializer] … Initializer is required in a data definition. But the initial value can be left undefined use “?”.data X byte 2, 24h Y word 18h, 126h Z byte ? How is the data filled in the data segment ? 012 3456012 3456 Data segment X X+1 Y Y+2 Z 02 24 18 00 26 01 ??
21
Defining Data n Characters and Strings Characters can be represented by the character or their ASCII value. Example : 5 ways to store 'A': FiveAs db‘A',"A",65,41h,01000001b String examples Name db "My name is Sally", \ 0Dh, 0Ah Continuation character: \ “0Dh” and “0Ah” are carriage return and line feed Characters in strings are stored in order, one character per byte
22
Defining Data n DUP Operator Used to assign multiple copies of the value Example dw 10 DUP (0) ; 10 zero words dq 20 DUP (?) ; 20 unitialized quads db 30 DUP (' '); 30 blanks (bytes) Example Matrix dw 2 DUP(1, 2 DUP (2, 3)) generates:Matrix 1 2 3 2 3 1 2 3 2 3 (20 bytes)
23
Outline Basic Elements of AssemblyBasic Elements of Assembly Sample ProgramSample Program Assembling, Linking, and RunningAssembling, Linking, and Running Defining DataDefining Data Symbolic ConstantsSymbolic Constants
24
n Symbolic Constants Symbolic Constants Allow naming constants Similar to C+ + code const int numElements = 20; const int numWords = 2 * numElements; or in Java public static final int numVal = 40; They are not assigned storage locations in assembly They can be defined anywhere in the program
25
Symbolic Constants n Equal-sign Directive Examples numValues = 25 min = 5 max = min + 100 Similar to C++ code const int numValues = 25; const int min = 5; const int max = min + 100; IMPORTANT: The values of constants are substituted for the constant name during assembly time. They a NOT evaluated during execution time.
26
Symbolic Constants n Equal-sign Directive Examples: Cost = 10 mov AX, Cost This is the same as mov AX, 10 Equal-sign directives can be reassigned Cost = 10... Cost = 20... Cost = Cost + 10
27
Symbolic Constants n EQU Directive: name EQU Formats: name EQU expressions name EQU symbol Expression must be a valid integer expression Symbol must be a existing symbol name, already defined with = or EQU Cannot be redefined
28
Symbolic Constants n TEXTEQU Directive: Formats: name TEXTEQU %constExpr name TEXTEQU name TEXTEQU textmacro Used to create a text macro Can be redefined
29
Symbolic Constants n TEXTEQU Directive: Example ContMsg TEXTEQU CR = 0Dh LF = 0Ah Ending = '$' Then Msg db ContMsg, CR, LF, Ending assembles as Msg db "Press return", 0Dh, 0Ah, '$'
30
Symbolic Constants n 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
31
Symbolic Constants n $ Operator The $ operator returns the current value of the location counter. We can use it to compute the string length at assembly time..data LongString db “This is a piece of text that I“ db “want to type on 2 separate lines” LongString_length = ($ - LongString) Note that we do not need to give a name to every line...
32
Outline Basic Elements of AssemblyBasic Elements of Assembly Sample ProgramSample Program Assembling, Linking, and RunningAssembling, Linking, and Running Defining DataDefining Data Symbolic ConstantsSymbolic Constants
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.