Exercises 230520024
Exercises 230520024
• Registers
A B Y
0 0 0
0 1 1
1 0 1
1 1 0
5. What is a register?
A stack pointer is a small register that stores the address of the last
program request in a stack. A stack is a specialized buffer which
stores data from the top down. As new requests come in, they
"push down" the older ones. The most recently entered request
always resides at the top of the stack, and the program always takes
requests from the top.
To switch some buses between address and data bus, this bit is used.
1. This is the starting condition where the program counter is zero and the stack pointer is 8000
(all these numbers are hexadecimal).
2. This simply loads register r0 with the immediate value 7 and moves to the next step (I'll
assume that you understand the default behavior will be to move to the next step unless
otherwise specified).
3. This pushes r0 onto the stack by reducing the stack pointer by two then storing the value of
the register to that location.
4. This calls a subroutine. What would have been the program counter is pushed on to the stack
in a similar fashion to r0 in the previous step and then the program counter is set to its new
value. This is no different to a user-level push other than the fact it's done more as a system-
level thing.
5. This loads r1 from a memory location calculated from the stack pointer - it shows a way to
pass parameters to functions.
6. The return statement extracts the value from where the stack pointer points and loads it into
the program counter, adjusting the stack pointer up at the same time. This is like a system-
level pop (see next step).
7. Popping r0 off the stack involves extracting the value from where the stack pointer points
then adjusting that stack pointer up.
Halt instruction simply leaves program counter where it is, an infinite loop of sorts.
11. 01H nolu porttan veriyi okuyup 02H nolu porta gönderen programı
yazınız.
IN 01H
OUT 02H
HLT
14. 2050H adresindeki veriyi alıp 25H dan çıkaran ve 2051H adresine
kaydeden programı yazınız.
LDA 2050H
SUI 25H
STA 2051H
HLT
MVI C, 6FH
MVI D, 4CH
DCR C
INR D
MOV A, C
ADD D
STA 2050H
HLT
16. D ve C registerlarına 9BH ve A7H sayılarını yükleyin. Daha sonra bu sayıları
toplatın. Eğer toplam FFH tan büyükse PORT0 çıkışına 01H bilgisini yazdırın.
Büyük değilse, toplamı görüntüleyin.
MVI D, 9BH
MVI C, A7H
MOV A, D
ADD C
JNC atla
MVI A, 01H
atla: OUT PORT0
HLT
MVI D, 10H
LXI H, xx50H
LXI B, xx70H
atla:MOV A, M
STAX B
INX H
INX B
DCR D
JNZ atla
HLT
18. Write a 8085 assembly language program to add three 16 bit numbers stored in
register HL, DE, BC and store the result in DE with minimum number of
instructions.
Example –
Assumptions –
1. Numbers to be added are already stored in register HL, DE, BC
2. Numbers stored in register are such that final result should not be
greater than FFFF
DAD: - Add register pair to HL register. The 16-bit contents of the
specified register pair are
added to the contents of the HL register and the sum is stored in the HL
register.
19. Write a program to add 2-BCD numbers where starting address is 2000 and the
numbers is stored at 2500 and 2501 memory addresses and store sum into 2502 and
carry into 2503 memory address.
Example –
Algorithm –
1. Load 00H in a register (for carry)
2. Load content from memory into register pair
3. Move content from L register to accumulator
4. Add content of H register with accumulator
5. Add 06H if sum is greater than 9 or Auxiliary Carry is not zero
6. If carry flag is not equal to 1, go to step 8
7. Increment carry register by 1
8. Store content of accumulator into memory
9. Move content from carry register to accumulator
10.Store content of accumulator into memory
11.Stop
20. Write a 8085 program to subtract two 16-bit numbers where starting address
is 2000 and the numbers are at 3000 and 3002 memory address and store result
into 3004 and 3006 memory address.
Algorithm:
1. Load 0000H into CX register (for borrow)
2. Load the data into AX(accumulator) from memory 3000
3. Load the data into BX register from memory 3002
4. Subtract BX with Accumulator AX
5. Jump if no borrow
6. Increment CX by 1
7. Move data from AX(accumulator) to memory 3004
8. Move data from CX register to memory 3006
9. Stop
21. Write an assembly language program in 8085 microprocessor to subtract two 16 bit
numbers. Assumption –
Starting address of program: 2000
Input memory location: 2050, 2051, 2052, 2053
Output memory location: 2054, 2055
Explanation –
1. LHLD 2050: load HL pair with address 2050.
2. XCHG: exchange the content of HL pair with DE.
3. LHLD 2052: load HL pair with address 2050.
4. MOV A, E: move the content of register E to A.
5. SUB L: subtract the content of A with the content of register L.
6. STA 2054: store the result from accumulator to memory address 2054.
7. MOV A, D: move the content of register D to A.
8. SBB H: subtract the content of A with the content of register H with
borrow.
9. STA 2055: store the result from accumulator to memory address 2055.
10.HLT: stops executing the program and halts any further execution.
22. Write an assembly language program in 8086 microprocessor to find average of n
eight bit numbers. N is stored in 500H.
Algorithm:
1. Assign value 500 in SI and 600 in DI
2. Move the contents of [SI] in CL
3. Move 0000 in AX
4. Move the contents of CL to BL
5. Increment the value of SI by 1
6. Add the contents of AL and [SI]
7. Add 00 to AH with previous carry
8. Increment the value of SI by 1
9. Decrements the value of CL by 1
10.If Zero Flag (ZF) is not set go to step 6 else go to step 11
11.Divide the contents of AX by BL
12.Move the contents of AX in [DI]
13.Halt the program
Explanation –
1. MOV SI, 500 is used to move offset 500 to Starting Index(SI).
2. MOV DI, 600 is used to move offset 600 to Destination Index(DI).
3. MOV AX, 0000 is used to move data 0000 to AX.
4. MOV CL, [SI] is used to move the contents of [SI] to BL.
5. MOV BL, CL is used to copy contents of CL to BL.
6. INC SI is used to increment contents of SI by 1.
7. ADD AL, [SI] is used to add contents of [SI] to AL.
8. ADC AH, 00 is used to 00 along with previous cy to AH.
9. INC SI is used to increment contents of SI by 1.
10.DEC CL is used to decrement contents of CL by 1.
11.JNZ 40E is used to jump to offset 40E if value of ZF = 0.
12.DIV BL is used to divide contents of AX by BL.
13.MOV [DI], AX is used to move the contents of AX to [DI].
14.HLT stops executing the program and halts any further execution.
23. Write an 8086 assembly Language Program to find sum of odd numbers in a
given series containing 8 bit numbers stored in a continuous memory location and
store the result in another memory location.
Example Explanation –
1. 500 offset stores the counter value of the series and the elements of the
series starts from 501 to 504 offset.
2. In this example, we have 4 terms only. Adding the odd terms (found in
BL register) 15+07 in AL register and result gets stored (1C) in AL
register.
3. The result from AL register gets stored in 600 offset.
Assumptions –
1. The counter value which tells that how many numbers are there in the
series is stored at memory location 500.
2. The elements of the series are stored in a continuous memory location
starting from 501.
3. The result is stored at a memory location 600.
4. The starting address of the program is 400.
Test –
Syntax: TEST d, s
It performs AND operation of destination(d) and source(s) but result is not stored
only flags ate modified.
Algorithm –
1. Load data from offset 500 to register CL.
2. Increment the value of offset.
3. Load 00H into CH register.
4. Load 00H into AL register.
5. Load data from offset to register BL.
6. Use TEST instruction to check whether data in BL is even or odd, if
zero flag is set means data is even then go to step 7 otherwise data is
odd then go to step 8.
7. Jump to memory location 413H.
8. Add the data of AL and BL registers and store the result in AL register.
9. Increment the value of offset.
10.Jump to memory location 40AH if content of CX is not equal to zero
otherwise go to step 11.
11.Load the data from AL register to memory location 600.
12.End
24. Write an assembly language program in 8086 microprocessor to search a number
in a string of 5 bytes, store the offset where the element is found and the number of
iterations used to find the number.
Algorithm:
1. Move 2000 in AX and assign it to ES
2. Assign value 600 to DI
3. Move 25 in AL
4. Move 0005 in CX
5. Move the contents of CX to BX
6. Clear the value of Directional Flag (DF)
7. Repeat step 7 till Zero Flag (ZF) is not set
8. Scan byte from [DI] and check its difference with contents of AL.
Update the value of DI
9. Decrements the value of DI by 1
10.Subtract the value of BX by CX
11.Decrements the value of BX by 1
12.Halt the program
Explanation –
1. MOV AX, 2000 is used to move data to register AX.
2. MOV ES, AX is used to move the data of AX to segment register ES.
3. MOV DI, 600 is used to move offset 600 to Destination Index(DI).
4. MOV AX, 25 is used to move data to AL.
5. MOV CX, 0005 is used to move data to CX.
6. MOV BX, CX is used to copy contents of CX to BX.
7. CLD is used to clear the value of Directional Flag (DF), so that memory
is accessed from lower byte to higher byte.
8. REPNE SCAS B is used to scan data from DI and compare it with data
from AL, if equal go to next step else increment the value of DI by 1
and repeat this step.
9. DEC DI is used to decrement contents of DI by 1.
10.MOV DX, DI is used to move the contents of DI to DX.
11.SUB BX, CX is used to subtract the contents of BX by CX.
12.DEC BX is used to decrement contents of BX by 1.
13.HLT stops executing the program and halts any further execution.
25. Write an assembly language program for calculating the factorial of a number using
8086 microprocessor.
Assumptions –
Starting address of program: 0400
Input memory location: 0500
Output memory location: 0600 and 0601
If the Given Number is a 16-bit number, the AX register is automatically used as the second parameter
and the product is stored in the DX:AX register pair. This means that the DX register holds the high
part and the AX register holds the low part of a 32-bit number.
Answer:
Appendix:
8085
MOV Copy from source to destination ADD Add register or memory to accumulator
MVI Move immediate 8-bit ADC Add register to accumulator with carry
LXI Load register pair immediate DAD Add register pair to H and L registers
LHLD Load H and L registers direct SUB Subtract register or memory from accumulator
STA Store accumulator direct SBB Subtract source and borrow from accumulator
SHLD Store H and L registers direct SBI Subtract immediate from accumulator with borrow
SPHL Copy H and L registers to the stack pointer INX Increment register pair by 1
XTHL Exchange H and L with top of stack DCR Decrement register or memory by 1
PUSH Push register pair onto stack DCX Decrement register pair by 1
OUT Output data from accumulator to a port with 8-bit address CMP Compare register or memory with accumulator
IN Input data to accumulator from a port with 8-bit address CPI Compare immediate with accumulator
CPO Call on parity odd ORA Logical OR register or memory with accumulator
JC Jump on carry
8086: