Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Assembly language

1. A system is designed to monitor the temperature of a furnace.


Temperature readings are recorded in 16 bits and stored in memory
locations starting at XX60H. The high-order byte is stored first and the
low-order byte is stored in the next consecutive memory location.
However, the high-order Byte of all the temperature readings is
constant. Write a program to transfer the low-order readings to
consecutive memory locations starting at XX80H and discard the high-
order bytes. Temperature Readings (H) 0581, 0595, 0578, 057A, 0598.

Solution:
PROGRAM:
START: LXI H, 2061 H ; HL = 2060 H : Point to Source address .
LXI D, 2080 H ; DE = 2080 H: Destination address.
MVI B, 05 H ; Counter for 5 temperature readings
NEXT: MOV A,M ; Get Reading: Load reading from source address HL to A
STAX D ; Store A at Destination Address
INX H ; Go to Next Source Address
INX H ;Go to next source address (Skip High Order of next Temp
Reading as given in question that it remains constant)
INX D ; Go to next Destination Address
DCR B ;Decrement Counter
JNZ NEXT ;If counter not zero Jump to NEXT and transfer the values
again.
HLT

Detailed Explanation:

 The source address, where readings are stored starts at XX60H, Let us assume
XX=20. We get source address 2060H. Now, we use LXI H, 2061H to point to
source address, HL register Pair.
 Now, these temperature values need to be transferred to memory location
XX80H. We use LXI D, 2080H to point to destination address , DE pair.
 We have 5 temperature values, therefore we need to transfer values 5 times. To
count this we use MVI B, 05 H to set a counter at register B with value 5.
Inside NEXT:
 Pick up value from source address i.e HL register pair and store it in A. We use
MOV A, M. to carry out this task.
 Value stored at A, need to be transferred to Destination address at DL register
pair. Use STAX D to store value from A to DL register Pair.
 Move to next source address to get next value. Increment the memory address
using INC H.
 Increment again, INC H to skip High order as given in question that it is constant.
 Increment the destination memory location to store value at next memory location
using INC D.
 Now, one value has been transferred, Decrement the counter by 1 using DCR B.
 If the counter value is not zero it means there are more values that need to be
transferred therefore, if NOT ZERO JUMP to NEXT. JNZ NEXT and repeat the
above steps inside NEXT to transfer rest of the temperature values.
 When Counter B becomes zero, it means all the values have been transferred; the
program will not jump to NEXT and will stop, HLT.

================================================================

2. The temperatures of two furnaces are being monitored by a


microcomputer. A set of five readings of the first furnace, recorded by
five thermal sensors, is stored at the memory location starting at
XX50H. A corresponding set of five readings from the second furnace is
stored at the memory location starting at XX60H. Each reading from
the first set is expected to be higher than the corresponding reading
from the second set. For example, the temperature reading at the
location 54H (T54) is expected to be higher than the temperature reading
at the location 64H (T64). Write a program to check whether each
reading from the first set is higher than the corresponding reading from
the second set. If all readings from the first set are higher than the
corresponding readings from the second set, turn on the bit D0 of the
output PORT1. If any one of the readings from the first set is lower than
the corresponding reading of the second set, stop the process and output
FF as an emergency signal to the output PORT1.

SOLUTION:
PROGRAM:
START: LXI D, 2050 H ; Pointer for readings of Furnace 1
LXI H, 2060 H ; Pointer for readings of furnace 2
MVI C, 05 H ; Counter for 5 readings
NEXT: LDAX D ; Storing reading from furnace 1 to A
MOV B,M ; Storing reading from furnace 2 in B
SUB B ; Subtract B from A,
JC SHUTDOWN ; If there is carry from (A-B) it means(B > A) i.e.
temperature in furnace 2 > temperature in furnace 1,
Go to shutdown and display emergency.
INX D ; Go to next address for furnace 1
INX H ;Go to next address for furnace 2
DCR C ;Decrement Counter
JNZ NEXT ;If counter is not zero, go to next and take readings
again
MVI A, 01H ; Set Bit D0=1
OUT PORT 1 ;Turn on Bit D0 and display content of A on Port1
HLT
SHUTDOW MVI A, 0FFH ;Load emergency indicator
N
OUT PORT 1 ;Display content of A (Here, Emergency) on PORT1
HLT

Detailed Explanation:
The question statement wants to compare two sets of readings stored at different
value. If the particular value of set 1 is greater than corresponding value of set 2
then D0 bit of PORT1 has to be turned on otherwise, emergency signal FF has to
be displayed at port1.
Now let us take look at steps of the program.
 Given furnace 1 readings are stored at memory address XX50H and
furnace two readings are stored at memory address XX60H. Let XX=20
We use LXI D, 2050H, to point to set of readings of Furnace 1
LXI H, 2060H, to point to set of readings of Furnace 2.
 Given, there are 5 readings therefore to compare the readings 5 times we
