lecture06
lecture06
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 is JMP:
JMP label
• To declare a label in 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
Note: You don’t have to use SHORT
directive as the compilers choose the
best form of the jump instruction. 11
Near Jump
• The near jump is similar to the short jump,
except that the distance is farther.
• A near jump passes control to an instruction in
the current code segment located within ±32K
bytes from the near jump instruction.
• Near jump is also a relative jump.
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
• Unlike JMP instruction 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 executes.
16
Jump Instructions That Test Single Flag
Instruction Description Condition Opposite
20
Conditional Jump Example
ORG 100H
MOV AL, 25 ; set AL to 25.
MOV BL, 10 ; set BL to 10.
CMP AL, BL ; compare AL - BL.
JE equal ; jump if AL = BL (Z = 1).
MOV CX, 1 ; if it gets here, then AL <> BL
JMP stop ; so set CX, and jump to stop.
equal: ; if gets here,
MOV CX, 0 ; then AL = BL, so clear CX.
stop:
RET ; gets here no matter what. 21
Loops
Instruction Operation and Jump Condition Opposite Direction
LOOP Decrease CX, jump to label if CX not zero DEC CX and JCXZ
LOOPE Decrease CX, jump to label if CX not zero LOOPNE
and equal (Z = 1)
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
• name PROC
; here goes the code
; of the procedure ...
RET
name ENDP
29
Procedure Example
SUMS PROC
ADD AX, BX
ADD AX, CX
ADD AX, DX
RET
SUMS ENDP
30
Some Notes on the Syntax
• Probably, you already know that RET instruction 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).
• PROC and ENDP are compiler directives, so they are not
assembled into any real machine code. Compiler just
remembers the address of procedure.
31
Calling a Procedure
• CALL instruction is used to call a procedure.
• Example:
ORG 100H
CALL m1
MOV AX, 2
RET ; return to operating system.
m1 PROC
MOV BX, 5
RET ; return to caller.
m1 ENDP
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.
• Don’t 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
• In either way, the programmer should know the structure of
the procedure
34
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, endian and endianness refer to conventional
orderings of components (not only of integer bytes) as stored
in memory
• Little Endian
In little endian, you store the least significant byte in the
smallest address. Here's how it would look:
• Big Endian