I want Python to mimic register behavior

Asked 2 years ago, Updated 2 years ago, 38 views

Can Python simulate the behavior of 32-bit registers?

EAX=AX+AH,AL
AX=AH+AL

Of course, AL is going to go up to AH.

In C++, I think the following declaration can be simulated, but how about Python?

 union REG {
    DWORDe;
    US>structure{
        WORD x, xh;;
    };
    US>structure{
        BYTEL,h;
    };
};

REGregA;

python python3

2022-09-30 19:20

1 Answers

C++ may be able to simulate the following declaration

The code presented simply means that you can retrieve and configure some of your 32-bit data in 16-bit or 8-bit increments, so you can do something similar with properties if it is equivalent to that.

class REG:
  def__init__(self):
    self.e = 0
  @property
  defx(self):
    return self.e&0xFFFF
  @property
  def xh(self):
    return(self.e>>16)&0xFFFF
  @property
  def(self):
    return self.e&0xFF
  @property
  defh(self):
    return(self.e>8)&0xFF
  @x.setter
  defx(self,val):
    self.e=(self.e&0xFFFF0000)|(val&0xFFFF)
  @xh.setter
  def xh(self,val):
    self.e=(self.e&0xFFFF)|(val&0xFFFF)<<16)
  @l.setter
  def(self,val):
    self.e=(self.e&0xFFFF00)|(val&0xFF)
  @ h.setter
  defh(self,val):
    self.e=(self.e&0xFFFF00FF)|(val&0xFF)<8)

r = REG()
r.e = 0x1234ABCD
print(hex(r.e))#->0x1234abcd
r.x = 0x5678
print(hex(r.e),hex(r.x))#->x123456780x5678
r.xh = 0x90EF
print(hex(r.e),hex(r.xh))#->0x90ef56780x90ef
r.l = 0x12
print(hex(r.e),hex(r.l))#->0x90ef5612 0x12
r.h = 0xAB
print(hex(r.e),hex(r.h))#->0x90efab120xab


2022-09-30 19:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.