You are on page 1of 21

UNIT 3

1. Explain steps to convert an algorithm to assembly language

The main steps involved in converting an algorithm to assembly language are

I. Defining the problem and writing the algorithm

II. Setting up the data structure

III. Initialization checklist

IV. Choosing instructions to implement algorithm

i. Defining the problem and writing the algorithm

1. Here we define the problem in general and write the algorithm. The
algorithm at the point should be general enough that it could be implemented in
any programming language or on any machine

2. Once we are sure of our algorithm, then we can start thinking of


architecture and instructions of the specific microcomputer

ii. Setting up the data structure

1. The primary step in converting an algorithm to assembly language is to


set up the data structure that the algorithm will be working with.

While setting up the data structure the following question should be considered

• Will data be in memory or in registers?

• Is the data of type byte, type word, or perhaps type double word?

• How many data items are there?

CSE BITM, BELLARY 1


• Does the data represent only positive numbers, or does it represent
positive and negative (signed) numbers?

• For more complex problems, you might ask how the data is structured

For ex: is the data in an array or in a record?

For ex: with respect to above points the data segment and the data are
initialized.

DATA SEGMENT

N1 db 28h

N2 db 24h

Sum db dup (?)

DATA ENDS

Here the data segment is initialized using assembler directive SEGMENT preceded by segment
name ‘DATA’ and the variable n1 and n2 are declared as byte type. To store the results, the byte
locations are reserved with the symbolic name ‘Sum’. The ENDS assembler directive preceded
by the logical segment name is used to end the data segment.

Next step is to start the code segment. Any instruction required initializing variables, segment
registers, peripheral devices are done here.

iii. Initialization checklist

1. The data segment address cannot be taken directly into segment registers.
Therefore it should be taken into any general purpose registers and then
transferred to data segment register.

2. In the beginning of the code segment the first two statements are

CSE BITM, BELLARY 2


Mov ax, @data

Mov ds, ax

3. If the program is with respect to i/o communication then the port addresses
are to be initialized

iv. Choosing the instructions to implement the algorithm

1. Determine the instructions required to implement each of the major


actions in the algorithm and decide how the data must be positioned for these
instructions.

Ex: for the data declared in the data segment if addition is to be performed then
add instruction is to be used. This can be shown as follows

Add al,n2

Mov sum,al

Jnc down1

Mov [sum+1],01

Down1 : int 3h

end

• First data is taken into al register. The second data is added with al. The
sum which is in al is stored in symbolic memory location sum and if there is a
carry, 01 is stored in [sum+1] else 00 is stored.

• Finally insert the mov or other instructions, required to get the data into
the correct position with these instructions.

Mov sum,al

CSE BITM, BELLARY 3


Mov [sum+1],01

2. What are the three basic structures types used to write the algorithm for a program and
describe different standard program structures?

The three basic structures types used to write the algorithm are as follows:

a) SEQUENCE

b) IF-THEN-ELSE

c) WHILE-DO

• The other useful structures such as IF-THEN, REPEAT-UNTIL and CASE can be
derived from the above basic structure.

• Each structure has only one entry point and only one exit point.

• Any structure can be used with in another.

a) SIMPLE SEQUENCE: The format for this structure is

FLOWCHART PSEUDOCODE EXAMPLE


STATEMENT 1 Get data sample
STATEMENT 2 Add 7
STATEMENT 3 Store in memory location

• In this structure the action are simply written in desired order, as shown in the above
example.

CSE BITM, BELLARY 4


• As shown in the above example, we are just reading the data and adding 7 to the read
data and store this data into the memory location. Here we are not going to check any
condition.

b) IF-THEN-ELSE: The format for this structure is

FLOWCHART: PSEUDOCODE

