Fall 2019/20 - Lecture Notes # 7: - Control Transfer Instructions
Fall 2019/20 - Lecture Notes # 7: - Control Transfer Instructions
8086
• The instruction “JNZ AGAIN” is assembled as “JNZ 000D” and the 000D is the address of
the instruction with label AGAIN.
• (000D = 0013+FA = 000D) the carry is dropped. Note that FA is 2’s complement of –6,
meaning that the address of target is –6 bytes from the IP of the next instruction.
EENG410: MICROPROCESSORS I
8086
Control transfer instructions
• Short Jumps
Ex: 0005 8A 47 02 AGAIN: MOV AL,[BX]+2
0008 3C 61 CMP AL,61H
000A 72 06 JB NEXT
000C 3C 7A CMP AL,7AH
000E 77 02 JA NEXT
0010 24 DF AND AL,DFH
0012 88 04 NEXT: MOV [SI],AL
• The displacement value is added to the IP of the next instruction to calculate the jump
address.
(06 + 000C = 0012) (02 + 0010 = 0012)
EENG410: MICROPROCESSORS I
8086
Control transfer instructions
• Unconditional Jumps
▪ “JMP label” is the unconditional jump in which control is transferred unconditionally to
the target label. Unconditional jump can take the following forms.
1. SHORT JUMP, which is specified by the format “JMP SHORT label” . In this jump the
address of the target location is within –128 to +127 bytes of the current IP. It works like
the conditional jump.
2. NEAR JUMP, which is the default has the format “JMP label”. It jumps within the
current code segment. It is exactly same as the short jump, except that the target
address can be anywhere within the range of +32767 to –32768.
3. FAR JUMP, has the format “JMP FAR PTR label”. This is the jump out of the current
code segment, meaning that not only the IP, but also the CS is replaced with new
values.
EENG410: MICROPROCESSORS I
8086
Control transfer instructions
• CALL statement
• Another control transfer instruction is the CALL instruction, which is used to call a sub-
procedure.
• The target address can be in the current segment, hence a NEAR call (IP is changed CS is
not).
• The target address can be outside the current segment, hence FAR call (IP and CS are
changed).
• To make sure that after the execution of the called subroutine the microprocessor
knows where to come back, the microprocessor automatically saves the address of the
instruction following the call on the stack.
• The last instruction of the called subroutine must be RET (return) instruction, which
directs CPU to pop the address of the next instruction before the called subroutine to be
restored.
• Subroutines
▪ In Assembly Language there can be one main program and many subroutions called from
the main program. Subroutines are organized as procedures. PROC can be FAR or
NEAR. If not mentioned, by default a PROC is NEAR
EENG410: MICROPROCESSORS I
8086
Control transfer instructions
• Shell of the Assembly Language Subroutines.
;-------Full Segment Definition ---------------- ;------- Simplified Segment Definition --------
.CODE
CODSEG SEGMENT
MAIN: MOV AX,@DATA
MAIN PROC FAR
MOV DS, AX
ASSUME ……. :
MOV AX,… CALL SUBR1
MOV DA,AX :
: CALL SUBR2
CALL SUBR1 :
: MOV AH,4CH
CALL SUBR2 INT 21H
: ;----------------------------------------------------
MOV AH,4CH SUBR1: …
RET
INT 21H
;----------------------------------------------------
MAIN ENDP
SUBR2: …
;---------------------------------------------------- RET
SUBR1 PROC END MAIN
…
RET
SUBR1 ENDP
;----------------------------------------------------
SUBR2 PROC
…
RET
SUBR2 ENDP
;----------------------------------------------------
CODSEG ENDS
END MAIN
EENG410: MICROPROCESSORS I
8086
Data Types and Data Definition
• Assembler Data Directives
▪ ORG (Origin) : ORG is used to indicate the beginning of the offset address. The number
after ORG can be either in hex or decimal. Mostly used in Data segment.
Ex: .DATA
DATA_IN DB 93H,86H,3BH,5AH
ORG 0010H
SUM DW ? ; SUM variable is at offset 0010H in DS.
▪ DB (define byte) : DB allows allocation of memory in bytes for decimal, binary, hex and
ASCII.
Ex: .DATA
DATA1 DB 25 ;DECIMAL D IS OPTIONAL
DATA2 DB 10001001B ;BINARY
DATA3 DB 12H ;HEX
ORG 0010H
DATA4 DB ‘2591’ ;ASCII NUMBERS
0RG 0018H
DATA5 DB ? ;SET ASIDE A BYTE
ORG 0020H
DATA6 DB ‘My name is Joe’ ;ASCII CHARACTERS
EENG410: MICROPROCESSORS I
8086
Data Types and Data Definition
• Assembler Data Directives
▪ DUP (duplicate): DUP is used to duplicate a given number of characters.
▪ DW(define word): DW is used to allocate 2 bytes (one word) of memory at a time. DUP
is used to duplicate a given number of characters.
▪ DT(define ten bytes): Allocates 10 bytes of memory space. Mostly used for BCD
numbers.