set up counter at C with value 05H using MVI C,05H.
Inside NEXT:
 LDAX D, loads the accumulator with value from register pair DL, here it
is storing temperature values of Furnace 1. Therefore, LDAX D, loads the
value of temperature of furnace 1 in accumulator.
 MOV B, M moves the value of HL pair to register B. Note, HL pair is
storing values of set of furnace 2. Therefore this instruction loads the B
with temperature value of Furnace 2.
 SUB B, To compare the values of furnace 1 and furnace 2, we subtracted
B from A i.e. Value of furnace 2 from furnace 1. If A>B then no carry will
be generated but if B>A then carry will be generated.
 If carry is generated it means B>A. i.e. Temperature value of Furnace 2 is
greater than Furnace 1. Now according to question if this happens the
process needs to stop and emergency signal should be generated at
PORT1. Therefore we use, JC SHUTDOWN. Which will jump to
SHUTDOWN section if the carry is generated.
 If A>B, it means no carry is generated and temperature value at furnace 1
> furnace 2. We will move on to check the next values. Therefore, we will
increment the memory locations.

Using INC D and INC H. Note: INC D will increment the DE pair(Furnace
1) and INC H will increment HL pair (Furnace 2).

 DCR C, will decrement the counter.


 If After decrementing counter the result is not zero, it means there are more
values to be compared and JNZ NEXT, will take the execution inside NEXT
once more to compare the remaining values.
 If after decrementing the result is zero it means all the 5 values have been
compared and all the for all the values temperature at Furnace 1 > Furnace 2.
Therefore, according to the question,Bit D0 is set. MVI A, 01H to set value
1.
 OUT PORT1, to set Bit D0 on by outputting value 01H from A to PORT1.
 Stop the execution using HLT.
Inside SHUTDOWN
 This is executed if at any point in execution B>A.
 Emergency signal FFH is loaded to A using MVI A, 0FFH.
 The signal is output to port1 using OUT PORT1.
 The execution is stopped using HLT.

3. Specify the contents of the accumulator and the status of the Cy flag
when the following instructions are executed
a. MVI A,B7H
ORA A
RLC

SOLUTION:

Accumulator (A): 6FH


Carry Flag (CY): 1

Explanation:
 MVI A, B7H:
Moves the value B7H to the accumulator.
 ORA A:
OR the value of A with A.
We have B7 at A:
B7 in binary : 10110111
A OR A: 10110111 OR 10110111 : 10110111
 RLC:
Rotate left accumulator, This mnemonic rotates the value stored at
accumulator to the left by 1 bit. Carry flag is not involved in rotation.
We have:
A : 10110111
Rotating each bit to left:
01101111
In Hexa: 01101111 -> 6FH
Carry flag is set.
b. MVI A,B7H
ORA A
RAL

SOLUTION:
Accumulator (A): 6EH
Carry Flag (CY): 1

Explanation:
 MVI A, B7H:
Moves the value B7H to the accumulator.
 ORA A:
OR the value of A with A.
We have B7 at A:
B7 in binary : 10110111
A OR A: 10110111 OR 10110111 : 10110111
 RAL:
Rotate accumulator left , This mnemonic rotates the value stored at
accumulator to the left by 1 bit. Carry flag is involved in rotation.
We have:
A : 10110111
Rotating each bit to left:
CY-1 01101110
In Hexa: 01101110 -> 6EH
Carry flag is set.

4. Calculate the decimal value of the number in the accumulator before


and after the Rotate instructions are executed, and explain the
mathematical functions performed by the instructions
a. MVI A,18H
RLC

SOLUTION:
Before rotate instruction: 24
After Rotate Instruction: 48
Mathematical Function: Multiply by 2

Explanation:
 MVI A, 18H:
Moves value 18H to A
18H in Binary: 00011000
00011000 in Decimal: 24
 RLC:
Rotates the accumulator value to left by 1 bit. Without involving
carry.
Rotate 00011000 to left by 1 bit:
00110000
00110000 in Decimal: 48
 From above we can deduce that after RLC the value in the
accumulator is multiplied by 2.

b. MVI A,78H
RRC
RRC

SOLUTION:
Before rotate instruction: 120
After Rotate Instruction: 30
Mathematical Function: Divide By 4

Explanation:
 MVI A, 78H:
Moves value 78H to A
78H in Binary: 01111000
01111000 in Decimal: 120
 RRC:
Rotates Righ Accumulator, It rotates the accumulator value to right by
1 bit. Involving carry.
Rotate 01111000 to right by 1 bit:
00111100
00111100 in Decimal: 60
 RRC:
It again rotates the bit value of A to right by 1 bit.
Rotating 00111100 to right by 1 bit:
00011110
00011110 in Decimal: 30
 From above we can deduce that two RRC instructions divided the
original value by 4.
5. Explain the mathematical function that is performed by the following
instructions
MVI A,07H
RLC
MOV B,A
RLC
RLC
ADD B

SOLUTION:

