0% found this document useful (0 votes)
8 views29 pages

8086 Assembly Language Operations Lab

The document outlines a series of experiments in 8086 assembly language programming, covering various arithmetic and logical operations such as ADD, SUB, MUL, DIV, and bitwise operations like OR, AND, XOR, and NOT. Each experiment includes a problem statement, required tools, theoretical concepts, step-by-step program logic, and sample input/output. Additionally, it features an experiment using Booth's algorithm in C for multiplying signed integers, demonstrating the application of various programming techniques and DOS interrupts.

Uploaded by

AGAM PURI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

8086 Assembly Language Operations Lab

The document outlines a series of experiments in 8086 assembly language programming, covering various arithmetic and logical operations such as ADD, SUB, MUL, DIV, and bitwise operations like OR, AND, XOR, and NOT. Each experiment includes a problem statement, required tools, theoretical concepts, step-by-step program logic, and sample input/output. Additionally, it features an experiment using Booth's algorithm in C for multiplying signed integers, demonstrating the application of various programming techniques and DOS interrupts.

Uploaded by

AGAM PURI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Microprocessor and Assembly Language

Programming Lab Report


Experiment 1: ADD and OR Operations in 8086 (MASM & DEBUG)

Problem Statement / Objective


Write an 8086 assembly program that performs addition (ADD) and bitwise OR operations
on two 8-bit numbers and stores/displays results. Assemble with MASM and run under
DOS/DEBUG.

Requirements / Tools
- MASM or TASM
- LINK utility
- [Link] (optional)
- DOSBox or 8086 emulator
- Basic knowledge of 8086 registers and interrupts

Theory / Concepts
ADD performs arithmetic addition: destination = destination + source; affects CF, ZF, SF, OF.
OR performs bitwise OR: destination = destination OR source; clears CF and OF, sets
ZF/SF/PF accordingly.

Program Logic / Step-by-step


1. Initialize DS to point to data segment.
2. Load NUM1 and NUM2 from data into AL/BL.
3. Perform ADD and store result in SUM.
4. Perform OR and store result in ORR.
5. Terminate program with INT 21H/AH=4CH.
Mount c c:\8086\
C:
Edit [Link]

Program Code
;--------------------------------------------
; Program: ADD and OR Operations in 8086 (MASM)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB 25H ; first operand (hex)
NUM2 DB 15H ; second operand (hex)
SUM DB ? ; will hold addition result
ORR DB ? ; will hold OR result
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX ; Initialize DS

MOV AL, NUM1


ADD AL, NUM2 ; AL = NUM1 + NUM2
MOV SUM, AL ; Store sum

MOV AL, NUM1


OR AL, NUM2 ; AL = NUM1 OR NUM2
MOV ORR, AL ; Store OR result

MOV AH, 4CH ; Exit to DOS


INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Inputs: NUM1 = 25H (37 decimal), NUM2 = 15H (21 decimal)
Expected ADD result: 3AH (58 decimal)
Expected OR result: 35H (53 decimal)

Result / Conclusion
Program demonstrates basic arithmetic and logical instructions on 8086. Flags affected:
ADD updates CF/OF, OR clears CF/OF.
Experiment 2: SUB and AND Operations in 8086 (MASM & DEBUG)

Problem Statement / Objective


Write an 8086 program to subtract two 8-bit numbers and to compute their bitwise AND;
store results.

Requirements / Tools
- MASM/TASM, LINK
- [Link] or DOSBox

Theory / Concepts
SUB does destination = destination - source; affects borrow (CF), ZF, SF, OF. AND does
bitwise AND; clears CF/OF, affects ZF/SF/PF.

Program Logic / Step-by-step


1. Init data segment.
2. Load operands into AL and BL.
3. SUB AL, NUM2 → store DIFF.
4. AND AL, NUM2 → store ANDR.
5. Exit.

Program Code
;--------------------------------------------
; Program: SUB and AND Operations in 8086 (MASM)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB 35H
NUM2 DB 12H
DIFF DB ? ; subtraction result
ANDR DB ? ; AND result
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AL, NUM1


