Lecture #7

advertisement
Quiz #2 Topics


Character codes
Intel IA-32 architecture





Registers, memory addressing
Bytes, words, etc.
Little-endian representation
Floating-point unit
Mostly MASM










General form of a MASM program
Directives (TITLE, INCLUDE, etc.)
Segments (.code, .data, etc.)
Declare variables, constants
Comments
Instruction format
Instructions (mov, add, call, etc.)
Trace MASM code
Convert simple statements to MASM
Assembling, linking, loading, etc.
Today’s topics

More MASM programming
Addressing modes
 Conditional control structures
 Decision
 Repetition

IA-32 Operand Modes

Immediate



Examples:
Direct


Examples: PI
equ
size
DWORD
myName BYTE
mov
mov
Register

constant or literal, OFFSET (memory address)
Examples:
3.14159
10
”Barney”
eax, 10
edx, OFFSET myName
register contents
mov
add
mov
eax, 10
eax, ebx
size, eax
memory contents
mov
mov
eax, size
size, eax
Others (later)

Register indirect, Indexed, Base-indexed, Stack
Instruction Operand Notation
Syntax
Examples
MOV mem,accum
mov
MOV accum,mem
mov
total,eax
al,string
Notes:
accum means “eax or
some valid part of eax”
imm means “a literal or
Syntax
Examples
MOV mem,imm
mov
Syntax
Examples
MOV reg,imm
mov
mov
constant”
color,7
ecx,256
edx,OFFSET string
Syntax
Examples
MOV reg,reg
mov
mov
mov
dh,bh
edx,ecx
ebp,esp
mov
count,ecx
MOV mem,reg
Notes:
mem8 means “BYTE”
mem16 means “WORD”
mem32 means “DWORD”
sreg means CS, DS, ES, FS,
MOV reg,mem
mov
ebx,pointer
Syntax
Examples
MOV sreg,reg16
mov
ds, ax
MOV sreg,mem16
mov
es,pos1
MOV reg16,sreg
mov
ax,ds
MOV mem16,sreg
mov
stack_save,ss
GS, or SS
Invalid MOV statements
.data
bVal BYTE
100
bVal2 BYTE
?
wVal WORD
2
dVal DWORD 5
.code
mov ds,45
mov esi,wVal
mov eip,dVal
mov 25,bVal
mov bVal2,bVal
immediate move to DS not permitted
size mismatch
EIP cannot be the destination
immediate value cannot be destination
memory-to-memory move not permitted
Branching execution



Sometimes it is necessary to interrupt
sequential instruction execution
EIP is changed
Examples:





Skip ahead (e.g., skip the else block)
Jump backwards (e.g., repeat a section of
code)
Call a procedure
Conditional / Unconditional branching
Label required
MASM Labels

Same rules as other identifiers
May not be any previously defined
identifier

Label definition ends with



Don’t use : when referencing the label
Specifies the memory address of the
associated instruction


:
… just like a variable name
Good practice to put label: on a separate
line.
Unconditional branching

Instruction format is


jmp label
label: should be inside the same procedure
MASM allows jumps to labels in other
procedures, but execution will certainly get
lost in space.
Conditional branching

Used for:


if structures (decisions, alternation)
loop structures (repetition, iteration)

In general, MASM requires you to build your own
control structures

Note: in the following discussion, status bits
(flags) are


Set (means status bit is set to 1)
Cleared (means status bit is set to 0)
loop instruction

Instruction format is





label: should be inside the same procedure, before
the loop instruction
Used for counted loops. Implements a “for” loop.
Conditional branch


loop label
Decrements ecx, if ecx is not zero, branch to label
Problem if ecx is changed inside the loop body
Problem if ecx starts at 0, or ecx becomes
negative
Exercise great care when constructing nested
“loop” loops
loop Example
Find sum of integers from 1 to 10
; initialize accumulator, first number,
; and loop control
mov
eax, 0
mov
ebx, 1
mov
ecx, 10
sumLoop:
; add numbers from 1 to 10
add
eax, ebx
inc
ebx
loop sumLoop
; Print result
call WriteDec
; ...
Conditional branching


We need a way to control branching by
checking some other types of conditions
Examples:



Some repetitive tasks can not be counted in
advance
IF-THEN-ELSE structures
MASM provides a way to compare two
operands
CMP Instruction

Compares the destination operand to the
source operand


Non-destructive subtraction of source from
destination (destination operand is not
changed)
Syntax: CMP destination, source
CMP Instruction (unsigned)

Example: destination is equal to source
mov al,5
cmp al,5
; Zero flag set
CMP Instruction (unsigned)

Example: destination < source
mov al,4
cmp al,5
; Carry flag set
CMP Instruction (unsigned)

Example: destination > source
mov al,6
cmp al,5
; ZF = 0, CF = 0
(both the Zero and Carry flags are cleared)
CMP Instruction (signed)
The comparisons shown here are performed
with signed integers.

Example: destination > source
mov al,5
cmp al,-2

