Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2002. All rights reserved. You.

Similar presentations


Presentation on theme: "Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2002. All rights reserved. You."— Presentation transcript:

1 Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed. Chapter corrections (Web)Assembly language sources (Web)Chapter correctionsAssembly language sources Slides prepared by Kip R. Irvine 09/15/2002 and modified by Khaled Alzoubi 1/30/2005 Kip R. Irvine

2 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 2 Chapter Overview Basic Elements of Assembly Language Example: Adding and Subtracting Integers Assembling, Linking, and Running Programs Defining Data Symbolic Constants Real-Address Mode Programming

3 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 3 Basic Elements of Assembly Language Integer constants Integer expressions Character and string constants Reserved words and identifiers Directives and instructions Labels Mnemonics and Operands Comments

4 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 4 Integer Constants Optional leading + or – sign binary, decimal, hexadecimal, or octal digits Common radix characters: h – hexadecimal d or t – decimal b or y – binary q or o – octal r – encoded real [{+|-}] digits [radix] The absence of radix means the integer is decimal Examples: 30d, 6Ah, 42, 1101b Hexadecimal beginning with letter must have a leading zero to prevent interpreting it as an identifier, ex. 0A5h

5 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 5 Integer Expressions Operators and precedence levels: Examples: An integer expression is a mathematical expression involving integers and operators, and evaluates to an integer that can be stored in 32 bits.

6 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 6 Character and String Constants Enclose character in single or double quotes ( will be converted by the Assembler ) Enclose strings (string of characters) in single or double quotes "ABC" 'xyz' Each character occupies a single byte Embedded quotes: 'Say "Goodnight," Gracie‘ “Say ‘Goodnight,’ Gracie” CharacterHexadecimal ASCII code Decimal ASCII code Binary ASCII code ‘A’416501000001 “d”6410001100100

7 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 7 Reserved Words Reserved words (Appendix D) cannot be used as identifiers Instruction mnemonics: identifies the operation carried out by an instruction, ex. MOV, ADD, MUL, SUB, JMP, CALL Directives: tell MASM assembler how to assemble programs, ex..code,.data type attributes, provides size and usage information about variables. ex. BYTE, WORD operators, ex, +, - predefined symbols (characters) ex. @

8 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 8 Identifiers Identifier: a name selected by the Programmer to identify a variable, constant, procedure, or a code label 1-247 characters, including digits case insensitive (by default) first character must be a letter, _, @, ?, or $ Subsequent characters may be digits -Cp command line switch makes all keywords and identifiers case sensitive

9 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9 Directives Commands that are recognized and acted upon by the assembler Not part of the Intel instruction set Used to declare code, data areas, select memory model, declare procedures, etc. case insensitive Different assemblers have different directives NASM != MASM, for example Examples on Directives:.DATA: identifies the area that contains variables.CODE: identifies the area that contains instructions PROC: identifies the beginning of a procedure, ex. ProcedureName PROC More Directives are available in Appendix D

10 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10 Instructions Assembled into object code (machine code) by the assembler Executed at runtime by the CPU Member of the Intel IA-32 instruction set Parts Label (Name) (optional) Mnemonic (required) Operand(s) (usually required) Comment (optional) Standard Format: Label --> Mnemonic --> Operands --> ;Comment

11 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 11 Labels Act as a place marker for instructions or data marks the address of code and data placed before a variable or instruction to imply its address Follow identifier rules Data label must be unique example: myArray (not followed by colon) Code label target of jump and loop instructions example: target: (followed by colon) mov ax, bx ……. jmp target ; loop (jump) to target

12 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 12 Operands Instructions can have 0 to 3 operands constant (immediate value) constant expression register

13 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 13 Comments Comments are good! explain the program's purpose when it was written, and by whom revision information tricky coding techniques application-specific explanations Single-line comments begin with semicolon (;) Multi-line (block) comments begin with COMMENT directive and a programmer-chosen character end with the same programmer-chosen character Example: COMMENT & This is an example of a comment on block comments &

