25
Assembly Language for Intel- Assembly Language for Intel- Based Computers, 4 Based Computers, 4 th th Edition 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) Slides prepared by Kip R. Irvine 09/15/2002 and modified by Khaled Alzoubi 1/30/2005 Kip R. Irvine

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

  • View
    219

  • Download
    5

Embed Size (px)

Citation preview

Assembly Language for Intel-Based Assembly Language for Intel-Based Computers, 4Computers, 4thth Edition 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)

Slides prepared by Kip R. Irvine 09/15/2002

and modified by Khaled Alzoubi 1/30/2005

Kip R. Irvine

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 2

Chapter OverviewChapter Overview

• Basic Elements of Assembly Language• Example: Adding and Subtracting Integers• Assembling, Linking, and Running Programs• Defining Data• Symbolic Constants• Real-Address Mode Programming

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 3

Basic Elements of Assembly LanguageBasic Elements of Assembly Language

• Integer constants• Integer expressions• Character and string constants• Reserved words and identifiers• Directives and instructions• Labels• Mnemonics and Operands• Comments

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 4

Integer ConstantsInteger 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, 1101bHexadecimal beginning with letter must have a leading zero to prevent interpreting it as an identifier, ex. 0A5h

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 5

Integer ExpressionsInteger 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.

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 6

Character and String ConstantsCharacter 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”

Character Hexadecimal ASCII code

Decimal ASCII code

Binary ASCII code

‘A’ 41 65 01000001

“d” 64 100 01100100

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 7

Reserved WordsReserved 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. @

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 8

IdentifiersIdentifiers

• 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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 9

DirectivesDirectives

• 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 PROCMore Directives are available in Appendix D

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 10

InstructionsInstructions

• 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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 11

LabelsLabels

• 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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 12

OperandsOperands

• Instructions can have 0 to 3 operands• constant (immediate value)

• constant expression

• register

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 13

CommentsComments• 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

&

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 14

Instruction Format ExamplesInstruction 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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 15

Example: Adding and Subtracting IntegersExample: 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 codemain PROC ; identifies the startup procedure named main

mov eax,10000h ; EAX = 10000hadd eax,40000h ; EAX = 50000hsub eax,20000h ; EAX = 30000hcall DumpRegs ; display registersexit ; calls MS-windows function to halt the program

main ENDP ; End of main procedure END main ; marks the last line to be assembled

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 16

Example OutputExample OutputProgram 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 executedEFLAGS- status flags: reflect the outcomes of arithmetic and logical operations performed by the CPU. Set=1, clear=0Carry flag (CF): is set when the result of unsigned arithmetic operation is out of rangeSign flag (SF): is set when the result is negativeZero flag (ZF): is set when the result is zeroOverflow flag (OF): is set when the result of signed arithmetic operation is out of range

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 17

Suggested Coding StandardsSuggested Coding Standards (1 of 2) (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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 18

Suggested Coding StandardsSuggested Coding Standards (2 of 2) (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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 19

Alternative Version of AddSubAlternative Version of AddSubTITLE Add and Subtract (AddSubAlt.asm)COMMENT !This program adds and subtracts 32-bit integers. The following 5lines 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 4096ExitProcess PROTO, dwExitCode:DWORD ;ExitProcess is an MS-Windows ; function that halts the program (process) DumpRegs PROTO ; Displays registers.codemain PROC

mov eax,10000h ; EAX = 10000hadd eax,40000h ; EAX = 50000hsub eax,20000h ; EAX = 30000hcall DumpRegsINVOKE ExitProcess,0 ; End the program, function returns 0

main ENDPEND main

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 20

Program TemplateProgram Template

TITLE Program Template (Template.asm)

; Program Description:; Author:; Creation Date:; Revisions: ; Date: Modified by:

INCLUDE Irvine32.inc.data

; (insert variables here).codemain PROC

; (insert executable instructions here)exit

main ENDP; (insert additional procedures here)

END main

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 21

Assembling, Linking, and Running ProgramszAssembling, Linking, and Running Programsz

• Assemble-Link-Execute Cycle• make32.bat• Listing File• Map File

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 22

Assemble-Link Execute CycleAssemble-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.

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 23

make32.batmake32.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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 24

Listing FileListing 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.lst

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Web site Examples 25

Map FileMap File

• Information about each program segment:• starting address

• ending address

• size

• segment type

• Example: addSub.map