ForumsCodeASMASM > C++ RSS



How would one convert 68000 ASM to another language such as C++

example code:
        ext.l	d0
	asl.l	#8,d0
	divs.w	d2,d0
	ext.l	d1
	asl.l	#7,d1
	divs.w	d2,d1


the way I would do it:

1) first convert the asm code to C in a 1 to 1 way

// variables
long d0;
long d1;

// code

// assume we have something in d0 and d1
// keep the lower 16bits only in d0
d0 = d0 & 0x0000ffff;

// test d0 sign on 16 bits
if (d0 & (1 << 15))
// extend sign bit
d0 = d0 | 0xffff0000;

// arithmetic shift left (using a multiply here, but bit shift would work as well)
d0 *= 256;

// divide d2 (declared and initialized somwhere above)
d2 /= d0;

// keep the lower 16bits only in d1
d1 = d1 & 0x0000ffff;

// test d0 sign on 16 bits
if (d1 & (1 << 15))
// extend sign bit
d1 = d1 | 0xffff0000;

// arithmetic shift left (using a multiply here, but bit shift would work as well)
d1 *= 128;

// divide d2 (declared and initialized somwhere above)
d2 /= d1;

2) optimize/ translate your code into something more readable... with named variables, etc.

NOTE: pay attention to the flags that can be set/cleared when you perform math ops. In asm it's easy to test the flags such as overflow, zero, etc. In C you have to test that yourself with your own code

Added 8 minutes later:

I must say that this kind of conversion is only worth if you attempt to convert something complex (like a special algorithm to achieve some weird effect). Usually such conversions are a waste of time and it's much faster to mimic the effect by writing proper code. Of course whenever possible...

This post was edited by st0rmbringer (2009-08-14 05:33, 7 years ago)


Thanks :D


Some more help needed

$900 = dc.w $6f80
$902 = dc.w $1b34

roxl $900(a3)
roxl $902(a3)


ror.b #3,d1
rol.b #3,d1


ROL and ROR do bit rotations

ROL rotates to the left, ROL to the right.

ROXL and ROXR do the same thing but use the eXtended bit in the process.

rotations can be done by 1 to 8 bits.

the ROR.B #3,D1 instruction means that you want to rotate the 8 least significant bits of D1 to the right by 3 positions.

so if D1 contains the following bits


rotating by 1 position will give you


again by 1 position


and again by one position


=> you have rotated by 3 positions

Now the ROXL/R intructions do the same but take a 9th bit in the count,the eXtended bit (I'll mark it as X). Same example:

11110000 X

rotate by 1

X1111000 => 0 goes into X bit

0X111100 => rotate again.

00X11110 => etc.

the ROXL/R work pretty much like a sushi bar. You are bit X and take a sushi from the roller. The chef adds a new one on the roller, the roller moves by one position. You eat the sushi and at the nex rotation you put the plate back on the roller, take the next plate, etc


Thanks Bringer of Storm,any chance someone can provide a small bit of C++ code to show this
BTW did you get my PM ?


got the PM! thanks a lot!

I'll provide you with a C implementation of the ROL/R and ROXL/R instructions in a few minutes


very unoptimized ROL.B implementation in C

int m68k_rol_dot_b_1bit(int value)
int bitOut;

// keep low 8bits only
value &= 0xff;

// shift by 1 bit to the left
value <<= 1;

// keep bit that goes out
bitOut = value & 0x100;
bitOut >>= 8;

// remove 9th bit from value
value &= 0xff;

// make rolled bit enter from the right
value |= bitOut;

// done

int m68k_rol_dot_b(int value, int bitRoll)
int c;

// roll n bits
for (c=0; c < bitRoll; c++)

// done