L7 Controlul fluxului



Yüklə 63 Kb.
tarix07.11.2018
ölçüsü63 Kb.
#78702

Laboratory Work no. 7

Flow control instructions




Object of laboratory

The 8086 family microprocessors have a large variety of instructions that allows the instruction flow control. They divide in 4 categories: jump, cycling, calling and break instructions.



Theoretical considerations

Jump instructions.


Jumping is the most direct method of modifying the instruction flow. At the internal level the jump instructions work by changing the value of the IP register and sometimes of the CS register (between segments jumping), so the next instruction address that exists in this registers will be changed with the destination address.

The unconditional jump.


The JMP instruction is used for making an unconditional jump to a specified address. The jump between segments can be short or near, regarding the destination address that can be between -127..127 of the current address or is situated just in the same segment. The jump between segments is far type.

From the destination address specification point of view there are direct and indirect jumps. In the direct jumps, the destination address is specified through a label. The syntax is:


JMP label
For the jumps in the same segment, the addressing is IP relative.

After the instruction code there is a displacement on an octet or two octets that represent the distance from the current address to the destination.


Example:

ALFA:


JMP ALFA

If the label is situated under 128 octets far from the jump instruction and has been defined before, than a short jump type is codified. If the label is defined after the jump instruction than a near jump type is codified, indifferent if the distance between the jump instruction and label is lesser or not than 128 octets. In this situation for obtaining a short codification type there can be used the SHORT operator.
Example:

JMP SHORT BETA

BETA:
Observation: Using the SHORT operator in an improper situation is signalized as an error.



In case of using the direct addressing for doing jumps between segments after the instruction code is followed by a displacement of four octets that represent the destination address.

If the label has been defined before, the codifying is correct. If the label is being defined afterwards, is necessary to specify the FAR type for this label when calling.

Example:

JMP FAR PTR GAMA

GAMA:
In case of indirect jumping, the destination address is specified through an operand, the syntax is:


JMP {register| memory}
Example:

JMP AX


JMP [BX]

JMP ALFA ; ALFA is a var. word or double word


If the variable is defined after the jump instruction in the case of between segment jumping there must be used DWORD PTR operator.
Example:

JMP DWORD PTR ALFA

An unconditional jump can be used as ca conditional jump form if the destination

address is specified in a register or a memory location.


The destination address can be calculated while executing the program based on the interaction with the user or other factors.

Example:


code segment

jmp proces


ctl_tbl LABEL WORD

dw extended ; the key with extended code(2 car.)

dw ctrla ; the key CTRL/A

dw ctrlb ; the key CTRL/B
proces:

mov ah, 8h ; reading the key in AL

int 21h


cbw

mov bx, ax



shl bx, 1 ; the address calculation in the table

jmp ctl_tbl[bx]


extended:

mov ah, 8h ; takes the second cod

int 21h



ctrla: ; routine for CTRL/A

jmp next



ctrlb: ; routine for CTRL/B

jmp next



next: ; continue

code ends


Conditional jumps


The conditional jump is the most frequent method of modifying the instruction flow. It consists of a process in two steps. In the first step the condition is tested and in the second the jump is done if the condition is true or the next instruction in executed if the condition is false

The jump instruction syntax is:

Jcondition label
The conditional jumps are short type, so that the distance at the destination address must be lesser than 128 octets. In a contrary case the error is signaled. The destination address is specified through a displacement on an octet of the current address. The conditional jumps use as a condition the indicators state or combinations of those.

The testing step is realized with the help of the instructions that affect the condition indicators. In this purpose the most frequent are used the CMP and TEST instructions.

The jumping step is made using one of the 13 conditional jump instructions.

If one conditional salt must be greater than 128 octets, it must be replaced through a conditional jump of reverse condition followed by an unconditional jump.


Example:

cmp ax, 7

je NEAR

cmp ax, 6 ; if AX is 6 and the jump is greater than 128 octets

; the instruction of conditional jumping is replaced

jne NEAR

jmp FAR



NEAR: ; less than 128 octets

; from the jump instruction



FAR: ; more than 128 octets

; from the jump instruction


Compare and jump

The CMP instruction compares 2 operands through substraction of the operand source from the destination operand without affecting the destination and with the proper entry of the condition indicators. The syntax:

