microprocessor programming ii to discuss more complicated programming techniques flag control...

42
Microprocessor Programming II • To discuss more complicated programming techniques • Flag control instructions • Compare and jump • Subroutines • Loop and string instructions

Upload: sharon-campbell

Post on 27-Dec-2015

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Microprocessor Programming II

• To discuss more complicated programming techniques

• Flag control instructions

• Compare and jump

• Subroutines

• Loop and string instructions

Page 2: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Flag instructions• Can affect the setting of flags directly

• Can store value of AH into flags

• Can clear/set the carry

• Can clear/set interrupt

• Complement carry flag (0->1 , 1->0)

Page 3: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Flag InstructionsMnemonic Meaning Flags Affected

LAHF Load AH from flag None

SAHF Store AH into flags SF, ZF, AF, PF, CF

CLC Clear carry CF

STC Set carry CF

CMC Complement carry CF

CLI Clear interrupt IF

STI Set interrupt IF

Page 4: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Flag instruction • Can you identify one application for

clearing the carry flag?

• Example before a rotation through carry

• Example if you want to calculate the average value of a series of data, the first time you use ADC

Page 5: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Compare instruction

• To compare two 8-bit or 16-bit numbers

• Operands can stored in Register, memory, ACC (AX), or an immediate

• The compare operation will affect the following flags: overflow, sign, zero, auxiliary carry, parity, and carry

• Instruction mnemonic is CMP

• Usually use compare operation when decision to branch (or jump) is required

Page 6: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Compare• In the compare operation, the source operand is

subtracted from the destination operand• But the result of the subtraction is not saved (i.e.

the values in the source and destination did not change)

• Flags are changed according to the subtraction result

• After the compare, we can do a conditional jump based on the flags status

• In C++ , we do if(x==0) – cmp x, #00h (compare X and 0 so if x = 0 then x-0

will set the zero flag!!)– JZ abc ; JZ – jump if zero flag is set

Page 7: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Example

• CMP 10011001, 00011011• Do 10011001 - 00011011• Results of flags after the compare• AF is set • CF is clear• OF is set • PF is set• SF is clear• ZF is clear

Page 8: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Exercise

Describe what will happen to the status flags (C, S, O, Z) as the sequence of instructions that follow is executedMOV AX, 1234HMOV BX, ABCDH ; this is a 16-bit valueCMP AX, BXAssume that initially all flags are reset (0)The CF=1

Page 9: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Change the flow of the program

Normal program flowIs top-down

The flow of aprogram can also be changedby:jumpConditional JumpSubroutine calllooping

Page 10: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Program flow control• Jump (jmp) – after the jump, the program will

execute instructions at the jump label and will not return

• Syntax: jmp label• Subroutine call (call) – program will go to the

location of the subroutine, execute the subroutine and when it finishes return back to the original program

• Syntax: call function (or acall function)– Function is a user defined name representing the

subroutine

Page 11: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

JUMP instruction

• To alter the execution path of instructions in the program

• By changing the values of the code segment register and the instruction pointer

• Program execution is not intended to return to the next sequential instruction after the jump, so no return linkage is saved

• Two kinds of jump operation: unconditional and conditional

Page 12: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Unconditional Jump• As its name implies, there are no status (condition)

requirements imposed for the jump to occur• As the instruction is executed, the jump always

takes place to change the execution sequence

Conditional JumpThe jump operation depends on some status conditions (for example jump if Carry is set!)If condition(s) is/are met, then the jump takes place; Otherwise execution continues with the next sequential instruction of the program

Page 13: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Unconditional jump• There are 2 kinds of unconditional jump:

intrasegment jump, and intersegment jump

• Intrasegment jump is limited to addresses within the current code segment (only the IP value is changed)

• Intersegment jump permits jumps from one code segment to another (both CS and IP values are changed)

Page 14: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Examples for unconditional jump• JMP $ -14 • Short-label – an 8-bit number is coded as an

immediate operand to specify a signed displacement (+127 to –128)