; Sign flag == Overflow flag
Example: destination < source
mov al,-1
cmp al,5
; Sign flag != Overflow flag
Conditional Jumps

Jumps Based On . . .
Specific flags
 Equality
 Unsigned comparisons
 Signed Comparisons

Jcond Instruction



A conditional jump instruction branches to
a label when specific register or flag
conditions are met
Usually the next instruction after cmp
Examples:





JB, JC jump to a label if the Carry flag is set
JE, JZ jump to a label if the Zero flag is set
JS jumps to a label if the Sign flag is set
JNE, JNZ jump to a label if the Zero flag is
cleared
JECXZ jumps to a label if ECX equals 0
Jumps Based on Specific Flags
Jumps Based on Equality
Jumps Based on Unsigned
Comparisons
Jumps Based on Signed
Comparisons
Compare and Jump
• Task: Jump to a label if unsigned EAX is
greater than EBX
• Solution: Use CMP, followed by JA
cmp eax,ebx
ja Larger
• Task: Jump to a label if signed EAX is
greater than EBX
• Solution: Use CMP, followed by JG
cmp eax,ebx
jg Greater
Compare and Jump
• Jump to label L1 if unsigned EAX is less
than or equal to Val1
cmp eax,Val1
jbe L1
; below or equal
• Jump to label L1 if signed EAX is less
than or equal to Val1
cmp eax,Val1
jle L1
Conditional Loop Instructions
(Use after CMP)

LOOPZ and LOOPE


Syntax:
LOOPE destination
LOOPZ destination
Logic:
ECX  ECX – 1
 if ECX > 0 and ZF=1, jump to destination

Conditional Loop Instructions
(Use after CMP)
LOOPNZ and LOOPNE

Syntax:
LOOPNZ destination
LOOPNE destination

Logic:
ECX  ECX – 1;
 if ECX > 0 and ZF=0, jump to destination

Conditional Directives





.IF, .ELSE, .ELSEIF, and .ENDIF
.WHILE, .ENDW
.REPEAT, .UNTIL
Not required for this course
It’s OK to use these in programming
assignments, but you must know the
“hard way” for exams and quizzes
Block-Structured IF Statements


You can create assembly language control
structures that are equivalent to
statements written in C++/Java/etc..
Example:
if( op1 == op2 )
X = 1;
else
X = 2;
mov
cmp
jne
mov
jmp
L1:
L2:
eax,op1
eax,op2
L1
X,1
L2
mov X,2
Assembly Language Control
Structures

Extend the idea to create your own








if-then
if-then-else
if-then-elseif-else
compound conditions
while loop
do-while loop
for loop
nested structures, switch structures, etc.
if-then




check condition using CMP
if condition is false, jump to endThen
(Note: test for complement of condition)
code for TRUE block
endThen:
if-then-else (Method 1)







check condition using CMP
if condition is false, jump to falseBlock
(Note: test for complement of condition)
code for TRUE block
jump to endFalse
falseBlock:
code for FALSE block
endFalse:
if-then-else (Method 2)







check condition using CMP
if condition is true, jump to trueBlock
code for FALSE block
jump to endTrue
trueBlock:
code for TRUE block
endTrue:
if-then-elseif-else












check condition1 using CMP
if condition1 is true, jump to trueBlock1
check condition2 using CMP
if condition2 is true, jump to trueBlock2
code for FALSE block
jump to endBlock
trueBlock1:
code for TRUE block1
jump to endBlock
trueBlock2:
code for TRUE block2
endBlock:
Compound conditions (AND)









check condition1 using CMP
if condition1 is false, jump to falseBlock
check condition2 using CMP
if condition2 is false, jump to falseBlock
code for TRUE block
jump to endBlock
falseBlock
code for FALSE block
endBlock:
Compound conditions (OR)









check condition1 using CMP
if condition1 is true, jump to trueBlock
check condition2 using CMP
if condition2 is true, jump to trueBlock
code for FALSE block
jump to endBlock
trueBlock
code for TRUE block
endBlock:
Pretest loop (while)







initialize loop control variable(s)
top:
check condition using CMP
if condition is false, jump to endWhile
code for LOOP BODY
(including loop control update)
jump to top
endWhile:
Example pre-test loop
Double x until x>1000
; initialize accumulator
mov
eax, x
dblLoop:
; Double x while x <= 1000
cmp
eax, 1000
ja
endLoop
add
eax, eax
jmp
dblLoop
endLoop:
mov
x, eax
; ...
Posttest loop (do-while)




top:
code for LOOP BODY
(including loop control update)
check condition using CMP
if condition is true, jump to top
Example post-test loop
Double x until x>1000
; initialize accumulator
mov
eax, x
dblLoop:
; Double x while x <= 1000
add
eax, eax
cmp
eax, 1000
jbe
dblLoop
mov
x, eax
; ...
Note: may want initial test for x>1000
Various solutions


Any control structure may be implemented
in a variety of ways.
Experiment!
Questions?
Quiz #2 Thursday
Program #2 is assigned
Learn the MASM instructions!
Experiment! Experiment!! Experiment!!!
Download