Download Chapter 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assembly Language for Intel-Based
Computers, 4th Edition
Kip R. Irvine
Chapter 3: Assembly Language
Fundamentals
Slides prepared by Kip R. Irvine 09/15/2002
and modified by Khaled Alzoubi 1/30/2005
• Chapter corrections (Web) Assembly language sources (Web)
(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 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
2
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
3
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
4
Integer Expressions
An integer expression is a mathematical expression
involving integers and operators, and evaluates to an
integer that can be stored in 32 bits.
• Operators and precedence levels:
• Examples:
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
5
Character and String Constants
•
Enclose character in single or double quotes (will be converted by the Assembler)
Character Hexadecimal
ASCII code
•
•
Decimal
ASCII code
Binary ASCII
code
‘A’
41
65
01000001
“d”
64
100
01100100
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”
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
6
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. @
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
7
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
8
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
9
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
10
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:
mov ax, bx
…….
jmp target
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
(followed by colon)
; loop (jump) to target
Web site
Examples
11
Operands
• 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
12
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
13
Web site Examples
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
&
Instruction Format Examples
• No operands
• stc
; set Carry flag
• One operand
• inc eax
• inc myByte
; register
; memory
• Two operands
• add ebx,ecx
• sub myByte,25
• add eax,36 * 25
; register, register
; memory, constant
; register, constant-expression
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
14
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
15
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
16
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
17
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
18
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
19
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
20
Assembling, 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
21
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.
Link
Library
Source
File
Step 1: text editor
Step 2:
assembler
Object
File
Step 3:
linker
Listing
File
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Executable
File
Step 4:
OS loader
Output
Map
File
Web site
Examples
22
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
23
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.lst
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
24
Map File
• Information about each program segment:
•
•
•
•
starting address
ending address
size
segment type
• Example: addSub.map
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Web site
Examples
25