Help emulating RCL and RCR

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Help emulating RCL and RCR

Post by earlz »

[fixed]

Ok, I have spent an hour or two now trying to figure out where this is going wrong...
my code is simple and understandable...freg.cf is CF btw (r0 is just a temporary variable)

Code: Select all

uint8_t RotateCarryLeft8(uint8_t base,uint8_t count){
	freg.of=(base&0x80)>>7;
	while(count>0){
		freg.r0=freg.cf; //reserved bit as a temp variable...
		freg.cf=(base&0x80)>>7;
		base=(base<<1);
		base|=freg.r0; //puts (old)CF in it's place
		count--;
	}
	freg.of=freg.of^((base&0x80)>>7);
	return base;
}
It seems to work until it gets to a base like this:
11001010b with CF=0, rather than setting CF to 1 and changing the base to 10010100b, it sets CF, but changes the base to 10010101b the last bit is the error, it should be 0, it should only be in CF, but for some reason it puts CF into the result...

I really have no clue...I have tried quite a few things, and this is the only way I see how it can be done...yet, it doesn't work...

my test code is

Code: Select all

mov bx,0
mov bl,0x32
mov cl,4
stc
rcl bl,cl
;here, BL should be 0x29, but instead it is 0x2A
adc bl,0
;here, the carry flag should be set and BL added to 0x30, but the carry flag is not set and BL is still 0x2A
cmp bl, 0x30
[code]

[edit]
omfg...please, no one make such a stupid mistake as me...0x29+1=0x2A, NOT 0x30
Post Reply