Professional Documents
Culture Documents
(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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 3 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 4
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10
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
• 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
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
• 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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 28
Map File
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
• 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
• 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