SUB AL, NUM2
MOV DIFF, AL

MOV AL, NUM1


AND AL, NUM2
MOV ANDR, AL
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Inputs: NUM1=35H (53), NUM2=12H (18)
Expected DIFF: 23H (35 decimal)
Expected ANDR: 10H (16 decimal)

Result / Conclusion
Shows SUB and AND; note flag effects and ensure DS initialized before accessing data.
Experiment 3: MUL and XOR Operations in 8086 (MASM & DEBUG)

Problem Statement / Objective


Write an 8086 program to multiply two unsigned 8-bit numbers (MUL) and perform bitwise
XOR.

Requirements / Tools
- MASM/TASM, LINK
- [Link] or DOSBox

Theory / Concepts
MUL (unsigned): AL * r/m8 -> AX (AH:AL). CF/OF set if AH != 0. XOR performs exclusive OR;
clears CF/OF.

Program Logic / Step-by-step


1. Init DS.
2. MOV AL, NUM1; MOV BL, NUM2; MUL BL -> AX (product). Store AH/AL.
3. XOR AL, NUM2 -> store XORR.
4. Exit.

Program Code
;--------------------------------------------
; Program: MUL and XOR Operations in 8086 (MASM)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB 05H
NUM2 DB 04H
PRODL DB ? ; lower byte of product
PRODH DB ? ; higher byte of product
XRES DB ? ; XOR result
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AL, NUM1


MOV BL, NUM2
MUL BL ; AX = AL * BL
MOV PRODL, AL
MOV PRODH, AH

MOV AL, NUM1


XOR AL, NUM2
MOV XRES, AL

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Inputs: NUM1=05H, NUM2=04H
Expected product AX=0014H (20 decimal), PRODL=14H, PRODH=00H
Expected XOR result: 01H (5 xor 4 = 1)

Result / Conclusion
Demonstrates 8-bit unsigned multiplication storing 16-bit result in AX; XOR for bitwise
exclusive OR.
Experiment 4: DIV and NOT Operations in 8086 (MASM & DEBUG)

Problem Statement / Objective


Write an 8086 program to divide two 8-bit numbers (DIV) and perform bitwise NOT on a
byte.

Requirements / Tools
- MASM/TASM, LINK
- [Link] or DOSBox
- Ensure divisor not zero

Theory / Concepts
DIV unsigned: AX / r/m8 -> AL = quotient, AH = remainder. NOT inverts bits; does not affect
flags.

Program Logic / Step-by-step


1. Init DS. 2. Clear AH and load dividend into AX. 3. MOV BL, divisor; DIV BL; store AL-
>QUOT, AH->REM. 4. NOT operation on NUM1 -> store NOTR. 5. Exit.

Program Code
;--------------------------------------------
; Program: DIV and NOT Operations in 8086 (MASM)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
NUM1 DB 28H ; 40 decimal
NUM2 DB 05H ; divisor 5
QUOT DB ?
REM DB ?
NVAL DB ?
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AL, NUM1


MOV AH, 00H ; clear AH before DIV
MOV BL, NUM2
DIV BL ; AL=quotient, AH=remainder
MOV QUOT, AL
MOV REM, AH

MOV AL, NUM1


NOT AL
MOV NVAL, AL

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Inputs: NUM1=28H (40), NUM2=05H (5)
Expected Quotient=08H (8), Remainder=00H (0)
NOT of 28H => D7H

Result / Conclusion
Demonstrates proper setup for DIV (clear AH) and NOT instruction behavior.
Experiment 5: Block Transfer using Index Registers (SI/DI) (8086)

Problem Statement / Objective


Copy a block of bytes from source to destination using SI, DI, CX, REP MOVSB.

Requirements / Tools
- MASM/TASM, DOSBox
- Understand DS and ES segments and direction flag