Multiply By 10
Explanation:
 MVI A, 07H:
Moves the value 07H to the Accumulator.
07H in Binary: 0000 0111
In decimal: 7
 RLC:
Rotates the bit value of A to left by 1 bit position by not involving
carry.
Rotates 0000 0111 to right by 1 bit:
0000 1110
 MOV B, A:
Moves value of A to B:
B← A
B← 0EH : 0000 1110
 RLC:
Rotates the bit value of A to left by 1 bit position.
Rotates 0001 1100 to left by 1 bit:
0001 1100
 RLC:
Again rotates the bit value of A to left by 1 bit position.
Rotates 0001 1100 to left by 1 bit:
0011 1000
 ADD B:
Add B to the content of accumulator.
A: 0011 1000
B: 0000 1110

0011 1000
+ 0000 1110
0100 0110
 In A: 0100 0110
In decimal: 70
 Therefore, It can be observed that given set of instructions
multiplied the decimal value in the beginning by 10.

6. A set of eight readings is stored in memory starting at location XX50H.


Write a program to check whether a byte of 40H exists in the set. If it
does, stop checking and display its memory location; otherwise output
FFH. DATA(H) 48, 32, F2, 38, 37, 40, 82, 8A

SOLUTION:

START: LXI H, 2050H ; Pointer to Address of data


MVI D, 40H ; Store Byte to be found at D
MVI C,08H ;Counter to traverse through 8 values
NEXT: MOV A,M ;Get byte data and store at A
CMP D ;Check if Byte at A = D (Is byte at A = 40H)
JZ DISPLAY ;If A = 40H, The result will be zero and if it is zero Jump
to DISPLAY
INX H ;Move to next Byte address
DCR C ;Decrement counter value
JNZ NEXT ;If counter value is not Zero, Jump to NEXT and again
check the byte
MVI A, FFH ;Move FFH to A to indicate 40H is not in the set.
OUT PORT1 ;Output the value at A to PORT1
HLT
DISPLAY: MOV A,H ;Load the high value address to A
OUT PORT1 ;Display the memory page stored at A to PORT1
MOV A,L ;Load lower order memory address to A
OUT PORT2 ;Display the lower order memory address at PORT2
HLT

Detailed Explanation:
 Point to the address where given readings are stored. It is given that
readings are stored starting at XX50H. Let XX=20.
We use LXI H, 2050H. To point to address of readings.
 Store the byte to be found at memory location. We have byte to be
found = 40H, we stored it in Register D using MVI D,40H
 There are 8 values therefore we will be checking 8 times. For this we
set the counter by storing 8 in register C. MVI C, 08H.
Inside NEXT:
 Get the first byte data from the source memory location and store it in
A for comparison. MVI A, M. [Here, M is Source memory location].
 Compare the value stored at A with the value that is to be checked and
is stored in register D. CMP D: Compares value of A with D.
 If the value at A = Value at D then CMP D will set Zero flag: 1
JZ Display: This will Jump to DISPLAY section if 40H is found in
the set.
 If the zero flag is not set it means the current value at A is not equal to
D. and we will increment the source address to get next value: INC H
 Decrement the counter: DCR C
 JNZ NEXT: Check if after decrementing the value is non zero. If it is
non zero it means there are more values to be compared and the
execution jumps to NEXT to compare more values.
 If the counter becomes Zero, It means all the values have been
checked and 40H is not found in the set. In this condition, according to
the question FFH is to be output.
MVI A FFH: Moves FFH to the A.
 OUT PORT1: Output value of A, here it is FFH to the PORT1.
 Stop the execution using HLT
Inside DISPLAY
 It is executed if 40H is found in the set.
 MOV A, H: It moves the Higher address value of location where 40H
is found to A.
 OUT PORT1: Outputs the content of A to port1, here the higher
address value of the location of 40H is output to PORT1.
 MOV A,L: It moves the lower address value of location of 40H to A.
 OUT PORT2: Outputs the lower address value of location of 40H to
PORT2.

7. The following program adds the number of bytes stored in memory


locations starting from XX00H and saves the result in memory. Read
the program and answer the questions given below
LXI H,XX00H ;Set up HL as a data pointer
LXI D,0000H;set up D as a byte counter and E as a Carry
register
NEXT: ADD M ;Add byte
JNC SKIP ;If the result has no carry, do not
increment Carry register
INR E
SKIP: DCR D ;Update byte counter
JNZ NEXT ;Go to the next byte
LXI H,XX90H
MOV M,A ;Save the result
INX H
MOV M,E
HLT
a. Assuming the byte counter is set up appropriately; specify the
number of bytes that are added by the program.

SOLUTION:
Number of Bytes that are added by Program: 256

b. Specify the memory locations where the result is stored.

SOLUTION:
Memory Locations where result is stored: 2090H – 2091H

c. Identify the two errors in this program.

SOLUTION:
ERROR 1: A not initialized to 00
ERROR 2: INX H is missing in the loop

You might also like