• IP = IP - 14• JMP START• Near-label – operand specifies a new value of IP

with a 16-bit displacement – START is a label

Page 15: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Intersegment jump

• Use a 32-bit immediate operand to specify the jump address

• The operand can be called a far-label

• The first 16 bits are loaded to IP and the next 16 bits are loaded into the CS

Page 16: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Example of unconditional jumpXOR BX,BX ; why doing this ?????Start: mov AX, #1

ADD AX,BXJMP NEXT

NEXT: mov BX, AXJMP start

Page 17: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Conditions• Conditions that can be referenced by a conditional

jump: CF, PF and OF• Mnemonic for condition jump• JC jump if CF=1• JS jump if SF=1 (could set by CMP, SUB, ADD,

AND, OR, XOR )• JB jump if CF =1 (but CF is set after a CMP

operation )• JNL jump if first operand is not less than second

operand in the CMP operation (SF = OF)

Page 18: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Exercise• The program that follows implements what is

known as a delay loop• MOV CX, 1000• DLY: DEC CX

JNZ DLY ; jump if not zero (zero is specified by what?)

NXT: -----

How many times does the JNZ DLY get executed?

Change the program so that JNZ DLY is executed just 17 times

Page 19: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

PUSH and POP• PUSH and POP are used to stored data onto the stack.

Stack is used as a temporary storage • When performing PUSH AX ; value of AH is stored

in SP-1, value of AL is stored in SP-2, the new value of SP is SP-2

• SP – stack pointer (offset)• During a POP AX• Content of SP is stored in AL• Content of SP+1 is stored in AH• New value of SP is SP+2

Page 20: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Stack operationTOS – Top Of Stack

Page 21: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Stack operation

Page 22: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Subroutines• A subroutine is a special segment of program that can

be called for execution from any point in a program (similar to a function in C program)

• To invoke a subroutine, we perform a CALL • Using subroutines can reduce the size of the program

and make the program easy to read• When performing a CALL operation, value of IP is

changed in order to branch to the location of the subroutine

• When the subroutine is completed then the program returns to the main program so the last statement in the subroutine must be RETURN

Page 23: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Subroutine• There are intrasegment call and

intersegment call• During a CALL operation, value of IP is

stored in the STACK• The IP value saved is the instruction

following the CALL operation• Example: CALL 1234 (1234 is a 16-bit

displacement that gets added to IP)• Example: Call delay

– Delay is the name of a subroutine

Page 24: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

RETURN operation• Mnemonic for RETURN is RET

• During the return operation, value of IP or both the values of IP and CS that were saved on the stack to be returned back to their corresponding registers

Page 25: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Example

Write a procedure named SUM that adds 2 numbers 5 and 31 together and places their sum in DX. Assume that this procedure is to be called from another procedure in the same code segment and that at the time it is to be called, DX contains the value of ABCD16 and this valuemust be saved at entry of the procedure and restored at its completion.

Page 26: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Sample solution

Sum proc nearpush dxmov dx, 5add dx,31pop dxret

Sum endp

Page 27: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Loop instructions• A mechanism to repeat a sequence of

operations repeatedly• Three different operations: loop,

loope/loopz, and loopne/loopnz• The number of looping is stored in CX• Whenever LOOP is executed, the contents

of CX are first decremented by 1 and then checked to determine if they are equal to 0

• If CX equals to 0, then return to the instruction following the loop statement

Page 28: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Example

mov cx, countNext: ……..

Loop Next

Syntax of the loop operation

Do something else

Loop repeated

Check CXFor 8051, one looping function isDJNZ (do jump if not zero )

Page 29: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Loop while equal• Loop while equal (loope) checks the

contents of both CX and the ZF flag. Each time the loop instruction is executed, CX decrements by 1 without affecting the flags, its contents are checked for 0, and the state of ZF that results from execution of the previous instruction is tested for.

• Loope (loop if CX != 0 and ZF = 1)

• Loopne (loop if CX != 0 and ZF = 0)

Page 30: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