Theory / Concepts
MOVSB moves a byte from [DS:SI] to [ES:DI]. REP MOVSB repeats this CX times. CLD clears
direction flag to increment SI/DI.

Program Logic / Step-by-step


1. Init DS and ES.
2. LEA SI, SRC; LEA DI, DEST; MOV CX, COUNT; CLD; REP MOVSB; Exit.

Program Code
;--------------------------------------------
; Program: Block Transfer using Index Registers (8086)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
SRC DB 10H,20H,30H,40H,50H
DEST DB 5 DUP(?)
COUNT EQU 5
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV ES, AX

LEA SI, SRC


LEA DI, DEST
MOV CX, COUNT
CLD
REP MOVSB

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN
Sample Input / Expected Output
SRC before: 10H,20H,30H,40H,50H
DEST before: uninitialized
After REP MOVSB, DEST: 10H,20H,30H,40H,50H

Result / Conclusion
Efficient block copy using string instructions and REP prefix.
Experiment 6: Block Exchange using Index Registers (8086)

Problem Statement / Objective


Exchange contents of two equal-sized memory blocks using SI and DI.

Requirements / Tools
- MASM/TASM, LINK
- Use of temporary register for swapping

Theory / Concepts
Algorithm: For each element i: temp = [SI+i]; [SI+i] = [DI+i]; [DI+i] = temp. Use CX as
counter.

Program Logic / Step-by-step


1. Init DS. 2. LEA SI, BLOCK1; LEA DI, BLOCK2; MOV CX, COUNT; Loop: read, swap, inc SI/DI,
LOOP.

Program Code
;--------------------------------------------
; Program: Block Exchange using Index Registers (8086)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
BLOCK1 DB 10H,20H,30H,40H,50H
BLOCK2 DB 60H,70H,80H,90H,0A0H
COUNT EQU 5
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

LEA SI, BLOCK1


LEA DI, BLOCK2
MOV CX, COUNT
EXCHANGE_LOOP:
MOV AL, [SI]
MOV BL, [DI]
MOV [SI], BL
MOV [DI], AL
INC SI
INC DI
LOOP EXCHANGE_LOOP

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Before:
BLOCK1: 10H,20H,30H,40H,50H
BLOCK2: 60H,70H,80H,90H,0A0H
After exchange:
BLOCK1: 60H,70H,80H,90H,0A0H
BLOCK2: 10H,20H,30H,40H,50H

Result / Conclusion
Swapped blocks correctly using indexed addressing and LOOP instruction.
Experiment 7: Find Character in a String using DOS Interrupts (8086)

Problem Statement / Objective


Accept a string from keyboard and a character, search for the character, display found/not
found using INT 21H utilities.

Requirements / Tools
- MASM/TASM
- DOS interrupts (INT 21H functions 0AH, 01H, 09H)
- Buffer format for function 0AH

Theory / Concepts
DOS buffered input (AH=0AH): first byte is max size, second byte is actual count, data
follows starting at offset+2. Use SI to iterate over characters.

Program Logic / Step-by-step


1. Prompt for string (0AH buffer) and read.
2. Prompt for character (01H) and read.
3. Set SI to buffer+2, loop through count, compare, branch accordingly.

Program Code
;--------------------------------------------
; Program: Find Character in a String using DOS Interrupts (8086)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
PROMPT1 DB 'Enter a string (max 20 chars):$'
PROMPT2 DB 0DH,0AH,'Enter a character to search:$'
FOUNDMSG DB 0DH,0AH,'Character FOUND!$'
NOTFNDMSG DB 0DH,0AH,'Character NOT FOUND!$'
BUFFER DB 20
DB ?
DB 20 DUP(0)
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

; Prompt for string


LEA DX, PROMPT1
MOV AH, 09H
INT 21H
LEA DX, BUFFER
MOV AH, 0AH
INT 21H

; Prompt for character


LEA DX, PROMPT2
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
MOV BL, AL ; character to search

