0% found this document useful (0 votes)
11 views

lecture06

Uploaded by

Hamza Satti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

lecture06

Uploaded by

Hamza Satti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Microprocessors &

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

x2: MOV AX, 2

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

JZ, JE Jump if Zero (Equal) Z=1 JNZ, JNE


JC, JB, JNAE Jump if Carry (Below, Not Above Equal) C=1 JNC, JNB,
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, JAE Jump if Not Carry (Not Below, Above C=0 JC, JB, JNAE
Equal)
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

* Different names are used to make programs easier to understand.


Jump Instructions for Signed Numbers
Instruction Description Condition Opposite
Jump if Equal (=)
JE, JZ Z=1 JNE, JNZ
Jump if Zero
Jump if Not Equal (≠)
JNE, JNZ Z=0 JE, JZ
Jump if Not Zero
Z=0
Jump if Greater (>)
JG, JNLE and JNG, JLE
Jump if Not Less or Equal (not <=)
S=O
Jump if Less (<)
JL, JNGE S≠O JNL, JGE
Jump if Not Greater or Equal
Jump if Greater or Equal (>=)
JGE, JNL S=O JNGE, JL
Jump if Not Less
Z=1
Jump if Less or Equal (<=)
JLE, JNG or JNLE, JG 18
Jump if Not Greater
S≠O
Jump Instructions for Unsigned Numbers
Instruction Description Condition Opposite

Jump if Equal (=)


JE, JZ Z=1 JNE, JNZ
Jump if Zero
Jump if Not Equal (≠)
JNE, JNZ Z=0 JE, JZ
Jump if Not Zero
C=0
Jump if Above (>)
JA, JNBE and JNA, JBE
Jump if Not Below or Equal
Z=O
C=1
Jump if Below or Equal (<=)
JBE, JNA or JNBE, JA
Jump if Not Above
Z=1
Jump if Below (<)
JNB, JAE,
JB, JNAE, JC Jump if Not Above or Equal C=1
JNC
Jump if Carry
19
Jump if Above or Equal (>=)
JAE, JNB, JNC Jump if Not Below C=0 JB, JNAE, JC
Jump if Not Carry
Signed or Unsigned?
• The numbers in a comparison may be signed or
unsigned, according to your selection.
• When signed numbers are compared, use the
instructions with the terms “greater than”, “less
than”, etc.
• When unsigned numbers are compared, use the
instructions with the terms “above” and
“below”.

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)

LOOPNE Decrease CX, jump to label if CX not zero LOOPE


and not equal (Z = 0)

LOOPNZ Decrease CX, jump to label if CX not zero LOOPZ


and Z = 0

LOOPZ Decrease CX, jump to label if CX not zero LOOPNZ


and Z = 1

JCXZ Jump to label if CX is zero OR CX, CX and JNZ 22


Loop Example: Assembly Code
org 100h
mov cx, 100 ; Number of elements in the blocks
mov bx, 0 ; Start index
L1:
mov ax, BLOCK1[bx] ; read next number from
BLOCK1
add ax, BLOCK2[bx] ; add next number from BLOCK2
mov BLOCK2[bx], ax ; store the result
add bx, 2 ; skip to next element
loop L1
Ret
BLOCK1 DW 100 DUP (1) 23
BLOCK2 DW 100 DUP (2)
Another Solution
org 100h
mov cx, 100 ; Number of elements in the blocks
mov SI, offset BLOCK1
mov DI, offset BLOCK2
L1:
mov ax, [SI] ; read next number from BLOCK1
add ax, [DI] ; add next number from BLOCK2
mov [DI], ax ; store the result
add SI, 2 ; skip to next element
add DI, 2
loop L1
Ret
BLOCK1 DW 100 DUP (1) 24
BLOCK2 DW 100 DUP (2)
Nested Loops
• Loops are basically the same jumps, it is possible to code loops
without using the loop instruction, by just using conditional
jumps and compare, and this is just what loop does.
• All loop instructions use CX register to count steps, as you
know CX register has 16 bits and the maximum value it can
hold is 65535 or FFFF
• However with some agility it is possible to put one loop into
another, and receive a nice value of 65535 * 65535 * 65535
....till infinity.... or the end of ram or stack memory.
• It is possible store original value of CX register using PUSH
CX instruction and return it to original when the internal loop
ends with POP CX 25
Nested Loop Example
ORG 100H PUSH CX
MOV BX, 0 ; total step counter. MOV CX, 5
MOV CX, 5 k3:
k1: ADD BX, 1
INC BX ; any other instructions
; any other instructions LOOP k3
PUSH CX POP CX
MOV CX, 5 LOOP k2 ; internal loop.
k2: POP CX
INC BX LOOP k1 ; external loop.
; any other instructions RET 26
Limitation
• JMP can jump to anywhere in the code segments
• But conditional jumps can only jump 127 bytes
forward and 128 bytes backward

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

• name - is the procedure name


• the same name should be in the top and the bottom, this
is used to check correct closing of procedures.

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

• In big endian, you store the most significant


byte in the smallest address. Here's how it
would look:

You might also like