//: Core data structures for simulating the SubX VM (subset of an x86 processor) //: //: At the lowest level ("level 1") of abstraction, SubX executes x86 //: instructions provided in the form of an array of bytes, loaded into memory //: starting at a specific address. //:: registers //: assume segment registers are hard-coded to 0 //: no floating-point, MMX, etc. yet :(before "End Types") enum { EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI, NUM_INT_REGISTERS, }; union reg { int32_t i; uint32_t u; }; :(before "End Globals") reg Reg[NUM_INT_REGISTERS] = { {0} }; uint32_t EIP = 1; // preserve null pointer :(before "End Reset") bzero(Reg, sizeof(Reg)); EIP = 1; // preserve null pointer :(before "End Help Contents") cerr << " registers\n"; :(before "End Help Texts") put_new(Help, "registers", "SubX currently supports eight 32-bit integer registers. From 0 to 7, they are:\n" " EAX ECX EDX EBX ESP EBP ESI EDI\n" "ESP contains the top of the stack.\n" "\n" "-- 8-bit registers\n" "Some instructions operate on eight *overlapping* 8-bit registers.\n" "From 0 to 7, they are:\n" " AL CL DL BL AH CH DH BH\n" "The 8-bit registers overlap with the 32-bit ones. AL is the lowest signicant byte\n" "of EAX, AH is the second lowest significant byte, and so on.\n" "\n" "For example, if EBX contains 0x11223344, then BL contains 0x44, and BH contains 0x33.\n" "\n" "There is no way to access bytes within ESP, EBP, ESI or EDI.\n" "\n" "For complete details consult the IA-32 software developer's manual, volume 2,\n" "table 2-2, \"32-bit addressing forms with the ModR/M byte\".\n" "It is included in this repository as 'modrm.pdf'.\n" "The register encodings are described in the top row of the table, but you'll need\n" "to spend some time with it.\n" "\n" "-- flag registers\n" "Various instructions (particularly 'compare') modify one or more of four 1-bit\n" "'flag' registers, as a side-effect:\n" "- the sign flag (SF): usually set if an arithmetic result is negative, or\n" " reset if not.\n" "- the zero flag (ZF): usually set if a result is zero, or reset if not.\n" "- the carry flag (CF): usually set if an arithmetic result overflows by just one bit.\n" " Useful for operating on unsigned numbers.\n" "- the overflow flag (OF): usually set if an arithmetic result overflows by more\n" " than one bit. Useful for operating on signed numbers.\n" "The flag bits are read by conditional jumps.\n" "\n" "For complete details on how different instructions update the flags, consult the IA-32\n" "manual (volume 2). There's various versions of it online, such as https://c9x.me/x86,\n" "though of course you'll need to be careful to ignore instructions and flag registers\n" "that SubX doesn't support.\n" "\n" "It isn't simple, but if this is the processor you have running on your computer.\n" "Might as well get good at it.\n" ); :(before "End Globals") // the subset of x86 flag registers we care about bool SF = false; // sign flag bool ZF = false; // zero flag bool CF = false; // carry flag bool OF = false; // overflow flag :(before "End Reset") SF = ZF = CF = OF = false; //:: simulated RAM :(before "End Types") const uint32_t SEGMENT_ALIGNMENT = 0x1000000; // 16MB inline uint32_t align_upwards(uint32_t x, uint32_t align) { return (x+align-1) & -(align); } // Like in real-world Linux, we'll allocate RAM for our programs in disjoint // slabs called VMAs or Virtual Memory Areas. struct vma { uint32_t start; // inclusive uint32_t end; // exclusive vector _data; vma(uint32_t s, uint32_t e) :start(s), end(e) {} vma(uint32_t s) :start(s), end(align_upwards(s+1, SEGMENT_ALIGNMENT)) {} bool match(uint32_t a) { return a >= start && a < end; } bool match32(uint32_t a) { return a >= start && a+4 <= end; } uint8_t& data(uint32_t a) { assert(match(a)); uint32_t result_index = a-start; if (_data.size() <= result_index) { const int align = 0x1000; uint32_t result_size = result_index + 1; // size needed for result_index to be valid uint32_t new_size = align_upwards(result_size, align); // grow at least 2x to maintain some amortized complexity guarantees if (new_size < _data.size() * 2) new_size = _data.size() * 2; // never grow past the stated limit if (new_size > end-start) new_size = end-start; _data.resize(new_size); } return _data.at(result_index); } void grow_until(uint32_t new_end_address) { if (new_end_address < end) return; // Ugly: vma knows about the global Memory list of vmas void sanity_check(uint32_t start, uint32_t end); sanity_check(start, new_end_ad
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: package test</title>
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong>test</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><</