Chapt 03
Chapt 03
Lecture 1
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
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
main PROC
mov eax, 5 ; move 5 to the EAX register
add eax, 6 ; add 6 to the EAX register
call WriteInt ; display value in EAX
exit ; quit
main ENDP
• Examples:
Precedence Examples:
4+5*2 Multiply, add
12 – 1 MOD 5 Modulus, subtract
-5 + 2 Unary minus, add
(4 + 2) * 6 Add, multiply
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
6
Real Number Constants
• Represented as decimal reals or encoded
(hexadecimal) reals
• Decimal real contains optional sign followed by
integer, decimal point, and optional integer that
expresses a fractional and an optional exponent
• [sign] integer.[integer] [exponent]
• Sign {+, -}
• Exponent E[{+, -}] integer
• Examples
• 2.
• +3.0
• -44.2E+05
• 26.E5
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
7
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'
STC instruction
stc ; set Carry flag
INC instruction
inc eax ; add 1 to EAX
MOV instruction
mov count, ebx ; move EBX to count
; first operation is destination
; second is the source
IMUL instruction (three operands)
imul eax, ebx, 5 ; ebx multiplied by 5, product in EAX
• 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
Lecture 2
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
20
Example: Adding and Subtracting 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
;******************************************************************
;* RoutineName - expanded name or phrase describing purpose
;* Brief description, typically a few lines explaining the
;* purpose of the program.
;*
;* I/O: Explain what is expected and what is produced
;*
;* Calling Convention: How is the routine called?
;*
;* Stack Usage: (when needed) When a routine has several variables
;* on the stack, this section describes the structure of the
;* information.
;*
;* Information about routines that are called and any registers
;* that get destroyed. In general, if some registers are pushed at
;* the beginning and pulled at the end, it is not necessary to
;* describe this in the routine header.
;******************************************************************
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
27
Alternative Version of AddSub
TITLE Add and Subtract (AddSubAlt.asm)
.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 x86 Processors 6/e, 2010.
28
Program Template
TITLE Program Template (Template.asm)
;***************************************************
; Program Name:
; Program Description:
; Author:
; Version:
; Date:
; Other Information:
;***************************************************
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 x86 Processors 6/e, 2010.
29
What's Next
• Assemble-Link-Execute Cycle
• Listing File
• Map File
Lecture 3
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
36
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
• BYTE, SBYTE
• 8-bit unsigned integer; 8-bit signed integer
• WORD, SWORD
• 16-bit unsigned & signed integer
• DWORD, SDWORD
• 32-bit unsigned & signed integer
• QWORD
• 64-bit integer
• TBYTE
• 80-bit integer
• REAL4
• 4-byte IEEE short real
• REAL8
• 8-byte IEEE long real
• REAL10
• 10-byte IEEE extended real
value1 BYTE 10
• Example:
val1 DWORD 12345678h
Lecture 4
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
53
Symbolic Constants
• Equal-Sign Directive
• Calculating the Sizes of Arrays and Strings
• EQU Directive
• TEXTEQU Directive
COUNT = 500
.
.
mov ax,COUNT
PI EQU <3.1416>
pressKey EQU <"Press any key to continue...",0>
.data
prompt BYTE pressKey
.code
setupAL ; generates: "mov al,10"
Lecture 5
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
61
Real-Address Mode Programming (1 of 2)
• Requirements
• INCLUDE Irvine16.inc
• Initialize DS to the data segment:
mov ax,@data
mov ds,ax