Professional Documents
Culture Documents
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
While setting up the data structure the following question should be considered
• Is the data of type byte, type word, or perhaps type double word?
• For more complex problems, you might ask how the data is structured
For ex: with respect to above points the data segment and the data are
initialized.
DATA SEGMENT
N1 db 28h
N2 db 24h
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.
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
Mov ds, ax
3. If the program is with respect to i/o communication then the port addresses
are to be initialized
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
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.
• In this structure the action are simply written in desired order, as shown in the above
example.
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.
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.
MULTIPLE IF-THEN-ELSE:
• This structure is used to select one of the several actions based on some
conditions.
C) WHILE-DO:
• It is used to indicate some action or sequence of action as long as some condition is true.
• In this structure, the condition is checked before the action is done for the first time.
STATEMENT(s)
For ex:
• 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.
• As shown in the above example, the loop will continue until 24 samples were taken and
stored in the memory.
Flowchart Psuedocode
REPEAT
STATEMENT(S)
UNTIL CONDITION
Example
Cond
repeat
add 7
Wait 1 hr
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
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. This is done by replacing the upper 4 bits of the ASCII byte with four
zeros
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
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
= 16+5N+16N-16
Clock period=
Time period=
N=500×10-6×5×10-6/21
N=119
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
T1=4×1+2(N1) + 3(N1)+!^(N-1)+4×1
=21N1-16+8
T1=21N1-8
T=4×1+ (T1)N2=2N2+16(N2-1)+4×1+8×1
We know T! =21N1-8
T=4+21N1N2-8N2+2N2+16N2-16+12
Time delay=
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:
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
JNS is a conditional jump instruction. In which only sign flag condition is checked.
5) JO - jump on overflow
8) JNC/JAE/JNB - jump if not carry/ jump above or equal/ jump if not below
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.
It takes branch if ((SF XOR OF) or ZF) =1, else next instruction to jump is executed.
It takes branch if ((SF XOR OF) or ZF) = 0 else next instructions to jump is executed.
It takes branch if (SF XOR OF) =1 else next instruction to jump is executed.
It takes branch if (SF XOR OF) =0 else next Instruction to jump is executed.