Lecture 3 Assembly Language Fundamentals Assembly Language for
Lecture 3: Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4 th edition Kip R. Irvine 1
Outline q Basic Elements of Assembly q Sample Program q Assembling, Linking, and Running q Defining Data q Symbolic Constants
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 6715 q 0 BAF 1 h ; Octal ; leading zero required
Basic Elements of Assembly n Integer Expressions Ø A mathematical expression involving integer values and arithmetic operators Ø Examples – 98 – 10*20 – -5 * 20 / 40 + 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. + 4. 0 -44. 2 E+05 Ø A encoded real (hex) shows the IEEE standard floating-point representation with the radix r 4 F 800000 r ; what is the number in decimal?
Basic Elements of Assembly n Character and String Constants Ø ASCII characters and constants are enclose 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)
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, 12 x, counter, Bill, Y 2 K, interest%, add
Basic Elements of Assembly n Directives Ø Tell the assembler how you want the code to be generated or listing formatted. Ø Example: directive giving title for printed listings title Hello World Program. stack 100 h. code (hello. asm) set the stack size starts the code segment
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. Error: mov cmp jl. . . mov BX, X Code label BX, 0 Error ; jump if X < 0 BX, Y
Basic Elements of Assembly n Instructions Ø Comments. q Single-line comments begin with a semicolon q 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 !
Outline q Basic Elements of Assembly q Sample Program q Assembling, Linking, and Running q Defining Data q Symbolic Constants
Sample Program Title Add and Subtract ; This program adds and subtracts 32 -bit integer Instructs the assembler to generate Include Irvine 32. inc code for a protected mode program. model flat starts the data segment where variables. data are stored. code starts the code segment main proc Declares the beginning of the procedure mov eax, 10000 h which is called main add eax, 40000 h calls the Irvine library sub eax, 20000 h procedure to display registers call Dump. Regs Calls a predefined MS-Window function exit main endp marks the end of the current procedure end main marks the end of the program to be assembled
Sample Program n See the program template in pp. 76
Outline q Basic Elements of Assembly q Sample Program q Assembling, Linking, and Running q Defining Data q Symbolic Constants
Assembling, Linking and Running Listing file (. lst) Link library (. lib) Text editor Source file (. asm) Assembler Object file (. obj) ML Linker Map file (. map) Dos Loader Output Executable program (. exe)
Assembling, Linking and Running n Assembling ¨ 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
Assembling, Linking and Running 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 ALL THESE COMMANDS HAVE BEEN INCLUDED IN THE MAKE 32. BAT – make 32 filename
Defining Data n Directives (see pp. 80 -81) Ø DB, BYTE 8 -bit unsigned integer SBYTE 8 -bit singed integer Ø DW, WORD 16 -bit unsigned integer SWORD 16 -bit signed integer Ø DWORD 32 -bit integer SDWORD 32 -bit signed integer Ø REAL 4 32 -bit IEEE short real Ø REAL 8 64 -bit IEEE long real
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 Ø Cost byte "Joe Brown" word 14
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 segment . data X byte 2, 24 h Y word 18 h, 126 h Z byte ? How is the data filled in the data segment ? 0 1 2 3 4 5 6 02 24 18 00 26 01 ? ? X X+1 Y Y+2 Z
Defining Data n Characters and Strings Ø Characters can be represented by the character or their ASCII value. Ø Example : 5 ways to store 'A': Five. As db‘A', "A", 65, 41 h, 01000001 b Ø String examples Name db "My name is Sally", 0 Dh, 0 Ah Ø Continuation character: Ø “ 0 Dh” and “ 0 Ah” are carriage return and line feed Ø Characters in strings are stored in order, one character per byte
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 (20 bytes)
Outline q Basic Elements of Assembly q Sample Program q Assembling, Linking, and Running q Defining Data q Symbolic Constants
Symbolic Constants n Symbolic Constants Ø Allow naming constants Ø Similar to C+ + code const int num. Elements = 20; const int num. Words = 2 * num. Elements; or in Java public static final int num. Val = 40; Ø They are not assigned storage locations in assembly Ø They can be defined anywhere in the program
Symbolic Constants n Equal-sign Directive Ø Examples num. Values = 25 min = 5 max = min + 100 Ø Similar to C++ code const int num. Values = 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.
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
Symbolic Constants n EQU Directive: Ø Formats: name EQU expressions name EQU symbol name EQU <text> Ø Expression must be a valid integer expression Ø Symbol must be a existing symbol name, already defined with = or EQU Ø Cannot be redefined
Symbolic Constants n TEXTEQU Directive: Ø Formats: name TEXTEQU <text> name TEXTEQU textmacro name TEXTEQU %const. Expr Ø Used to create a text macro Ø Can be redefined
Symbolic Constants n TEXTEQU Directive: Ø Example Cont. Msg TEXTEQU <"Press return"> CR = 0 Dh LF = 0 Ah Ending = '$' Ø Then Msg db Cont. Msg, CR, LF, Ending assembles as Msg db "Press return", 0 Dh, 0 Ah, '$'
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
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 Long. String db “This is a piece of text that I“ db “want to type on 2 separate lines” Long. String_length = ($ - Long. String) Ø Note that we do not need to give a name to every line. . .
Outline q Basic Elements of Assembly q Sample Program q Assembling, Linking, and Running q Defining Data q Symbolic Constants
- Slides: 32