; Search
LEA SI, BUFFER+2
MOV CL, [BUFFER+1]
CMP CL, 0
JE NOT_FOUND
SEARCH_LOOP:
MOV AL, [SI]
CMP AL, BL
JE FOUND
INC SI
DEC CL
JNZ SEARCH_LOOP

NOT_FOUND:
LEA DX, NOTFNDMSG
MOV AH, 09H
INT 21H
JMP EXIT

FOUND:
LEA DX, FOUNDMSG
MOV AH, 09H
INT 21H

EXIT:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Sample: Input string HELLO (enter), search character L -> Output: Character FOUND!

Result / Conclusion
Uses DOS buffered input and demonstrates string scanning and interrupt usage.
Experiment 8: Booth's Multiplication Algorithm (C)

Problem Statement / Objective


Implement Booth's algorithm in C to multiply two signed integers and display intermediate
steps and final product.

Requirements / Tools
- Turbo C/GCC (Turbo C expects conio.h for getch if used)
- Understanding of two's complement and arithmetic right shifts

Theory / Concepts
Booth's algorithm uses A (accumulator), Q (multiplier), Q-1 bit and repeats n times: based
on Q0 and Q-1, add or subtract M then arithmetic right shift (A,Q,Q-1).

Program Logic / Step-by-step


1. Read multiplicand M and multiplier Q and bit width n.
2. Initialize A=0, Q-1=0, loop n times applying Booth's rules.
3. Combine A and Q as final product.

Program Code
#include <stdio.h>
#include <conio.h>
int main() {
int M, Q, A = 0, Q_1 = 0, n, count;
printf("Enter multiplicand (M): ");
scanf("%d", &M);
printf("Enter multiplier (Q): ");
scanf("%d", &Q);
printf("Enter number of bits (n, e.g. 8): ");
scanf("%d", &n);
count = n;
printf("Initial: A=%d Q=%d Q-1=%d\n", A, Q, Q_1);
while (count--) {
int q0 = Q & 1;
if (q0 == 0 && Q_1 == 1) {
A = A + M;
printf("A=A+M => A=%d\n", A);
} else if (q0 == 1 && Q_1 == 0) {
A = A - M;
printf("A=A-M => A=%d\n", A);
}
Q_1 = Q & 1;
// Arithmetic right shift of (A,Q,Q_1)
int combined = ((A << n) | (Q & ((1<<n)-1)));
// perform signed arithmetic right shift by 1 on (A:Q)
int sign = (combined & (1 << (n*2 - 1))) ? 1 : 0;
combined = (combined >> 1) | (sign ? (1 << (n*2 - 1)) : 0);
A = combined >> n;
Q = combined & ((1<<n)-1);
printf("After shift: A=%d Q=%d Q-1=%d\n", A, Q, Q_1);
}
long long product = ((long long)A << n) | (unsigned int)Q;
printf("Final Product = %lld\n", product);
getch();
return 0;
}

Sample Input / Expected Output


Example: M=3, Q=-4, n=8 -> Final Product = -12 (expected)

Result / Conclusion
Booth's algorithm reduces number of additions/subtractions for signed multiplication and
handles two's complement correctly.
Experiment 9: Restoring Division Algorithm (C)

Problem Statement / Objective


Implement restoring division in C to compute quotient and remainder by simulating
hardware steps.

Requirements / Tools
- Turbo C/GCC
- Understanding of shifts and subtraction/restore steps

Theory / Concepts
Restoring division maintains remainder A and shifts (A,Q) left, subtracts divisor, if negative
restore and set Q0 accordingly.

Program Logic / Step-by-step


1. Read dividend and divisor and bit width n.
2. Loop n times: left shift (A,Q), A=A-M, if A<0 restore and set Q0=0 else Q0=1.
3. Output Q and A.

