Professional Documents
Culture Documents
MIT06
MIT06
MIT06
s & Interfacing
Techniques
Lecture 06
Contents
Program Flow Control
Conditional Instructions
Loops
Procedures
2
Program Flow Control
Controlling the program flow is a very important
thing, this is where your program can make
decisions according to certain conditions.
Remember the if statement in C programming
language.
In the Assembly language, flow control is handled
by some several Jump instructions.
3
Unconditional Jumps
The basic instruction that transfers control to
another point in the program isJMP:
JMPlabel
To declare alabelin your program, just type its
name and add ":" to the end.
A label can be any character combination but it
cannot start with a number. Examples:
label1:
label2:
a:
4
More About Labels
Label can be declared on a separate line or before
any other instruction
Examples:
x1:
MOV AX, 1
5
JMP Example
ORG 100H
MOV AX, 5 ; set AX to 5.
MOV BX, 2 ; set BX to 2.
JMP calc ; go to 'calc'.
back:
JMP stop ; go to 'stop'.
calc:
ADD AX, BX ; add BX to AX.
JMP back ; go 'back'.
stop:
RET ; return to operating system. 6
JMP in Detail
There are three types of the JMP instruction:
1. Short Jump
2. Near Jump
3. Far Jump
Short and near jumps are intrasegment
jumps and far jump is intersegment jump.
7
Jump Types
8
Short Jump
The jump address is not stored in the
opcode.
Instead, a distance, or displacement,
follows the opcode.
Therefore, short jumps are also called
relative jumps.
The displacement takes a value in the
range between -128 and +127 Bytes.
9
Short Jump Illustration
The displacement is
added to instruction
pointer (IP) to
generate the jump
address within the
current code
segment.
The instruction
branches to this
new address for
the next instruction 10
in the program.
Short Jump Example
XOR BX, BX
START: MOV AX, 1
ADD AX, BX
JMP SHORT NEXT
;some instructions here
NEXT: MOV BX, AX
JMP START
12
Near Jump Example
XOR BX, BX
START: MOV AX, 1
ADD AX, BX
JMP NEXT
NEXT: MOV BX, AX
JMP START
13
Far Jump
A far jump instruction obtains a new
segment and offset address to
accomplish the jump.
The far jump instruction sometimes
appears with the FAR PTR directive.
Another way to obtain a far jump is to
define a label as a far label. A label is
far only if it is external to the current
code segment or procedure (i.e. the
compiler sets it automatically). 14
Conditional Jumps
UnlikeJMPinstruction that does an
unconditional jump, there are instructions
that do a conditional jumps (jump only when
some conditions are in act)
Conditional jumps are always short jumps.
These instructions are divided in three
groups:
First group just tests single flag
Second group compares numbers as signed
Third group compares numbers as
unsigned. 15
General Syntax
The general syntax for conditional
jumps:
<Conditional jump instruction>
<label>
Example: JC label1
If the condition under the test is true, a
branch to the label occurs.
If the condition is false, the next
sequential step in the program
16
executes.
Jump Instructions That Test Single
Flag
Instruction Description Conditio Opposit
n e
JZ, JE Jump if Zero (Equal) Z=1 JNZ, JNE
JC, JB, JNAE Jump if Carry (Below, Not C=1 JNC, JNB,
Above Equal) JAE
JS Jump if Sign S=1 JNS
JO Jump if Overflow O=1 JNO
JPE, JP Jump if Parity Even P=1 JPO, JNP
JNZ, JNE Jump if Not Zero (Not Equal) Z=0 JZ, JE
JNC, JNB, Jump if Not Carry (Not Below, C=0 JC, JB,
JAE Above Equal) JNAE
JNS Jump if Not Sign S=0 JS
JNO Jump if Not Overflow O=0 JO
JPO, JNP Jump if Parity Odd (No Parity) P=0 JPE, JP 17
27
Procedures
Procedure is a part of code that can
be called from your program in order to
make some specific task (remember
the functions in C).
Procedures make program more
structural and easier to understand.
Generally procedure returns to the
same point from where it was called.
28
Procedure Syntax
namePROC
; here goes the code
; of the procedure ...
RET
nameENDP
30
Some Notes on the
Syntax
Probably, you already know thatRETinstruction is
used to return to operating system.
The same instruction is used to return from
procedure (actually operating system sees your
program as a special procedure).
PROCandENDPare compiler directives, so they
are not assembled into any real machine code.
Compiler just remembers the address of
procedure.
31
Calling a Procedure
CALLinstruction is used to call a procedure.
Example:
ORG 100H
CALL m1
MOV AX, 2
RET ; return to operating system.
m1PROC
MOV BX, 5
RET ; return to caller.
m1ENDP
END
32
CALL and RET
CALL saves a return address on the
stack.
RET removes the return address from
the stack and places it into IP.
Dont forget to put a RET at the end of
a procedure!
You can use several RETs in a
procedure (like return in C).
33
Passing Parameters to a
Procedure
There are several ways to pass parameters to a
procedure
The easiest way is by using registers
Put parameters to the registers and call the
procedure
The procedure should be designed so that it
uses those registers
Another way is by using stack
Push the parameters to the stack and call the
procedure
The procedure should be designed so that it
receives parameters from the stack 34
In either way, the programmer should know the
structure of the procedure
Example
ORG 100h
MOV AL, 1
MOV BL, 2
CALL m2
RET ; return to operating system.
m2 PROC
MUL BL ; AX = AL * BL.
RET ; return to caller.
m2 ENDP
END
35
MACHINE CONTROL AND
MISCELLANEOUS
INSTRUCTIONS
Controlling the Carry Flag Bit
The carry flag (C) propagates the carry or borrow
in multiple-word/doubleword addition and
subtraction.
It also can indicate errors in assembly language
procedures.
Three instructions control the contents of the
carry flag: STC (set carry), CLC (clear carry), and
CMC (complement carry).
Because the carry flag is seldom used except with
multiple-word addition and subtraction it is
available for other uses.
The most common task for the carry flag is to
indicate an error upon return from a procedure.
WAIT
this instruction waits for the coprocessor to
complete the task
HLT
The halt instruction (HLT) stops the execution of
software.
NOP
When the microprocessor encounters a no
operation instruction (NOP), it takes a short time
to execute. was often used to pad software with
space for future machine language instructions
Taking Input from
Keyboard
mov ah, 1h ; keyboard input subprogram
int 21h ; character input
; character is stored in al
mov cl, al ; copy character from al to cl
Character Output
mov dl, a ; dl = a
mov ah, 2h ; character output
subprogram
int 21h ; call ms-dos output
character
Endianness
In computing,endianandendiannessrefer to
conventional orderings of components (not only
ofintegerbytes) as stored inmemory
Little Endian
In little endian, you store theleastsignificant byte in the
smallest address. Here's how it would look:
Big Endian