Sie sind auf Seite 1von 6

Chapter Overview

Assembly Language for Intel-Based


Computers, 4th Edition
Kip R. Irvine • Basic Elements of Assembly Language
• Example: Adding and Subtracting Integers
Chapter 3: Assembly Language • Assembling, Linking, and Running Programs
• Defining Data
Fundamentals
• Symbolic Constants
• Real-Address Mode Programming

(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. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 2

Basic Elements of Assembly Language Integer Constants

• Integer constants • Optional leading + or – sign


• Integer expressions • binary, decimal, hexadecimal, or octal digits
• Character and string constants • Common radix characters:
• h – hexadecimal
• Reserved words and identifiers
• d – decimal
• Directives and instructions
• b – binary
• Labels • r – encoded real
• Mnemonics and Operands
• Comments Examples: 30d, 6Ah, 42, 1101b
• Examples Hexadecimal beginning with letter: 0A5h

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 3 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 4

Integer Expressions Character and String Constants


• Operators and precedence levels: • Enclose character in single or double quotes
• 'A', "x"
• ASCII character = 1 byte
• Enclose strings in single or double quotes
• "ABC"
• 'xyz'
• Each character occupies a single byte
• Examples:
• Embedded quotes:
• 'Say "Goodnight," Gracie'

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 5 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 6

1
Reserved Words and Identifiers Directives

• Reserved words (Appendix D) cannot be used as • Commands that are recognized and acted
identifiers upon by the assembler
• Instruction mnemonics, directives, type attributes, • Not part of the Intel instruction set
operators, predefined symbols • Used to declare code, data areas, select
• Identifiers memory model, declare procedures, etc.
• 1-247 characters, including digits • not case sensitive
• not case sensitive • Different assemblers have different directives
• first character must be a letter, _, @, ?, or $ • NASM not the same as MASM, for example

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 7 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 8

Instructions Labels

• Assembled into machine code by assembler • Act as place markers


• Executed at runtime by the CPU • marks the address (offset) of code and data
• We use the Intel IA-32 instruction set • Follow identifer rules
• An instruction contains: • Data label
• Label (optional) • must be unique
• Mnemonic (required) • example: myArray (not followed by colon)
• Operand (depends on the instruction) • Code label
• Comment (optional) • target of jump and loop instructions
• example: L1: (followed by colon)

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10

Mnemonics and Operands Comments


• Comments are good!
• explain the program's purpose
• Instruction Mnemonics • when it was written, and by whom
• memory aid • revision information
• examples: MOV, ADD, SUB, MUL, INC, DEC • tricky coding techniques
• Operands • application-specific explanations
• constant • Single-line comments
• constant expression • begin with semicolon (;)
• register
• Multi-line comments
• memory (data label)
• begin with COMMENT directive and a programmer-
chosen character
Constants and constant expressions are often called
• end with the same programmer-chosen character
immediate values

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 11 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 12

2
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 13 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 14

Instruction Format Examples Example: Adding and Subtracting Integers

• No operands TITLE Add and Subtract (AddSub.asm)

• stc ; set Carry flag ; This program adds and subtracts 32-bit integers.
• One operand
INCLUDE Irvine32.inc
• inc eax ; register .code
• inc myByte ; memory main PROC
• Two operands mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
• add ebx,ecx ; register, register sub eax,20000h ; EAX = 30000h
• sub myByte,25 ; memory, constant call DumpRegs ; display registers
• add eax,36 * 25 ; register, constant-expression exit
main ENDP
END main

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 15 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16

Example Output Suggested Coding Standards (1 of 2)

• Some approaches to capitalization


Program output, showing registers and flags: • capitalize nothing
• capitalize everything
EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF • capitalize all reserved words, including instruction
ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 mnemonics and register names
EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0
• 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. 17 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18

3
Suggested Coding Standards (2 of 2) Alternative Version of AddSub
TITLE Add and Subtract (AddSubAlt.asm)

• Indentation and spacing ; This program adds and subtracts 32-bit integers.
.386
• code and data labels – no indentation .MODEL flat,stdcall
• executable instructions – indent 4-5 spaces .STACK 4096
• comments: begin at column 40-45, aligned vertically ExitProcess PROTO, dwExitCode:DWORD
• 1-3 spaces between instruction and its operands DumpRegs PROTO
• ex: mov ax,bx .code
• 1-2 blank lines between procedures main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs
INVOKE ExitProcess,0
main ENDP
END main

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19 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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22

Assembling, Linking, and Running Programs

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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 24

4
Assemble-Link Execute Cycle make32.bat
• The following diagram describes the steps from creating a • Called a batch file
source program through executing the compiled program.
• Run it to assemble and link programs
• If the source code is modified, Steps 2 through 4 must be
repeated. • Contains a command that executes ML.EXE (the
Microsoft Assembler)
Link • Contains a command that executes LINK32.EXE (the
Library
Step 2: Step 3: Step 4: 32-bit Microsoft Linker)
Source Object Executable
• Command-Line syntax:
assembler linker OS loader
Output
File File File
make32 progName
Listing Map
File File (progName does not include the .asm extension)
Step 1: text editor

Use make16.bat to assemble and link Real-mode programs

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26

Make32.bat Listing File


@echo off
REM make32.bat - Batch file for assembling/linking 32-bit Assembly programs
• Use it to see how your program is compiled
REM ************ MASMDIR should be defined *************
SET INCLUDE=%MASMDIR%\INCLUDE • Contains
SET LIB=%MASMDIR%\LIB
• source code
ML -Zi -c -Fl -Sg -coff %1.asm • addresses
if errorlevel 1 goto terminate
• object code (machine language)
LINK32 %1.obj Irvine32.lib user32.lib kernel32.lib /SUBSYSTEM:CONSOLE /DEBUG /MAP
• segment names
:terminate • symbols (variables, procedures, and constants)
• Example: addSub.lst

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 28

Map File

• Information about each program segment:


• starting address
• ending address
• size
• segment type
• Example: addSub.map (16-bit version)

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 29 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 30

5
Defining Data
• Intrinsic Data Types
• Data Definition Statement
• Defining BYTE and SBYTE Data
• Defining WORD and SWORD Data
• Defining DWORD and SDWORD Data
• Defining QWORD Data
• Defining TBYTE Data
• Defining Real Number Data
• Little Endian Order
• Adding Variables to the AddSub Program
• Declaring Uninitialized Data

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 31 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 32

Intrinsic Data Types (1 of 2) Intrinsic Data Types (2 of 2)

• BYTE, SBYTE
• 8-bit unsigned integer; 8-bit signed integer • REAL4
• WORD, SWORD • 4-byte IEEE short real
• 16-bit unsigned & signed integer • REAL8
• DWORD, SDWORD • 8-byte IEEE long real
• 32-bit unsigned & signed integer • REAL10
• QWORD • 10-byte IEEE extended real
• 64-bit integer
• TBYTE
• 80-bit integer

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 33 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 34

Data Definition Statement Defining BYTE and SBYTE Data


Each of the following defines a single byte of storage:
• A data definition statement sets aside storage in memory for a
variable. value1 BYTE 'A' ; character constant
• May optionally assign a name (label) to the data value2 BYTE 0 ; smallest unsigned byte
• Syntax: value3 BYTE 255 ; largest unsigned byte
[name] directive initializer [,initializer] . . .
value4 SBYTE -128 ; smallest signed byte
value5 SBYTE +127 ; largest signed byte
value6 BYTE ? ; uninitialized byte
value1 BYTE 10

• All initializers become binary data in memory A variable name is a data label that implies an offset (an address).

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 35 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 36

Das könnte Ihnen auch gefallen