Assembly Language for Intel-Based
Computers, 4th Edition
Kip R. Irvine
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 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. 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
• Examples
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 3
Integer Constants
• Optional leading + or – sign
• binary, decimal, hexadecimal, or octal digits
• Common radix characters:
• h – hexadecimal
• d – decimal
• b – binary
• r – encoded real
Examples: 30d, 6Ah, 42, 1101b
Hexadecimal beginning with letter: 0A5h
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 4
Integer Expressions
• Operators and precedence levels:
• Examples:
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 5
Character and String Constants
• 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
• Embedded quotes:
• 'Say "Goodnight," Gracie'
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 6
Reserved Words and Identifiers
• Reserved words (Appendix D) cannot be used as
identifiers
• Instruction mnemonics, directives, type attributes,
operators, predefined symbols
• Identifiers
• 1-247 characters, including digits
• not case sensitive
• first character must be a letter, _, @, ?, or $
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 7
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.
• not case sensitive
• Different assemblers have different directives
• NASM not the same as MASM, for example
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 8
Instructions
• Assembled into machine code by assembler
• Executed at runtime by the CPU
• We use the Intel IA-32 instruction set
• An instruction contains:
• Label (optional)
• Mnemonic (required)
• Operand (depends on the instruction)
• Comment (optional)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9
Labels
• Act as place markers
• marks the address (offset) of code and data
• Follow identifer rules
• Data label
• must be unique
• example: myArray (not followed by colon)
• Code label
• target of jump and loop instructions
• example: L1: (followed by colon)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10
Mnemonics and Operands
• Instruction Mnemonics
• memory aid
• examples: MOV, ADD, SUB, MUL, INC, DEC
• Operands
• constant
• constant expression
• register
• memory (data label)
Constants and constant expressions are often called
immediate values
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 11
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 comments
• begin with COMMENT directive and a programmer-
chosen character
• end with the same programmer-chosen character
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 12
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
• 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. 15
Example: Adding and Subtracting Integers
TITLE Add and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit integers.
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs ; display registers
exit
main ENDP
END main
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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17
Suggested Coding Standards (1 of 2)
• Some approaches to capitalization
• capitalize nothing
• capitalize everything
• 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. 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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19
Alternative Version of AddSub
TITLE Add and Subtract (AddSubAlt.asm)
; This program adds and subtracts 32-bit integers.
.386
.MODEL flat,stdcall
.STACK 4096
ExitProcess PROTO, dwExitCode:DWORD
DumpRegs PROTO
.code
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. 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
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23