IF CONDITION THEN
` STATEMENT(S) 1
Cond ELSE
STATEMENT(S) 2
EXAMPLE:
If temp <700 then
Turn on heater
else
Turn off heater

• This structure is used for direct operation on one of two different actions based on
same condition.
• Here one series of actions or another series of actions is done before the program
goes on with the next main line instructions.

• In the above example if the temperature is less than 700 then the heater will turn
ON and if the temperature is greater than or equal to 700 then the heater will turn
OFF, either one of the operation is performed.

• This structure can be implemented by using jump instruction in assembly


language programs.

For ex: Mov dx, port_address

CSE BITM, BELLARY 5


In al, dx if temperature <300 then

Cmp al, 30 light yellow lamp


Jb yellow else
Jmp green light green lamp

IF-THEN: The format of this structure is

FLOW CHART PSEUDO CODE


If CONDITION then
Yes
Condit STATEMENT (s)
ion
?

Statement
No (s)

• If the stated condition is found to be true, then series of actions following THEN
will be executed.

• If the condition is false, execution will skip over the actions after THEN and
proceed with the next main line instructions.

• This structure can be implemented as shown in the below example

For ex: cmp ax, bx If A equal to B THEN

Je there C equal 07h

Add ax, 0002h A equal A+2h

There: mov cl, 07h

MULTIPLE IF-THEN-ELSE:

• This structure is used to select one of the several actions based on some
conditions.

CSE BITM, BELLARY 6


• The last ELSE is the part of the IF-THEN just before it.

• This structure chooses b/w two alternative courses of action.

• The structure can be implemented as follows:

For ex: cmp al,30

Jb yellow If temp <300 THEN

Cmp al,40 light yellow lamp

Jb green ELSE If temp<400 THEN

Red: mov al,04h light green lamp

Out dx,al ELSE

CSE BITM, BELLARY 7


Jmp exit light red lamp

C) WHILE-DO:

• This structure is one form of repetition.

• It is used to indicate some action or sequence of action as long as some condition is true.

• This structure represents a program loop.

• In this structure, the condition is checked before the action is done for the first time.

• The format of this structure is as follows:

FLOW CHART PSEUDO CODE


NO
WHILE CONDITION DO
Cond

STATEMENT(s)

For ex:

TEMP_IN: mov dx, i/p port_address


in al,dx while temp<1000 do
cmp ax,100 turn heater on
jae heater_off read temp

mov al,80h turn heater off


mov dx, o/p port_address
out dx,al
jmp temp_in

CSE BITM, BELLARY 8


REPEAT UNTIL:

• This structure is used to indicate that to repeat some actions or series of actions until
some condition is satisfied.

• In this structure, the action(s) is done once before the condition is checked.

• This structure contains a simple IF-THEN-ELSE decision operation.

• As shown in the above example, the loop will continue until 24 samples were taken and
stored in the memory.

• The format of this structure is as follows:

Flowchart Psuedocode

REPEAT

STATEMENT(S)

UNTIL CONDITION

Example
Cond
repeat

Get data sample

add 7

Store result in memory

Wait 1 hr

Until 24 samples are taken

• This structure is implemented as follows:


LOOK_AGAIN: in al,dx Repeat
and al,01 Mask upper 4 bits of al
jz look_again UNTIL strobe is low

CSE BITM, BELLARY 9


mov dx,0fff8h Read ASCII code
in al,dx

3. Explain how to Debug Assembly Language Programs


Certain steps have to follow in order to debug an assembly language programs they are:
1. Very carefully define the problem you are trying to solve with the program and work out
the best algorithm you can.
2. Write and test each section of a program as you go, instead of writing a large program all
at once.
3. If a program or program section does not work, first recheck the algorithm to make sure it
really does what you want it to.
4. If the algorithm seems correct, check to make sure that you have used the correct
instructions to implement the algorithm.
5. If you are hand coding your programs, this is the next place to check. It is very easy to
get a bit wrong when you construct the 8086 instruction codes. When constructing instruction
codes which contain addresses or displacement, that the low byte of the address or
displacement is coded in before the high byte.
6. If you don't find a problem in the algorithm, instructions, or coding, now is the time to
use debugger, monitor, or emulator tools to help you localize the problem. Using debugger,
monitor and emulator tools, in the SDK-86 board single step command can be used to
execute one instruction at a time
• When a single step command is used one instruction at a time is executed and
stopped.
• Now you can use examine register and examine memory command to see if
register and memory contains the correct data.
• If results are correct proceed to next instruction (or) else you keep stepping
through the program until you reach a point where the results are not what you
predicted they should be at the point. Once you have localized the problem to one
or two instructions, it is usually not too hard to find the error.
7. If the programs are too long, the single step approach can be somewhat tedious then

CSE BITM, BELLARY 10


breakpoints approach is often faster to narrow down the source problem to a small region.
• Most debuggers, monitors, and emulators allow to specify both a starting
address and ending address in their GO command.
• The GO command format is GO address, breakpoint address.
• When this command is executed the execution starts from the address
specified and steps when it reaches the address specified as breakpoint address.
• Once the execution stops we can use examine register and examine
memory command to check result at that point.
• If the results are correct, then go to next instructions, if not debug (or)
correct the instruction and then go for the next instruction.

5. What are jump instructions? Explain types of jump instructions.


Jump instructions are used to tell the computer the address to fetch its next instructions
from some place in memory other than the next sequential location.
8086 has two types of jump instructions. They are
1. Conditional
2. Un-conditional.
Conditional: 8086 fetches and decodes a conditional jump instruction. It evaluates the state of a
specified flag to determine whether to fetch its next instruction from the jump destination
location (or) to fetch its next instruction from the next sequential memory location.
1. The 8086 conditional jump instruction look at the state of a specified flag(s) to determine
whether the jump should be made or not. There are six conditional flags in 8086 indicate
the conditions that are present after an instruction.
2. All conditional jumps are short -type jumps. This means that destination label must be in
the same code segment as the jump instruction.
3. The destination address must be in the range of -128 bytes to +127 bytes from the address
of the instruction after the jump instruction.
4. Conditional jump instructions are usually used after arithmetic or logic instructions. They
are commonly used after compare instructions.
Example: cmp bl,dh
i. JAE heater-off

CSE BITM, BELLARY 11


Here cmp instruction compares the style in dh and bl and sets flags according to the
result.
Here JAE instruction says, “jump if above (or) equal”: i.e.
If CF=0 and ZF=1 then it takes a branch to heater-off else it goes to the next instruction to jump.
Un-conditional:
1. When 8086 fetches and decodes an un-conditional jump instruction, it always goes to the
specified jump destination.
2. We might use this type of jump at the end of a program so that the entire program runs
over and over.
3. JMP instruction always causes a jump to occur, so this is referred as an un-conditional
jump.
4. When JMP instruction gets executed, it loads a new number into the IP and in some cases
new value in CS registers.
5. If it loads only IP with new value then that type of JMP is referred as near (or) into a
segment jump.
6. If it loads both IP and CS with new value then that type of JMP is referred as far or
intersegment jump.

Un-conditional Jump Instruction types:


Direct near and short JMP (or) JMP- Intrasegment Short Jump Instruction
1. A near type jump instruction can cause the next instruction to be fetched from anywhere
in the current code segment.
2. A positive displacement usually means you are jumping ahead in the program, if negative
displacement then jumping backward in the program and displacement range is 127 to
-128 bytes.
3. Opcode for this instruction is EBH.
Example:
BACK: sub Bx, Ax
…..
…..
JMP BACK

CSE BITM, BELLARY 12


NEXT: Mov Ax, Cx

here offset address of NEXT-offset address of BACK<=128 then while translating JMP BACK,
the assembler can easily calculate the required displacement, as the assembler has earlier come
across the label BACK. Here JMP BACK is translated as EBH.
JMP- Intrasegment Relative Jump with 2 byte Displacement
1. It is a 3 byte instruction, here 1st byte provides the Opcode and the next 2 byte provides
the 16 bit displacement in 2's compliment notation.
2. Opcode for this instruction is E9H.
3. Memory location specified by the label in the JMP instruction can be in the range -32768
to 32767 bytes from beginning of the next instruction after JMP.
4. If we require a jump beyond 128 bytes, then the jump instruction is translated as E9H,
followed by 2 byte displacement.
5. Template looks as:

Example:
REPEAT: JC NEXT



JMP REPEAT

Assume that here we are branching outside the permitted range of -128 to 127 byte, then we
have to use this type of JMP instruction.
JMP- Intrasegment Indirect Jump (or) Near Indirect Jump
1. The mnemonic for the instruction is JMP. The template for the instruction as follow.
2. This form of jump instruction is used to select one of several jump destinations based on
a computed value.
3. Here W bit is absent in the template since the jump address is given as contents of a word
register or a word Location in memory.
Example: JMP Dx

CSE BITM, BELLARY 13


Suppose Dx content is 1234H, then this statement results in an un-conditional branch to offset
location 1234H.

6. Write the steps and explain how to convert two ASCII characters to a packed BCD and
write a program?

Following steps are involved in converting two ASCII characters to a packed BCD

i. Convert first ASCII number to unpacked BCD

ii. Convert second ASCII number to unpacked BCD

iii. Move first BCD nibble to upper nibble position in byte

iv. Pack two BCD nibbles in one byte.

• Numbers represented as one BCD digit per byte are called


unpacked BCD

• For mathematical operations on the BCD numbers, we combine


two BCD digits in a single byte this form is called packed BCD.

Convert a number in ASCII form to its unpacked BCD equivalent

i. This is done by replacing the upper 4 bits of the ASCII byte with four
zeros

ii. To do this in 8086 we use AND instruction

iii. ANDing a bit with a zero is called masking because previous state of the
bit is hidden (or) masked.

iv. To do so we should mask upper 4 bits with 0 and lower 4 bits with 1 and
the result is the number in unpacked form

Moving a nibble with the ROTATE instruction

CSE BITM, BELLARY 14


i. Move the 4 BCD bits in the first unpacked BCD byte to the upper nibble
position in the byte

ii. We need to do, so that the 4 BCD bits are in the correct position for packing
with second BCD nibble.

7. Write a delay loop which produces a delay of 500µs on an 8086 with 5MHZ clock

To write delay loop, first we have to calculate the total number of T-states, it is given by

T-state (1 clock cycle)

Mov cx, count(N) 4


Up: dec cx 2
Nop 3
Jnz up 16/4
Ret 8
Now total no of T states (N) = 4×1+2×N+3×N+16× (N-1)+4×1+8×1
= 16+5N+16(N-1)

= 16+5N+16N-16

Total no of T states = 21N

We know Time delay=Total no. of T states × clock period

Clock period=

Time period=

Now time delay for 500µs

N=500×10-6×5×10-6/21

N=119

Now the value of N in hexadecimal is =77h

The delay loop for 500µs is

CSE BITM, BELLARY 15


Mov cx, 77h
Up: dec cx
Nop
Jnz up
Ret

8. Write a delay loop which creates a maximum delay using 2-registers on an 8086 with
5MHZ frequency, and find the max delay?
Delay proc No of T-states

Mov cx , count 1(0ffffh) 4


Up1: mov dx , count2(0ffffh) 4
Up2: decd x 2
Nop 3
Jnz up2 16/4
Dec cx 2
Jnz up1 16/4
Ret 8
Delay endp

Now to calculate total no of T-states

First we calculate no of T-states for the loop

T1=4×1+2(N1) + 3(N1)+!^(N-1)+4×1

=21N1-16+8

T1=21N1-8

Now we calculate total no of T-states

T=4×1+ (T1)N2=2N2+16(N2-1)+4×1+8×1

We know T! =21N1-8

T=4×1+ (21N2-8) N2+2N2+16(N2-1)+4×1

T=4+21N1N2-8N2+2N2+16N2-16+12

CSE BITM, BELLARY 16


T=21N1N2+10N2 …… (1)

We know time delay =total no of T-states×clock period

But clock period=

Time delay=

Here in the given example to calculate max delay

i.e.; count1=0ffffh and count2=0fffffh

i.e; N1=65535 and N2=65535

Substitute in (1)

T=21(65535) (65535)+10(65535)

T=9.01915×1010+655350

T=9.019221608×1010

Time delay=9.019221608×1010
5×106

=18.03 ms

9. Explain some of the conditional jumps both based on single flag condition and more than
one flag.
Ans: CONDITIONAL JUMPS BASED ON A SINGLE FLAG

The instructions which perform a branch on the value of a single status flag are:

1) JE - jump if equal (or) JZ - jump if zero

JE/JZ is a conditional jump instruction on which only zero flag condition is


checked.

CSE BITM, BELLARY 17


The branch takes if ZF=1 else the next instruction to jump is executed.

2) JNE - jump if not equal (or) JNZ - jump if not zero

JNE/JNZ is a conditional jump instruction. In which only zero flag condition is checked.

The branch takes if ZF=0, else the next instruction to jump is executed.

3) JS - jump on sign

JS is a conditional jump instruction. In which only sign flag condition is checked.

The branch takes if SF=1, else the next instruction is executed.

4) JNS - jump on no sign

JNS is a conditional jump instruction. In which only sign flag condition is checked.

The branch takes if SF=0, else the next instruction is executed.

5) JO - jump on overflow

JO is a conditional jump instruction in which only overflow flag condition is


checked.

Branch takes if OF=1, else next instruction to jump is executed.

6) JNO - jump on no overflow

JNO is a conditional jump instruction in which only overflow flag condition is


checked.

CSE BITM, BELLARY 18


Branch takes if OF=0, else next instruction to jump is executed.

7) JB/JNAE/JC - jump below/ jump on not above or equal/ jump if carry

JB/JNAE/JC is a conditional jump instruction in which only carry flag is checked.

Branch takes if CF=1, else next instruction to jump is executed.

8) JNC/JAE/JNB - jump if not carry/ jump above or equal/ jump if not below

JNC/JAE/JNB is a conditional jump instruction in which only carry flag is


checked.

Branch takes if CF=0, else next instruction to jump is executed.

9) JP/JPE - jump if parity/ jump if parity even

JP/JPE is a conditional jump instruction in which only parity flag condition is


checked.

Branch takes if PF=1, else next instruction to jump is executed.

10) JNP/JPO-jump if not parity/jump if parity odd.

JNP/JPO is a conditional jump instruction in which only parity flag condition is checked.
Branch takes if PF=0, else next instruction to jump are executed.

CONDITIONAL JUMPS BASED ON MORE THAN ONE FLAG

1) JBE/JNA- Jump if below or equal/jump if not above

CSE BITM, BELLARY 19


JBE/JNA is a conditional jump instruction, on which carry flag and zero flag conditions
are checked

It takes branch if either CF or ZF=1, else next instruction to jump is executed.

2) JNBE/JA- jump if not below or equal/jump if above

JNBE/JA is a conditional jump instruction, in which CF or ZF are checked.

It takes branch if CF or ZF=0, else instruction to jump is executed.

3) JLE/JNG- jump if less or equal/ jump if not greater

JLE/JNG is a conditional jump instruction in which SF, OF and ZF conditions are


checked.

It takes branch if ((SF XOR OF) or ZF) =1, else next instruction to jump is executed.

4) JNLE/JG- jump if not less or equal/ jump if greater

JNLE/JG is a conditional jump instruction, in which SF, OF and ZF conditions are


checked.

It takes branch if ((SF XOR OF) or ZF) = 0 else next instructions to jump is executed.

5) JL/JNGE- jump if less/ jump if not greater or equal

JL/JNGE is a conditional jump instruction, in which SF and OF conditions are checked.

It takes branch if (SF XOR OF) =1 else next instruction to jump is executed.

CSE BITM, BELLARY 20


6) JNL/JGE- jump if not less/ jump if greater or equal

JNL/JGE is a conditional jump instruction, in which SF and OF condition is checked.

It takes branch if (SF XOR OF) =0 else next Instruction to jump is executed.

CSE BITM, BELLARY 21

You might also like