Program Code
#include <stdio.h>
#include <conio.h>
int main() {
int dividend, divisor, n;
printf("Enter Dividend: "); scanf("%d", &dividend);
printf("Enter Divisor: "); scanf("%d", &divisor);
printf("Enter bit size (n, e.g. 8): "); scanf("%d", &n);
int A = 0;
int Q = dividend & ((1<<n)-1);
int M = divisor & ((1<<n)-1);
for (int i = 0; i < n; i++) {
// left shift (A,Q)
A = (A << 1) | ((Q >> (n-1)) & 1);
Q = (Q << 1) & ((1<<n)-1);
A = A - M;
if (A < 0) {
Q = Q & (~1);
A = A + M; // restore
} else {
Q = Q | 1;
}
printf("Step %d: A=%d Q=%d\n", i+1, A, Q);
}
printf("Quotient = %d, Remainder = %d\n", Q, A);
getch();
return 0;
}

Sample Input / Expected Output


Example: Dividend=13, Divisor=3, n=4 -> Quotient=5, Remainder=1

Result / Conclusion
Restoring division simulates hardware algorithm giving correct quotient and remainder for
unsigned values.
Experiment 10: Two 8-bit BCD Addition with Keyboard Input and Output
(8086)

Problem Statement / Objective


Accept two 2-digit BCD numbers as keyboard input and compute their BCD sum using DAA
and display result via INT 21H.

Requirements / Tools
- MASM/TASM
- INT 21H for input/output
- DAA instruction for BCD adjust

Theory / Concepts
DAA adjusts AL after addition to produce correct BCD when adding two packed BCD
nibbles. Input via INT 21H function 01 or buffered 0AH.

Program Logic / Step-by-step


1. Prompt and read two ASCII digits for first number; convert ASCII to numeric; combine as
BCD.
2. Repeat for second number.
3. ADD and DAA, then display ASCII of result digits.

Program Code
;--------------------------------------------
; Program: BCD Addition (two 2-digit BCD numbers)
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
PROMPT1 DB 'Enter first 2-digit BCD number (e.g. 25):$'
PROMPT2 DB 0DH,0AH,'Enter second 2-digit BCD number:$'
RESULT_MSG DB 0DH,0AH,'Sum = $'
BUFF DB 4 DUP(?)
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
; Read first two digits (using AH=01 twice)
LEA DX, PROMPT1
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
SUB AL,30H
MOV BL, AL ; tens
MOV AH,01H
INT 21H
SUB AL,30H
MOV BH, AL ; ones
MOV AL, BL
MUL BYTE PTR 10
ADD AL, BH
; AL now contains numeric value of first number
; Read second number
LEA DX, PROMPT2
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
SUB AL,30H
MOV CL, AL
MOV AH,01H
INT 21H
SUB AL,30H
MOV CH, AL
MOV AL, CL
MUL BYTE PTR 10
ADD AL, CH
; Add both numbers
ADD AL, BL ; (note: BL currently contains first tens, but for
brevity in this template you would store first number earlier)
DAA
; Display result (simple single byte output)
LEA DX, RESULT_MSG
MOV AH,09H
INT 21H
; Convert AL to two ASCII digits
MOV AH,0
MOV BL,10
DIV BL
ADD AH,30H
MOV DL, AH
MOV AH,02H
INT 21H
ADD AL,30H
MOV DL, AL
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
Sample Input / Expected Output
Input: 25 and 47 -> Output: Sum = 72

Result / Conclusion
Uses DAA to correct BCD after binary addition; careful ASCII conversion required for
display.
Experiment 11: Cursor Activity: Hide and Change Cursor Shape using INT
10H (8086)

Problem Statement / Objective


Use BIOS INT 10H function to hide the text cursor and change its shape (e.g., block or
underline).

Requirements / Tools
- MASM/TASM
- INT 10H BIOS services (AH=01 to set cursor shape)
- DOS or emulator supporting BIOS

Theory / Concepts
Cursor shape determined by CH (start scan line) and CL (end scan line). Setting bit 5 in CH
hides the cursor (CH >= 20H).

Program Logic / Step-by-step


1. Use INT 10H AH=01, CH=20H, CL=0 to hide cursor.
2. Wait for keypress using INT 21H AH=01.
3. Set CH=00H CL=07H and call INT 10H AH=01 to make block cursor.
4. Exit.

