Microprocessors I
Lecture Notes # 8
Outline of the Lecture
Control transfer instructions
CALL statement
Subroutines
CONTROL TRANSFER INSTRUCTIONS
FAR and NEAR : (given CS:IP)
In NEAR the control is transferred within the current code segment
(intrasegment). IP is changed.
In FAR the control is transferred outside the current code segment
(intersegment). Both CS and IP are changed.
short near
Instruction Description Flags jump jump
opcodes opcodes
JO Jump if overflow OF = 1 70 0F 80
JNO Jump if not OF = 0 71 0F 81
overflow
JS Jump if sign SF = 1 78 0F 88
JNS Jump if not sign SF = 0 79 0F 89
JE Jump if equal ZF = 1 74 0F 84
JZ Jump if zero
JNE Jump if not ZF = 0 75 0F 85
JNZ equal
Jump if not zero
JB Jump if below CF = 1 72 0F 82
JNAE Jump if not
JC above or equal
Jump if carry
1
short near
Instruction Description Flags jump jump
opcodes opcodes
JNB Jump if not CF = 0 73 0F 83
JAE below
JNC Jump if above or
equal
Jump if not carry
JBE Jump if below or CF = 1 76 0F 86
JNA equal
or ZF =
Jump if not
above
1
JA Jump if above CF = 0 77 0F 87
JNBE Jump if not
and ZF
below or equal
=0
JL Jump if less SF <> 7C 0F 8C
JNGE Jump if not
OF
greater or equal
JGE Jump if greater SF = OF 7D 0F 8D
JNL or equal
Jump if not less
JLE Jump if less or ZF = 1 7E 0F 8E
JNG equal
or SF
Jump if not
greater
<> OF
JG Jump if greater ZF = 0 7F 0F 8F
JNLE Jump if not less
and SF
or equal
= OF
JP Jump if parity PF = 1 7A 0F 8A
JPE Jump if parity
even
JNP Jump if not PF = 0 7B 0F 8B
JPO parity
Jump if parity
odd
2
CALL STATEMENTS
Another control transfer instruction is the CALL instruction, which is
used to call a 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
Shell of the Assembly Language Subroutines
;-------------------------------------------------------
CODSEG SEGMENT
MAIN PROC FAR
ASSUME .
MOV AX,
MOV DA,AX
CALL SUBR1
CALL SUBR2
CALL SUBR3
MOV AH,4CH
INT 21H
MAIN ENDP
;----------------------------------------------------
SUBR1 PROC
RET
SUBR1 ENDP
;----------------------------------------------------
SUBR2 PROC
RET
3
SUBR2 ENDP
;----------------------------------------------------
SUBR3 PROC
RET
SUBR3 ENDP
;----------------------------------------------------
CODSEG ENDS
END MAIN
Alternatively the following format can be used in
Simplified Segment Definition:
;-------------------------------------------------------
.CODE
MAIN: MOV AX,@DATA
MOV DS, AX
CALL SUBR1
CALL SUBR2
CALL SUBR3
MOV AH,4CH
INT 21H
;----------------------------------------------------
SUBR1:
RET
;----------------------------------------------------
SUBR2:
RET
;----------------------------------------------------
SUBR3:
RET
END MAIN
4
Assembly language programs examples
Write a program to read a character from the keyboard and display it
on the screen:
.model small
.stack 100h
.code
start:
mov ah, 1h ; keyboard input subprogram
int 21h ; read character into al
mov dl, al
mov ah, 2h ; display subprogram
int 21h ; display character in dl
mov ax, 4c00h ; return to ms-dos
int 21h
end start
Write a program to print the digits 0, 1 9.
.MODEL SMALL
.STACK 100H
.DATA
PROMPT DB 'The counting from 0 to 9 is : $'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
LEA DX, PROMPT ; load and print PROMPT
MOV AH, 9
INT 21H
MOV CX, 10 ; initialize CX
MOV AH, 2 ; set output function
MOV DL, 48 ; set DL with 0
@LOOP: ; loop label
INT 21H ; print character
INC DL ; increment DL to next
ASCII character
DEC CX ; decrement CX
JNZ @LOOP ; jump to label @LOOP if
CX is 0
MOV AH, 4CH ; return control to DOS
5
INT 21H
MAIN ENDP
END MAIN
;------
;OUTPUT
;------
; 0123456789
Note
MOV DL, 48 is equivalent to MOV DL, 0. 48 is the ASCII code to 0.
ASCII Hex Symbol
48 30 0
49 31 1
50 32 2
51 33 3
52 34 4
53 35 5
54 36 6
55 37 7
56 38 8
57 39 9
58 3A :
59 3B ;
60 3C <
61 3D =
62 3E >
63 3F ?
The difference between output a character and output a string
Output a character
MOV DL, ...
MOV AH, 02h
INT 21h
Load the desired character in to DL, then call the interrupt with function
code 2 in AH.
6
Output a string
MOV DX, ...
MOV AH, 09h
INT 21h
Load the address of a '$'-terminated string into DX, then call the interrupt
with function code 9 in AH.
LEA ( Load Effective Address )
The format of LEA instruction is
LEA register, source
LEA is an instruction which calculates an a address and then then loads it
into the double register designated in the mnemonic. It must use the indexed
addressing mode.