Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Chapter 3 Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Revised 3/11/05."— Presentation transcript:

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

2 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 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 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 / 40 + 5 – 17/3 (17/3 = 5)

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

6 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) 12345 is a number (2 or 4 bytes)

7 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 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 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 + 10 - y; add EAX, 10 sub EAX, y Store: mov z, EAX inc counter; counter++;

10 10 3.1 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

11 11 3.1 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

12 12 3.1 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

13 13 3.1 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

14 14 3.3 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

15 15 3.3 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

16 16 3.3 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.

17 17 3.3 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 –http://msdn.microsoft.com/library/default.asp?url=/library/en- us/vcmasm/html/vcoriMicrosoftAssemblerMacroLanguage.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en- us/vcmasm/html/vcoriMicrosoftAssemblerMacroLanguage.asp

18 18 3.3 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

19 19 3.4 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)

20 20 3.4 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

21 21 3.4 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 12345678h Little endian Big endian W001078 W 001012 0011 56 0011 34 001234 001256 0013 12 0013 78 u Intel uses little endian Memory

22 22 3.4 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+2 5 6 Z 02 24 18 00 26 01 Data segment ??

23 23 3.4 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,01000001b u Characters in strings are stored in order, one character per byte

24 24 3.4 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 1 2 3 2 3 1 2 3 2 3 (20 bytes)

25 25 3.4 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?

26 26 3.4 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.

27 27 3.5 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

28 28 3.5 Symbolic constants 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; u IMPORTANT: The values of constants are substituted for the constant name during assembly time. They a NOT evaluated during execution time.

29 29 3.5 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).

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

31 31 3.5 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 32767 NumValues EQU MaxWord/100 CR EQU 0Dh True EQU 1 ; define boolean values False EQU 0 No EQU 'NO'

32 32 3.5 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

33 33 3.5 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, '$'

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

35 35 3.5 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 36


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

Similar presentations


Ads by Google