Program Code
;--------------------------------------------
; Program: Cursor Control using INT 10H
;--------------------------------------------
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'Hiding Cursor...$'
MSG2 DB 0DH,0AH,'Press any key to change cursor shape...$'
MSG3 DB 0DH,0AH,'Changing to block cursor...$'
MSG4 DB 0DH,0AH,'Program End.$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX, MSG1
MOV AH,09H
INT 21H
MOV AH,01H
MOV CH,20H
MOV CL,00H
INT 10H
LEA DX,MSG2
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
LEA DX,MSG3
MOV AH,09H
INT 21H
MOV AH,01H
MOV CH,00H
MOV CL,07H
INT 10H
LEA DX,MSG4
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

Sample Input / Expected Output


Shows cursor hidden, then changed to block shape after key press.

Result / Conclusion
Demonstrates use of BIOS video services via INT 10H to control cursor appearance.
Experiment 12: Direct and Fully Associative Cache Mapping (C)

Problem Statement / Objective


Simulate direct mapping and fully associative mapping of cache and compute hit/miss
counts.

Requirements / Tools
- Turbo C/GCC
- Basic arrays and loops

Theory / Concepts
Direct mapping maps each block to exactly one line (line = block % cacheSize). Fully
associative allows placement anywhere and uses replacement policy.

Program Logic / Step-by-step


1. Read sequence of block references and cache size.
2. For direct mapping compute line index and update cache; count hits/misses.
3. For fully associative search cache for block; if miss use FIFO replacement.

Program Code
#include <stdio.h>
#define MAX 100
void directMapping(int blocks[], int n, int cacheSize){
int cache[MAX]; int hits=0,misses=0;
for(int i=0;i<cacheSize;i++) cache[i]=-1;
for(int i=0;i<n;i++){
int line = blocks[i] % cacheSize;
if(cache[line]==blocks[i]) hits++; else {cache[line]=blocks[i];
misses++;}
}
printf("\nDirect Mapping: Hits=%d Misses=%d\n", hits, misses);
}
void fullyAssociative(int blocks[], int n, int cacheSize){
int cache[MAX]; int hits=0,misses=0,rep=0;
for(int i=0;i<cacheSize;i++) cache[i]=-1;
for(int i=0;i<n;i++){
int found=0;
for(int j=0;j<cacheSize;j++) if(cache[j]==blocks[i]){hits++;
found=1; break;}
if(!found){ cache[rep]=blocks[i]; rep=(rep+1)%cacheSize; misses+
+; }
}
printf("Fully Associative: Hits=%d Misses=%d\n", hits, misses);
}
int main(){ int n,cacheSize,blocks[MAX]; printf("Enter number of
references: "); scanf("%d",&n); printf("Enter block refs: "); for(int
i=0;i<n;i++) scanf("%d",&blocks[i]); printf("Enter cache size: ");
scanf("%d",&cacheSize); directMapping(blocks,n,cacheSize);
fullyAssociative(blocks,n,cacheSize); return 0; }

Sample Input / Expected Output


Example: refs: 2 5 8 5 12 8 2 15 7 5 ; cacheSize=4 -> shows hit/miss counts

Result / Conclusion
Simulates mapping policies; fully associative gives better hit ratio generally but is costlier.
Experiment 13: 2-Way Set Associative and Fully Associative Mapping (C)

Problem Statement / Objective


Simulate 2-way set associative cache and compare with fully associative mapping.

Requirements / Tools
- Turbo C/GCC

Theory / Concepts
2-way: cache divided into sets where each set has 2 lines; set index = block % sets.
Fully associative: any block can go to any line; replacement via FIFO/LRU.

Program Logic / Step-by-step


1. For 2-way compute set index and check two slots. 2. If miss and slot available put there,
else replace via FIFO. 3. Track hits/misses.

