M.Umair Farooq
M.Umair Farooq

Reputation: 11

Simulation of Virtual Memory Management with Swapping and Access Protection in assembly

these are my project requirements

  1. Use VirtualAlloc to reserve two memory regions: Virtual Memory: A larger region to simulate virtual memory (e.g. 10).
  2. Mark these areas with read and write permissions to allow subsequent manipulations. Step 2: Create Files to Simulate Programs in a Specific Folder
  3. Create several text files that will represent different programs.
  4. File Structure: MOV AX, BX First line: Memory size that the program will occupy in RAM (in bytes). ADD AX, 4
  1. Display at all times all existing files (programs) with their status (running, open, swapped or closed) SUB CX, AX Step 3: Launch a Program (Load it into RAM)
  2. Reading File: When the user chooses to launch a program, read the associated file.
  3. Allocation in RAM:

mark the space occupied by instructions as read-only and the rest as read/write Otherwise, perform swapping: move a program into virtual memory to free up space in RAM then load it into memory Once loaded, show that it is running and display all of its instructions Step 4: View RAM and Virtual Memory Status

  1. Create a function to display: The start and end addresses of RAM and virtual memory. Information about each loaded program (start address, end address, status: in RAM or swapped).
  2. Allow the user to view this status at any time. Step 5: Close a Program
  3. Freeing Memory: When the user chooses to close a program, free the reserved memory space in RAM (or in virtual memory if the program is swapped there). T
  4. Update Status: Update the status of the program to indicate that it is closed. Step 6: Using a Program
  5. Allow user to select a program as active program (in use) and display all its instructions
  6. If the program is in virtual memory (swapped), bring it back to RAM by applying swapping mechanisms if necessary. Step 7: Request Memory Access
  7. Allow the active program to request access to a specific memory address.
  8. Access Verification: Check if the address is in the memory space reserved for this program. On 1 point: the access request must specify the type of access (read, write) and after the first verification it is also necessary to test if it is the correct type of access (avoid writing to addresses containing instructions)
  9. Display a message indicating whether access is allowed or denied.

this is my code in masm x86 Assembly. when i build this code. it builds succesfully. but when i run it it throws me an exception. Exception Error is attached in an image. i can specify the line where is this exception thrown. the Exception is thrown at line no 72
.this is the exception image


INCLUDE Irvine32.inc

; Constants
RAM_SIZE      EQU 4 \* 1024      ; Simulated 4 KB RAM
VM_SIZE       EQU 10 \* 1024     ; Simulated 10 KB Virtual Memory
MAX_PROGRAMS  EQU 10            ; Maximum number of programs

; Permissions
PERM_READ     EQU 1
PERM_WRITE    EQU 2

.DATA
RAM        DB RAM_SIZE DUP(0)  ; Simulated RAM memory region
VirtualMem DB VM_SIZE DUP(0)   ; Virtual memory region

; Program metadata
ProgramStatus DB MAX_PROGRAMS DUP(0) ; 0 = Closed, 1 = Running, 2 = Swapped
ProgramRAMStart DW MAX_PROGRAMS DUP(0) ; Start address in RAM
ProgramRAMSize DW MAX_PROGRAMS DUP(0)  ; Size occupied in RAM
ProgramVMStart DW MAX_PROGRAMS DUP(0)  ; Start address in Virtual Memory

; Buffers for user input and program data
inputBuffer  DB 50 DUP(0)
programBuffer DB 100 DUP(0)

; Menu options
Menu DB "1. Load Program", 13, 10, "2. View Memory Status", 13, 10, "3. Close Program", 13, 10, "4. Use Program", 13, 10, "5. Request Memory Access", 13, 10, "6. Exit", 0

; Status and error messages
NoFreeSlotMsg DB "Error: No free slots available for new programs.", 13, 10, 0
ProgramLoadedMsg DB "Program loaded successfully.", 13, 10, 0
CloseProgramMsg DB "Program closed successfully.", 13, 10, 0
UseProgramMsg DB "Using the selected program.", 13, 10, 0
RAMStatus DB "RAM and Virtual Memory Status:", 13, 10, 0
AccessDeniedMsg DB "Access denied: Invalid permissions or memory address.", 13, 10, 0
AccessGrantedMsg DB "Access granted.", 13, 10, 0

.CODE
MAIN PROC
; Initialize RAM and Virtual Memory
CALL InitializeMemory

    ; Display menu and process user input
    WHILE_TRUE:
        CALL DisplayMenu
        CALL GetUserInput
        CMP AL, '1'
        JE LoadProgram
        CMP AL, '2'
        JE ViewMemory
        CMP AL, '3'
        JE CloseProgram
        CMP AL, '4'
        JE UseProgram
        CMP AL, '5'
        JE RequestMemoryAccess
        CMP AL, '6'
        JE ExitProgram
        JMP WHILE_TRUE

ExitProgram:
MOV AX, 4C00h
INT 21h
MAIN ENDP

; Initialize simulated memory
InitializeMemory PROC
; Zero out RAM
LEA DI, RAM            ; Load effective address of RAM
MOV CX, RAM_SIZE       ; Set count to RAM size
MOV AL, 0              ; Value to store (0)
REP STOSB              ; Fill RAM with 0

    ; Zero out Virtual Memory
    LEA DI, VirtualMem     ; Load effective address of VirtualMem
    MOV CX, VM_SIZE        ; Set count to VM size
    MOV AL, 0              ; Value to store (0)
    REP STOSB              ; Fill Virtual Memory with 0
    
    ; Reset program metadata
    MOV CX, MAX_PROGRAMS   ; Loop counter for MAX_PROGRAMS
    XOR DI, DI             ; Clear DI to use it as an index

Upvotes: 0

Views: 39

Answers (0)

Related Questions