04/10/2010ELEG3230B Tutorial 32Outline
„
Recap: Flag Registers
„
Programming Model
„
Data Addressing Mode
„
Assembly Language & Instruction Set (I) ‰
REP, REPZ, REPNZ
‰
CMPS, SCAS
‰
LOOPING
„
Summary
04/10/2010ELEG3230B Tutorial 33Recap –Flag registers
„
6 condition flags in flag register ‰
CF (carry flag): set to 1 when the result of an addition
has a carry out of MSB.
‰
PF (parity flag): set to 1 if the result has an even
number of 1s
‰
AF (auxiliary carry flag): set to 1 if there is a carry/
borrow of bit-3
‰
ZF (zero flag): set to 1 if the result is zero
‰
SF (sign flag): equal to MSB of result
‰
OF (overflow flag): set to 1 if the result is out of range
04/10/2010ELEG3230B Tutorial 34Recap –Flag registers
„
What are the result values in Carry, Zero, Sign,
Parity, Auxiliary Carry, and Overflow flags? ‰
0110 1001 + 0001 1001
‰
0111 1111 + 1000 0001
‰
0101 1011 + 1101 0010
‰
45E9h + 546Ah
04/10/2010ELEG3230B Tutorial 35Programming Model
04/10/2010ELEG3230B Tutorial 36Programming Model –Registers
„
General Registers ‰
General use (like variables)
‰
Special purpose
‰
AX: stores the result of arithmetic and logic
instructions
‰
BX: stores the base (offset) address in XLAT
instruction
‰
CX: stores the loop count for instruction
‰
DX: stores the most significant part of the result in
16-bit multiplication
04/10/2010ELEG3230B Tutorial 37Programming Model –Registers
„
Pointer and Index Registers ‰
SP, BP, SI, DI usually store offset addresses
‰
SP: Stack Pointer
‰
BP: Base Pointer
‰
DI: Destination Index
‰
SI: Source Index
‰
IP usually stores offset address of the next instruction
04/10/2010ELEG3230B Tutorial 38Programming Model –Registers „
Segment Registers ‰
Stores the initial address information of the
corresponding memory segments
‰
CS: Code Segment
‰
DS: Data Segment
‰
SS: Stack Segment
‰
ES: Extra Segment
04/10/2010ELEG3230B Tutorial 39Programming Model –Segment and Offset
„
20-bit address in memory:
16-bit segment address + 16-bit offset
„
Calculate the physical address with
CS = 348Ah and IP = 4214h.
04/10/2010ELEG3230B Tutorial 310Programming Model –Segment and Offset
„
If it doesn’t state the segment, always use the
default segment register!
„
Examples: ‰
MOV CL, [BP] (SS:[BP] -> CL)
‰
MOV CL, DS:[BP] (DS:[BP] -> CL)
04/10/2010ELEG3230B Tutorial 311Programming Model –Sample Problems „
Assume
ES=6000h, CS=4000h, SS=7000h, DS=5000h,
IP=43E8h, SP=0000h, BP=9468h, SI=4C00h,
DI=7D00h, AX=4235h, BX=075Ah, CX=0004h,
DX=3302h. ‰
What is the next instruction’s physical address?
‰
What is the physical address of the top of the stack?
‰
Show the result of MOV ES:[BP], AH. Give the physical
address if a memory is affected.
04/10/2010ELEG3230B Tutorial 312Data Addressing Mode „
Register: move from registers (AX, BX, AL,…)
„
Immediate: move a constant value (without a [ ])
„
Direct: move a content of an address (value with a [ ])
„
Register Indirect: move a content of an address stored in a
register
„
Based: move using base pointers (BP/BX)
„
Indexed: move using index pointers (DI,SI)
„
Based Indexed: move using both base and index pointers
(e.g., [BP+SI])
„
String: move DS:[SI] to ES:[DI]
„
Port: Communicate to I/O ports (using IN/ OUT)
04/10/2010ELEG3230B Tutorial 314Data Addressing Mode –Sample Problems „
State the physical address and content (if
available) of the destination for each of the
following problems. Assume
DS=3000h, ES=4000h, SS=5000h, CS=6000h,
IP=3388h, SP=0FFh, BP=2468h, SI=4000h,
DI=50DDh, AX=2345h, BX=77EEh, CX=3333h,
DX=2222h. ‰
MOV CL, 12h
‰
MOV [BP], DX
‰
MOVSB
‰
MOV WORD PTR [SI+2], 20h
04/10/2010ELEG3230B Tutorial 315Assembly Language & Instruction Set (I) „
REP, REPZ, REPNZ ‰
Repeats an instruction and decrements the count in CX
‰
REP: repeats if CX is nonzero
‰
REPZ: repeats if CX is nonzero and zero flag is 1
‰
REPNZ: repeats if CX is nonzero and zero flag is 0
„
Example
MOV AX, 0h
MOV BX, 1h
MOV CX, 0h
REP ADD AX, BX
What is the value of AX and BX?
04/10/2010ELEG3230B Tutorial 316Assembly Language & Instruction Set (I) „
CMPS (compare string) & SCAS (scan string)
„
REPZ CMPS stringA, stringB ‰
Set CX to the length of the string, compare stringAwith
stringB
stringA: 1 2 3 4 5 6 1 2 3 4 5 6
stringB: 1 2 3 4 8 6 1 2 3 4 5 6
ZF of CMP: 0 0 0 0 1 <-stop!
„
REPNZ SCAS stringA ‰
Scan stringAuntil item in accumulator is found
stringA: 1 2 3 4 5 6 1 2 3 4 5 6
AL : 5 5 5 5 5 <-stop!
04/10/2010ELEG3230B Tutorial 317Assembly Language & Instruction Set (I) „
LOOPING ‰
JNZ : Conditional jump, jump not zero; if Z=0
„
Example
MOV CX, 0Ah
MOV AX, 5h
LOOOP: DEC CX, 1h
AND AX, CX
JNZ LOOOP
What are the values of AX and CX?
04/10/2010ELEG3230B Tutorial 318Assembly Language & Instruction Set (I) „
LOOPING ‰
LOOPNZ : Jump if both CX and Z are nonzero
„
Example
MOV CX, 0Ah
MOV AX, 5h
LOOOP: AND AX, CX
LOOPNZ LOOOP
What are the values of AX and CX?
04/10/2010ELEG3230B Tutorial 319Summary „
Recap: Flag registers
‰
CF, PF, AF, ZF, SF, OF
„
Programming Model
‰
General registers, pointer and index registers,
segment registers
‰
Calculate physical address with segment and offset
„
Data Addressing Mode
‰
Register, Immediate, Direct, Register Indirect, Based,
Indexed, Based Indexed, String, Port
„
Assembly Language & Instruction Set (I)
‰
REP, REPZ, REPNZ, CMPS, SCAS, LOOPING