14 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 14 Instruction Format Examples No operands stc; set Carry flag One operand inc eax; register inc myByte; memory Two operands add ebx,ecx; register, register sub myByte,25; memory, constant add eax,36 * 25; register, constant-expression

15 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 15 Example: Adding and Subtracting Integers TITLE Add and Subtract (AddSub.asm) ; The TITLE directive marks the entire line as a comment ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc ;copies the contents of file Irvine32.inc.code ; marks the beginning of the code main PROC ; identifies the startup procedure named main mov eax,10000h; EAX = 10000h add eax,40000h; EAX = 50000h sub eax,20000h; EAX = 30000h call DumpRegs; display registers exit ; calls MS-windows function to halt the program main ENDP ; End of main procedure END main ; marks the last line to be assembled

16 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16 Example Output Program output, showing registers and flags: EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 EIP – instruction pointer register, contains the address of the next instruction to be executed EFLAGS- status flags: reflect the outcomes of arithmetic and logical operations performed by the CPU. Set=1, clear=0 Carry flag (CF): is set when the result of unsigned arithmetic operation is out of range Sign flag (SF): is set when the result is negative Zero flag (ZF): is set when the result is zero Overflow flag (OF): is set when the result of signed arithmetic operation is out of range

17 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17 Suggested Coding Standards (1 of 2) Some approaches to capitalization, remember Assembly is case-insensitive capitalize the initial letters of identifiers capitalize nothing capitalize all reserved words, including instruction mnemonics and register names capitalize only directives and operators Other suggestions descriptive identifier names spaces surrounding arithmetic operators blank lines between procedures

18 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18 Suggested Coding Standards (2 of 2) Indentation and spacing code and data labels – no indentation executable instructions – indent 4-5 spaces comments: begin at column 40-45, aligned vertically 1-3 spaces between instruction and its operands ex: mov ax,bx 1-2 blank lines between procedures

19 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19 Alternative Version of AddSub TITLE Add and Subtract (AddSubAlt.asm) COMMENT ! This program adds and subtracts 32-bit integers. The following 5 lines are some contents of the Irvine32.inc file !.386 ; minimum CPU required.MODEL flat,stdcall ; generate a code for protected mode program ; STDCALL enables the calling of MS-Windows functions.STACK 4096 ; stack size is 4096 ExitProcess PROTO, dwExitCode:DWORD ;ExitProcess is an MS-Windows ; function that halts the program (process) DumpRegs PROTO ; Displays registers.code main PROC mov eax,10000h; EAX = 10000h add eax,40000h; EAX = 50000h sub eax,20000h; EAX = 30000h call DumpRegs INVOKE ExitProcess,0 ; End the program, function returns 0 main ENDP END main

20 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 20 Program Template TITLE Program Template (Template.asm) ; Program Description: ; Author: ; Creation Date: ; Revisions: ; Date: Modified by: INCLUDE Irvine32.inc.data ; (insert variables here).code main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedures here) END main

21 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21 Assembling, Linking, and Running Programsz Assemble-Link-Execute Cycle make32.bat Listing File Map File

22 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22 Assemble-Link Execute Cycle The following diagram describes the steps from creating a source program through executing the compiled program. If the source code is modified, Steps 2 through 4 must be repeated.

23 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23 make32.bat Called a batch file Run it to assemble and link programs Contains a command that executes ML.EXE (the Microsoft Assembler) Contains a command that executes LINK32.EXE (the 32-bit Microsoft Linker) Command-Line syntax: make32 progName (progName does not include the.asm extension) Use make16.bat to assemble and link Real-mode programs

24 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 24 Listing File Use it to see how your program is compiled Contains source code addresses object code (machine language) segment names symbols (variables, procedures, and constants) Example: addSub.lstaddSub.lst

25 Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25 Map File Information about each program segment: starting address ending address size segment type Example: addSub.mapaddSub.map


Download ppt "Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, 2002. All rights reserved. You."

Similar presentations


Ads by Google