CMP {register memory, {register | memory | immediate value}
It is used for testing the next relations: equal, not equal, greater than, less than, greater or equal than, less or equal than.

The conditional jump instruction used after the compare instruction has the flow chart accordance with the tested relation, generated form the next letters:


LETTER SEMNIFICATION
J Jump

G Greater than (for no. with sign)

L Less than (for no. with sign)

A Above (for no. without sign)

B Below (for no. without sign)

E Equal


N Not
In the next table there are represented the conditional jump instruction according to each relation:


Jump condition

Compare with sign

Jump condition

Compare without sign

Jump condition

Equal =

JE

ZF=1

JE

ZF=1

Not equal <>

JNE

ZF=0

JNE

ZF=0

Greater than >

JG or JNLE

ZF=0 and SF=OF

JA or JNBE

ZF=0 and CF=0

Less than <

JL or JNGE

SF<>OF

JB or JNAE

CF=1

Greater than or equal >=

JGE or JNL

SF=OF

JAE or JNB

CF=0

Less than or equal <=

JLE or JNG

ZF=1 or SF=OF

JBE or JNA

CF=1 or ZF=1

Example


; IF (CX<-20) THEN DX=30 ELSE DX=20

cmp cx, -20

jl less

mov dx, 20



jmp cont

less:


mov dx, 30

cont:


Example:

; IF (CX>=-20) THEN DX=30 ELSE DX=20

cmp cx, -20

jl notless

mov dx, 20

jmp cont


less:

mov dx, 30

cont:

Jumps based on the conditional pointers state:





INSTRUCTIONS

JUMP CONDITIONS

JO

JNO


JC

JNC


JZ

JNZ


JS

JNS


JP

JNP


JPE

JPO


JCXZ

OF=1

OF=0


CF=1

CF=0


ZF=0

ZF=1


SF=1

SF=0


PF=1

PF=0


PF=1

PF=0


CX=0

As it can be observed JCXZ is the only conditional jump instruction that does not test the conditional pointers but the content of the CX register.


Example:

add ax, bx

jo overflow

overflow:


Cycling instructions


The cycling instructions allow an easy programming of the control structures of the final test cycle type.

The syntax of these instructions is:


LOOP label ; CX is decremented and if CX is not null the jump is                         ; done.

LOOPE label ; CX is decremented and if CX is not null and ZF=1 the                         ; jump is done.

LOOPZ label ; identical with LOOPE

LOOPNE label ; CX is decremented and if CX is not null and ZF=1 the                         ; jump is done.

LOOPNZ label ; identical with LOOPNE
The cycling instructions decrement the content of the CX register and if the jump condition is fulfilled the jump is done.

The distance between the looping instruction and the destination address must be between the interval -128..127 octets


Example:

mov cx, 200 ; initialize counter

next:




loop next ; repeat if CX in not null

; continue after the cycle


This loop has the same effect as the one in the next example:

Example:


mov cx, 200

next:


dec cx


cmp cx, 0

jne next
The first version is more efficient.


Using the JCXZ instruction allows us to realize some control instructions of the starting test cycle type.
Example:

next:


jcxz cont

loop next



cont:

Using the procedures

The procedures are code units that execute specific functions. They represent a way of modulating the code so that a specific function can be executed from any other point in the program without having to introduce each block.


The procedures from the assembler language are comparable with the C functions, the subprograms, the functions and the subroutine from BASIC, subroutine and the functions from FORTRAN etc.

For defining and using some procedures there are two directions and instructions. So the PROC and ENDP directions mark the beginning and the end of defining the procedure. The CALL instruction is used for calling the defined procedures, and the RET instruction is used for controlling the transfer to the caller.

The CALL and RET instructions use the stack for saving and retrieval the address of return. The CALL instruction introduces in stack the return address (the address of the next CALL instruction) and then the jump to the address at the beginning of the procedure is done.

The RET instruction extracts from the stack the address introduced by the CALL instruction and returns the instruction control of the next immediate called address.

The procedures can be found or not in the same segment with the called instructions.

From this point of view there are NEAR and FAR type procedures. When declaring the procedures their type is declared too. The NEAR type is implicit.


The procedure definition syntax is:


Label PROC [NEAR | FAR]

RET [constant]



Label ENDP
The RET instruction allows one constant operand that specify a number of octets that will be added to the content of the SP register after returning from the procedure. This operand can be used for deleting from the stack the arguments that were transmitted to the procedure through the stack.

The calling procedure syntax is:


CALL {register | memory}

Lab tasks


  1. There will be studied the instructions and the examples presented before.

  2. There will be written a program sequence that transforms the code of a small letter in the code of the big letter. The code will be taken from one memory location and it will be putted in the same memory location.

  3. Write a program that calculates the arithmetic average of the numbers from a set that has values between 5 and 10. Write the average obtained on the display plotting “The average is: 8”. The average will be calculated as a whole number, and for the interruptions of printing data on the display The Norton Guide functions: will be studied.

  4. Write a program that plots an integer without sign from the AX register. Indication: more divides by 10 will be made, and the obtained digits will be printed in the reverse order.

  5. Write program that reads an integer without sign from the keyboard until the enter key is pressed. Indication: every digit you read will be converted to its numeric value and the writing model of writing a number will be used like this: 145=(((1*10)+4)*10)+5

  6. Write a procedure that converts a hex digit (00-0F) in an ASCII character. Transmitting the hex digit to the procedure is done in the AL register, and the procedure returns the ASCII character in the same register. Write the procedure calling for more cases and test.

  7. Write a procedure for converting an ASCII character into a hex digit. The conversion of the ASCII character (to the procedure) and the hex digit (to the calling program) in done through the AL register. The procedure will set the CF indicator in the case where the ASCII character does not correspond to a hex digit.

  8. Write a procedure for comparing two sets of ASCII characters, found in different segments. The address of the sets is delivered to the procedure in registers. At exit, AL will contain 00 if the sets are identical and FF if the sets are different.







Yüklə 63 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə