LPU
ASSIGNMEN
T NO 2
COMPUTER
PERIPHERAL
SUBMITTED TO - SUBMITTED
BY-
[Link] ANKUR SINGH
E3801A29
BCA-MCA
PART A
Q1: Write a program using if-then-else statements :
If Temperature <30 : Light yellow lamp
If temperature >=30 and <40 : Light green lamp
If Temperature >=40 : Light red lamp
Ans:-
1 0000 code Segm
ent
2 Assume CS:
CODE
3
4 0000 BA FFFE MOV 0FFFEH
5 0003 BO 99 MOV 99H
6 0005 EE OUT AL
7
8 0006 BA FFF8 MOV 0FFF8
9 0009 EC IN DX
10 000A 3C 1E CMP 30
11 000C 72 03 JB YELLW
12 000E EB 0A 90 JMP GREEN
13 0011 BO 01 YELLO MOV 01H
W
14 0013 BA FFFA MOV 0FFFA
H
15 0016 EE OUT AL
16 0017 EB 07 90 JMP EXIT
17 001A BO BO 02 GREEN MOV 02H
18 001C BA BA MOV 0FFFA
FFFA H
19 001F EE EE OUT AL
20 0020 EB BA JMP OFFFC
FFFC H
21 0025 BO BO 02 RED MOV AL
22 0026 BA BA MOV OFFFC
FFFA H
23 001C EE EE OUT DX
24 0027 DX BA EXIT MOV OFFFC
FFFC H
25 0028 AL DX IN DX
26 0030 CODE END
27 END
Q2: Compute the average of 4 bytes stored in an array in
memory.
Ans 2
Code_Seg SEGMENT
Assume DS:data_seg,cs:code_seg
MOV AL,data_seg
MOV DS,AL
MOV AL, LOW_TEMP
ADD Hi_TEMP
MOV BL,02H
DIV BL
Code_Seg ENDS
Q3: How the near call procedures and far call procedures are
different from each other.
Ans 3 The 80x86 supports near and far subroutines. Near calls and returns transfer control
between procedures in the same code segment. Far calls and returns pass control between different
segments. The two calling and return mechanisms push and pop different return addresses
Use the near ptr and far ptr operators to override the automatic assignment of a near or far
call. If NearLbl is a near label and FarLbl is a far label then the following call instructions
generate a near and far call respectively:
call NearLbl ;Generates a NEAR call.
call FarLbl ;Generates a FAR call.
Suppose you need to make a far call to NearLbl or a near call to FarLbl. You can accomplish this
using the following instructions:
call far ptr NearLbl ;Generates a FAR call.
call near ptr FarLbl ;Generates a NEAR call.
Calling a near procedure using a far call or calling a far procedure using a near call isn't something
you'll normally do. If you call a near procedure using a far call instruction the near return will leave the
cs value on the stack. Generally rather than:
call far ptr NearProc
you should probably use the clearer code:
push cs
call NearProc
Calling a far procedure with a near call is a very dangerous operation. If you attempt such a call the
current cs value must be on the stack. Remember a far ret pops a segmented return address off the
stack. A near call instruction only pushes the offset not the segment portion of the return address.
PART B
Q4: Write a program to push and pop some data using assembly
language.
Ans 4
Push — Push stack (Opcodes: FF, 89, 8A, 8B, 8C, 8E, ...)
The push instruction places its operand onto the top of the hardware supported
stack in memory. Specifically, push first decrements ESP by 4, then places its
operand into the contents of the 32-bit location at address [ESP]. ESP (the stack
pointer) is decremented by push since the x86 stack grows down - i.e. the stack
grows from high addresses to lower addresses.
Syntax
push <reg32>
push <mem>
push <con32>
Examples
push eax — push eax on the stack
push [var] — push the 4 bytes at address var onto the stack
pop — Pop stack
The pop instruction removes the 4-byte data element from the top of the
hardware-supported stack into the specified operand (i.e. register or memory
location). It first moves the 4 bytes located at memory location [SP] into the
specified register or memory location, and then increments SP by 4.
Syntax
pop <reg32>
pop <mem>
Examples
pop edi — pop the top element of the stack into EDI.
pop [ebx] — pop the top element of the stack into memory at the four bytes
starting at location EBX.
Q5: Write the syntax and example of the following instructions
a) CALL b) CMP c)INC d)INT e)JNP/JPO
Ans5
CALL:- Subroutine call and return
These instructions implement a subroutine call and return. The call instruction
first pushes the current code location onto the hardware supported stack in
memory (see the push instruction for details), and then performs an
unconditional jump to the code location indicated by the label operand. Unlike the
simple jump instructions, the call instruction saves the location to return to when
the subroutine completes.
The ret instruction implements a subroutine return mechanism. This instruction
first pops a code location off the hardware supported in-memory stack (see the
pop instruction for details). It then performs an unconditional jump to the
retrieved code location.
Syntax
call <label>
ret
CMP:-Compare
Compare the values of the two specified operands, setting the condition codes in
the machine status word appropriately. This instruction is equivalent to the sub
instruction, except the result of the subtraction is discarded instead of replacing
the first operand.
Syntax
cmp <reg>,<reg>
cmp <reg>,<mem>
cmp <mem>,<reg>
cmp <reg>,<con>
Example
cmp DWORD PTR [var], 10
jeq loop
If the 4 bytes stored at location var are equal to the 4-byte integer constant 10,
jump to the location labeled loop.
INC:- Increment
The inc instruction increments the contents of its operand by one.
Syntax
inc <reg>
inc <mem>
Examples
inc DWORD PTR [var] — add one to the 32-bit integer stored at location var
INT:- Some examples are:
int 21h ; calls DOS service
int 10h ; calls the Video BIOS interrupt
Most interrupts have more than one function, this means that you have to pass a
number to the function you want. This is usually put in AH. To print a message on
the screen all you need to do is this:
mov ah,9 ; subroutine number 9
int 21h ; call the interrupt
First you have to specify what to print. This function needs DS:DX to be a far
pointer to where the string is. The string has to be terminated with a dollar sign
($). This would be easy if DS could be manipulated directly, to get round this we
have to use AX. Later one we will build a program that is printing a string on the
screen.
JNP/JPO:- Purpose: Conditional jump, and the state of the flags is taken into
account.
Syntax:
JNP label
It jumps if there is no parity or if the parity is uneven.
The jump is done if PF = 0.
Q6: What are the benefits of using macros in microprocessor
and give an example to show the working of the macro.
Ans 6
A macro is a grow of repetitive instructions in a program which are
codified only once and can be used as many times as necessary.
The main difference between a macro and a procedure is that in the macro
the passage of parameters is possible and in the procedure it is not, this
is only applicable for the TASM - there are other programming languages
which do allow it. At the moment the macro is executed each parameter is
substituted by the name or value specified at the time of the call.
We can say then that a procedure is an extension of a determined program,
while the macro is a module with specific functions which can be used by
different programs.
Another difference between a macro and a procedure is the way of calling
each one, to call a procedure the use of a directive is required, on the
other hand the call of macros is done as if it were an assembler
instruction.
Syntax of a Macro
The parts which make a macro are:
Declaration of the macro
Code of the macro
Macro termination directive
The declaration of the macro is done the following way:
NameMacro MACRO [parameter1, parameter2...]
Even though we have the functionality of the parameters it is possible to
create a macro which does not need them.
The directive for the termination of the macro is: ENDM
An example of a macro, to place the cursor on a determined position on the
screen is:
Position MACRO Row, Column
PUSH AX
PUSH BX
PUSH DX
MOV AH, 02H
MOV DH, Row
MOV DL, Column
MOV BH, 0
INT 10H
POP DX
POP BX
POP AX
ENDM
To use a macro it is only necessary to call it by its name, as if it were
another assembler instruction, since directives are no longer necessary as
in the case of the procedures. Example:
Position 8, 6