ExerciseGiven the following sequence of instructions:

mov AX, 0mov DS, AXmov AL, 05mov DI, A000mov CX, 0FH

Again: inc DIcmp [DI], ALloopne Again

Next:

Explain what happens as they are executedAns. Search for 05 for addresses A001H to A010H

Page 31: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

String instructions• String refers to a series of data words (or

bytes) that reside in consecutive memory locations.

• In C++, you get the strcpy function• Can move a data from a block of memory to

a block elsewhere• To scan a string of data elements stored in

memory looking for a specific value• To compare two strings• Five basic string instructions

Page 32: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

String Operation

String db “abcd”

a

d

Memory (Data Segment)

0000

FFFF

SI

Memory (Extra Segment)

0000

FFFF

DI

Source Destination

Page 33: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Basic string instructionsMnemonic meaning format

MOVS Move string MOVs operand

MOVSB Move string byte

MOVSB

Movsw Move string word

Movsw

Cmps Compare string Cmps operand

Scas Scan string Scas operand

Lods Load string Lods operand

Stos Store string Stos operand

Page 34: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

String operations• The instructions MOVS, MOVSB, MOVSW all

perform the same basic operation. • An element of the string specified by the source

index (SI) register with respect to the current data segment (DS) register is moved to the location specified by the destination index (DI) register with respect to the current extra segment (ES) register

• It can be a byte or word operation• After the move is completed, the contents of both SI

and DI are automatically incremented or decremented by 1 for byte move, or by 2 for word move

Page 35: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Direction flag

Direction flag selects the auto-increment or the auto-decrement operation for the DI and SI registers during string operations.The direction flag is used only with the string instructions.

CLD – clear direction flag (D=0 auto increment)

STD – set (D=1 auto decrement)

Page 36: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Move string• Increment or decrement is controlled by the direction flag DF• Example of copying N bytes of characters from blk1addr to

blk2addr• Mov AX, datasegaddr ; load AX with data segment • Mov DS, AX ; address• Mov ES, AX ; ES = DS point to the same segment• LEA SI, blk1addr ; note this is moving the address• LEA DI, blk2addr• Mov CX, N• CLD ; clear the direction flag - increment • Next: movsb• Loop next• HLT ; halt = stop

Page 37: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Compare strings• Compare operations perform: subtracts the

destination operand from the source operand and adjusts flags CF, PF, AF, ZF, SF, and OF accordingly.

• The result of subtraction is not saved• Operation does not affect the operands• Example

– cmpsb ; compare byte no operand• Source is pointed to by the address in SI• Destination is specified by DI • SI and DI are updated after the operation and

pointed to the next element

Page 38: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Scan string• AL, or AX stores a target element

• Destination string is referred by DI and ES

• Flags are adjusted based on the operation and DI incremented or decremented

Page 39: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Example for scan• Example: mov Ax, 0• mov DS, AX• mov ES, AX• mov AL, 05H• mov DI, A000H• mov CX, 0F• CLD ; clear direction flag• Again: scasb ; scan byte no operand• loopne again ; stop if find the same byte • Next:

Page 40: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Load and store string• To move string elements between the accumulator

and memory• LODS loads either a byte or a word from a string

in memory into AL or AX, respectively• Address is derived from SI and DS• Example: LODSW• Load AX with a word• Value of SI is incremented by 2• STOS stores a byte from AL or a word from AX

into a string location in memory• Location is generated by ES and DI

Page 41: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

Examplemov AX, 0mov DS, AXmov ES, AXmov AL, 05mov DI, A000mov CX, 0FCLD

Again: stosbloopne again

Next:

Store the value 5 into location A000 to A00F

Page 42: Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string

ExerciseDescribe what will happen as the following sequence of instructionsis executed

CLDMov AX, data_segmentMov DS, AXMov AX, extra_segmentMov ES, AXMov CX, 20LEA SI, offset_masterLEA DI, offset_copyOps:

CMPSBLoop opsAns. The two strings are compared for 20 bytes