Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# ! / usr / bin / env python import sys from optparse import OptionParser import random import math def convert ( size ) :
#usrbinenv python
import sys
from optparse import OptionParser
import random
import math
def convertsize:
length lensize
lastchar sizelength
if lastchar k or lastchar K:
m
nsize intsize:length m
elif lastchar m or lastchar M:
m
nsize intsize:length m
elif lastchar g or lastchar G:
m
nsize intsize:length m
else:
nsize intsize
return nsize
#
# main program
#
parser OptionParser
parser.addoptionsseed', default help'the random seed', action'store', type'int', dest'seed'
parser.addoptionaasize', defaultk help'address space size egkmg action'store', type'string', dest'asize'
parser.addoptionpphysmem', defaultk help'physical memory size egkmg action'store', type'string', dest'psize'
parser.addoptionnaddresses', default help'number of virtual addresses to generate', action'store', type'int', dest'num'
parser.addoptionbb default help'value of base register', action'store', type'string', dest'base'
parser.addoptionll default help'value of limit register', action'store', type'string', dest'limit'
parser.addoptionccompute', defaultFalse, help'compute answers for me action'storetrue', dest'solve'
options args parser.parseargs
print
print 'ARG seed', options.seed
print 'ARG address space size', options.asize
print 'ARG phys mem size', options.psize
print
random.seedoptionsseed
asize convertoptionsasize
psize convertoptionspsize
if psize :
print 'Error: must specify a nonzero physical memory size.
exit
if asize :
print 'Error: must specify a nonzero addressspace size.
exit
if psize asize:
print 'Error: physical memory size must be GREATER than address space size for this simulation
exit
#
# need to generate base, bounds for segment registers
#
limit convertoptionslimit
base convertoptionsbase
if limit :
limit intasizeasize random.random
# now have to find room for them
if base :
done
while done :
base intpsize random.random
if base limit psize:
done
print 'BaseandBounds register information:
print
print Base : xx decimal dbase base
print Limit : dlimit
print
if base limit psize:
print 'Error: address space does not fit into physical memory with those basebounds values.
print 'Base Limit: base limit Psize: psize
exit
#
# now, need to generate virtual address trace
#
print 'Virtual Address Trace'
for i in rangeoptions.num:
vaddr intasize random.random
if options.solve False:
print VA d: xx decimal: d PA or segmentation violation?i vaddr, vaddr
else:
paddr
if vaddr limit:
print VA d: xx decimal: d SEGMENTATION VIOLATION' i vaddr, vaddr
else:
paddr vaddr base
print VA d: xx decimal: d VALID: xx decimal: di vaddr, vaddr, paddr, paddr
print
if options.solve False:
print 'For each virtual address, either write down the physical address it translates to
printOR write down that it is an outofbounds address a segmentation violation For'
print 'this problem, you should assume a simple virtual address space of a given size.
print
Exercise Run the simulation with these flags: s n l What is the maximum value that base can be set to such that the address space still fits into physical memory in its entirety?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started