03-IntroAssembly
03-IntroAssembly
IA-32 Architecture
– slide 4
General-Purpose Registers
Used primarily for arithmetic and data movement
mov eax, 10 move constant 10 into register
eax
Specialized uses of Registers
EAX – Accumulator register
Automatically used by multiplication and division instructions
ECX – Counter register
Automatically used by LOOP instructions
ESP – Stack Pointer register
Used by PUSH and POP instructions, points to top of stack
ESI and EDI – Source Index and Destination Index register
Used by string instructions
EBP – Base Pointer register
Used to reference parameters and local variables on the stack
IA-32 Architecture
– slide 5
Accessing Parts of Registers
EAX, EBX, ECX, and EDX are 32-bit Extended registers
Programmers can access their 16-bit and 8-bit parts
Lower 16-bit of EAX is named AX
AX is further divided into
AL = lower 8 bits
AH = upper 8 bits
ESI, EDI, EBP, ESP have only
16-bit names for lower half
IA-32 Architecture
– slide 6
Special-Purpose & Segment
Registers
EIP = Extended Instruction Pointer
Contains address of next instruction to be executed
EFLAGS = Extended Flags Register
Contains status and control flags
Each flag is a single binary bit
Six 16-bit Segment Registers
Support segmented memory
Six segments accessible at a time
Segments contain distinct contents
Code
Data
Stack
IA-32 Architecture
– slide 7
EFLAGS Register
Status Flags
Status of arithmetic and logical operations
Control and System flags
Control the CPU operation
Programs can set and clear individual bits in the EFLAGS register
IA-32 Architecture
– slide 8
Status Flags
Carry Flag
Set when unsigned arithmetic result is out of range
Overflow Flag
Set when signed arithmetic result is out of range
Sign Flag
Copy of sign bit, set when result is negative
Zero Flag
Set when result is zero
Auxiliary Carry Flag
Set when there is a carry from bit 3 to bit 4
Parity Flag
Set when parity is even
Least-significant byte in result contains even number of 1s
IA-32 Architecture
– slide 9
Floating-Point, MMX, XMM
Registers
Floating-point unit performs high speed FP operations
Eight 80-bit floating-point data registers
ST(0), ST(1), . . . , ST(7)
Arranged as a stack
Used for floating-point arithmetic
IA-32 Architecture
– slide 10
Assembly Language Statements
Three types of statements in assembly language
Typically, one statement should appear on a line
1. Executable Instructions
Generate machine code for the processor to execute at runtime
Instructions tell the processor what to do
2. Assembler Directives
Provide information to the assembler while translating a program
Used to define data, select memory model, etc.
Non-executable: directives are not part of instruction set
3. Macros
Shorthand notation for a group of statements
Sequence of instructions, directives, or other macros
Introduction to Assembly Language
slide 11
Instructions
Assembly language instructions have the format:
[label:] mnemonic [operands] [;comment]
Instruction Label (optional)
Marks the address of an instruction, must have a colon :
Used to transfer program execution to a labeled instruction
Mnemonic
Identifies the operation (e.g. MOV, ADD, SUB, JMP, CALL)
Operands
Specify the data required by the operation
Executable instructions can have zero to three operands
Operands can be registers, memory variables, or constants
Introduction to Assembly Language
slide 12
Instruction Examples
No operands
stc ; set carry flag
One operand
inc eax ; increment register eax
call Clrscr ; call procedure Clrscr
jmp L1 ; jump to instruction with label L1
Two operands
add ebx, ecx ; register ebx = ebx + ecx
sub var1, 25 ; memory variable var1 = var1 - 25
Three operands
imul eax,ebx,5 ; register eax = ebx * 5
Introduction to Assembly Language
slide 13
Identifiers
Identifier is a programmer chosen name
Identifies variable, constant, procedure, code label
May contain between 1 and 247 characters
Not case sensitive
First character must be a letter (A..Z, a..z),
underscore(_), @, ?, or $.
Subsequent characters may also be digits.
Cannot be same as assembler reserved word.
Single-line comments
Begin with a semicolon ; and terminate at end of line
Multi-line comments
Begin with COMMENT directive and a chosen character
End with the same chosen character
Introduction to Assembly Language
slide 15
Next . . .
; Program Description:
; Author: Creation Date:
; Modified by: Modification Date:
.686
.MODEL FLAT, STDCALL
.STACK
INCLUDE Irvine32.inc
.DATA
; (insert variables here)
.CODE
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
Introduction to Assembly Language
slide 17
TITLE and .MODEL Directives
TITLE line (optional)
Contains a brief heading of the program and the disk file name
.MODEL directive
Specifies the memory configuration
For our purposes, the FLAT memory model will be used
Linear 32-bit address space (no segmentation)
STDCALL directive tells the assembler to use …
Standard conventions for names and procedure calls
.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
Introduction to Assembly Language
slide 22
Example of Console Output
.686
.MODEL flat,stdcall
.STACK 4096
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
push 0
call ExitProcess ; to terminate program
main ENDP
END main
Introduction to Assembly Language
slide 26
Next . . .
Debugger: WINDBG.exe
Assemble
Trace program execution
Either step-by-step, or library.lib prog.obj prog.lst
Use breakpoints
View Link
val1 BYTE 10
ROWSIZE = 5
COUNT TEXTEQU %(ROWSIZE * 2) ; evaluates to 10
MOVAL TEXTEQU <mov al,COUNT>
ContMsg TEXTEQU <"Do you wish to continue (Y/N)?">
.DATA
prompt BYTE ContMsg
.CODE
MOVAL ; generates: mov al,10
Introduction to Assembly Language
slide 48
Next . . .
.DATA
bVal BYTE ? ; Assume bVal is at 00404000h
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
.CODE
mov esi, OFFSET bVal ; ESI = 00404000h
mov esi, OFFSET wVal ; ESI = 00404001h
mov esi, OFFSET dVal ; ESI = 00404003h
mov esi, OFFSET dVal2 ; ESI = 00404007h
.DATA
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?
.CODE
mov eax, TYPE var1 ; eax = 1
mov eax, TYPE var2 ; eax = 2
mov eax, TYPE var3 ; eax = 4
mov eax, TYPE var4 ; eax = 8
.DATA
array1 WORD 30 DUP(?),0,0
array2 WORD 5 DUP(3 DUP(?))
array3 DWORD 1,2,3,4
digitStr BYTE "12345678",0
.code
mov ecx, LENGTHOF array1 ; ecx = 32
mov ecx, LENGTHOF array2 ; ecx = 15
mov ecx, LENGTHOF array3 ; ecx = 4
mov ecx, LENGTHOF digitStr ; ecx = 9
.DATA
array1 WORD 30 DUP(?),0,0
array2 WORD 5 DUP(3 DUP(?))
array3 DWORD 1,2,3,4
digitStr BYTE "12345678",0
.CODE
mov ecx, SIZEOF array1 ; ecx = 64
mov ecx, SIZEOF array2 ; ecx = 30
mov ecx, SIZEOF array3 ; ecx = 16
mov ecx, SIZEOF digitStr ; ecx = 9
.DATA .DATA
array WORD 10,20, array WORD 10,20
30,40, WORD 30,40
50,60 WORD 50,60
.CODE .CODE
mov eax, LENGTHOF array ; 6 mov eax, LENGTHOF array ; 2
mov ebx, SIZEOF array ; 12 mov ebx, SIZEOF array ; 4
.CODE
mov al, dval ; error – why?
mov al, BYTE PTR dval ; al = 78h
mov ax, dval ; error – why?
mov ax, WORD PTR dval ; ax = 5678h
mov eax, array ; error – why?
mov eax, DWORD PTR array ; eax = 30201000h
Introduction to Assembly Language
slide 56
LABEL Directive
Assigns an alternate name and type to a memory location
LABEL does not allocate any storage of its own
Removes the need for the PTR operator
Format: Name LABEL Type
.DATA blist
dval LABEL DWORD
00 10 00 20
wval LABEL WORD
wval
blist BYTE 00h,10h,00h,20h
.CODE dval
mov eax, dval ; eax = 20001000h
mov cx, wval ; cx = 1000h
mov dl, blist ; dl = 00h