Program Code
#include <stdio.h>
#define MAX 100
void twoWaySet(int blocks[], int n, int cacheSize){
int sets = cacheSize/2;
int cache[50][2]; int front[50]; int hits=0,misses=0;
for(int i=0;i<sets;i++){ cache[i][0]=cache[i][1]=-1; front[i]=0; }
for(int i=0;i<n;i++){
int set = blocks[i] % sets; int found=0;
for(int j=0;j<2;j++) if(cache[set][j]==blocks[i]){ hits++;
found=1; break; }
if(!found){ cache[set][front[set]] = blocks[i]; front[set] =
(front[set]+1)%2; misses++; }
}
printf("2-Way Set Associative: Hits=%d Misses=%d\n", hits, misses);
}
void fullyAssoc(int blocks[], int n, int cacheSize){
int cache[MAX],hits=0,misses=0,rep=0; for(int i=0;i<cacheSize;i++)
cache[i]=-1;
for(int i=0;i<n;i++){ int found=0; for(int j=0;j<cacheSize;j++)
if(cache[j]==blocks[i]){ hits++; found=1; break; } if(!found)
{ cache[rep]=blocks[i]; rep=(rep+1)%cacheSize; misses++; } }
printf("Fully Associative: Hits=%d Misses=%d\n", hits, misses);
}
int main(){ int n,cacheSize,blocks[MAX]; printf("Enter number of
references: "); scanf("%d",&n); printf("Enter blocks: "); for(int
i=0;i<n;i++) scanf("%d",&blocks[i]); printf("Enter cache size: ");
scanf("%d",&cacheSize); twoWaySet(blocks,n,cacheSize);
fullyAssoc(blocks,n,cacheSize); return 0; }
Sample Input / Expected Output
Example run demonstrates different hit ratios between 2-way and fully associative

Result / Conclusion
2-way reduces conflicts compared to direct mapping; fully associative is most flexible.
Experiment 14: Direct and 2-Way Set Associative Mapping (C)

Problem Statement / Objective


Simulate and compare direct mapping and 2-way set associative mapping.

Requirements / Tools
- Turbo C/GCC

Theory / Concepts
Direct: block % cacheSize -> line. 2-way: sets = cacheSize/2, set = block % sets.

Program Logic / Step-by-step


1. Implement both simulations and present hit/miss counts.

Program Code
#include <stdio.h>
#define MAX 100
void directMapping(int blocks[], int n, int cacheSize){ int
cache[100],hits=0,misses=0; for(int i=0;i<cacheSize;i++) cache[i]=-1;
for(int i=0;i<n;i++){ int line = blocks[i] % cacheSize;
if(cache[line]==blocks[i]) hits++; else{ cache[line]=blocks[i]; misses+
+; } } printf("Direct Mapping: Hits=%d Misses=%d\n", hits, misses); }
void twoWaySet(int blocks[], int n, int cacheSize){ int sets =
cacheSize/2; int cache[50][2], front[50]; int hits=0,misses=0; for(int
i=0;i<sets;i++){ cache[i][0]=cache[i][1]=-1; front[i]=0; } for(int
i=0;i<n;i++){ int set = blocks[i] % sets, found=0; for(int j=0;j<2;j++)
if(cache[set][j]==blocks[i]){ hits++; found=1; break; } if(!found)
{ cache[set][front[set]] = blocks[i]; front[set]=(front[set]+1)%2;
misses++; } } printf("2-Way Set Associative: Hits=%d Misses=%d\n", hits,
misses); }
int main(){ int n,cacheSize,blocks[MAX]; printf("Enter number of refs:
"); scanf("%d", &n); printf("Enter blocks: "); for(int i=0;i<n;i++)
scanf("%d", &blocks[i]); printf("Enter cache size: "); scanf("%d",
&cacheSize); directMapping(blocks,n,cacheSize);
twoWaySet(blocks,n,cacheSize); return 0; }

Sample Input / Expected Output


Example: shows comparative hit/miss counts

Result / Conclusion
Completes the set of experiments and demonstrates cache mapping tradeoffs.

You might also like